Get the id of the last product added to the basket in purple

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)

+4
source share
3 answers

Call

 Mage::getSingleton('checkout/session')->getLastAddedProductId(true); 

Actually clears the session variable after reading it. Magento makes extensive use of magic methods. In this case, you use the __call magic method, which in turn uses the getData () method. In Mage_Core_Model_Session_Abstract_Varien you will see that they override the default behavior of getData () so that the second parameter is logical (the first parameter getData is the key name for the desired value). This boolean is a flag telling the session to clear the variable after reading.

You can always listen to the checkout_cart_product_add_after event and add the item to your own variable in the session. This event actually fires on the line before calling setLastAddedProductId ().

+4
source

try grep the variable you are looking for. Since they come from magic methods, then it is difficult to find the exact function, after which it is easier for you to see the places where the data is installed than where they are used.

 grep '>setLastAddedProductId' app/code -rsn 

to find out where the product id is set to this session variable

 app/code/core/Mage/Checkout/Model/Cart.php:255: $this->getCheckoutSession()->setLastAddedProductId($product->getId()); 

and then you can set this variable (if it is set else empty ())

 Mage::getSingleton('checkout/session')->getLastAddedProductId(); 

and you can see everything in checkout / session and see if data is available.

 var_dump(Mage::getSingleton('checkout/session')); 
+1
source

I don't know why it works this way, but it works for me in Magento 1.6 ...

 <?php require_once ( "app/Mage.php" ); umask(0); Mage::app("default"); Mage::getSingleton('core/session', array('name'=>'frontend')); $session = Mage::getSingleton('checkout/session'); $lastadded = $session->getData("last_added_product_id"); print_r($lastadded); 

Apparently you need to create an instance of the kernel / session and then verify / session. I tried it differently, but this is the only way to find work. Perhaps someone can explain why it works this way. Hope this helps you!

0
source

Source: https://habr.com/ru/post/1369212/


All Articles