I am trying to get a product id that has recently been added to the users cart. A quick google search showed this feature
Mage::getSingleton('checkout/session')->getLastAddedProductId(true);
which is also used in Mage_Checkout_Block_Cart_Crosssell. However, when I try to call a function in my own controller, it returns nothing. I tried to instantiate the main session via
Mage::getSingleton('core/session', array('name'=>'frontend'))
however, this approach does not seem to work. I also tried to create a Crossell block and make a protected method that wraps around the getLastAddedProductId public function, but returns null the same as when I tried to call it myself.
Is there something I need to call or instantiate in order to use this function? Here is my list of sources for reference.
class Mymodule_Addcartaftermath_ItemaddedController extends Mage_Core_Controller_Front_Action { public function generatemessageAction() { $parameters = $this->getRequest()->getParams(); if (isset($parameters['ajax_call'])) { $latest_item_id = Mage::getSingleton('checkout/session')->getLastAddedProductId(true); $response = array('response' => $latest_item_id); echo json_encode($response); } else { $this->_redirect('/'); } } }
I tried to get the source code, especially the checkout / model / session.php file in the kernel, and I cannot find the function definition. I also looked at the definition of the class of parents, but could not find it there either.
If this method is not available to me, is there another way to get the last item added? I know that the items are added sequentially, and I could just get the last item in the item list from the basket, however this will not work if the user adds the same item to the basket, significantly increasing the quantity, and not the actual one (for example, the user adds a laptop to the basket, if it is already there)