Magento checkout_cart_product_add_before and get the number of products added

I created the checkout_cart_product_add_before event: http://markshust.com/2012/08/27/create-checkout_cart_product_add_before-observer-magento

When I do this, getQuoteItem () seems to be unavailable, so I cannot get the number of products added to the cart. When I use the _after method, I can use:

public function checkStock2($observer) { Mage::log("Check stock before"); $request = $observer->getQuoteItem(); Mage::log("q in order = " .$request['qty'] .""); } 

However, when I get to this, I cannot get to getQuoteItem, since it does not exist yet. Is there a way to get the number of products that the user is trying to add to the cart?

Thanks!

+4
source share
1 answer

If you have carried out a related observer, you lack the necessary information. You must add the request to your dispatch manager to verify that the user has selected:

 public function addProduct($productInfo, $requestInfo=null) { $product = $this->_getProduct($productInfo); $request = $this->_getProductRequest($requestInfo); Mage::dispatchEvent('checkout_cart_product_add_before', array( 'product' => $product, 'request' => $request )); return parent::addProduct($productInfo, $requestInfo); } 

Then you can get the number of users in your observer with:

 $observer->getEvent()->getRequest()->getQty(); 

The method ->getRequest() refers to the magic getter for the request parameter of your dispatcher.

+7
source

All Articles