Magento - Get Cart Items for this Product ID

I am trying to get basket items for this product;

I tried this code:

$product = Mage::getModel('catalog/product') ->setStoreId(Mage::app()->getStore()->getId()) ->load('2784'); $quote = Mage::getSingleton('checkout/cart')->getQuote(); $cartItems = $quote->getItemByProduct($product); foreach ($cartItems as $item) { echo $item->getId()."<br/>"; } 

but gives nothing.

How can I change my code to use "getItemByProduct" in the correct form?

Thanks for the help.

+6
source share
3 answers

getItemByProduct() returns the first match of Mage_Sales_Model_Quote_Item , so there is no need for an extra loop.

 $item = $quote->getItemByProduct($product); if ($item !== false) echo $item->getId(); 
+10
source

I would use

 foreach ($quote->getItems() as $item) { if ($item->getProductId() == $product->getId()) { print_r($item->getData()); } } 
+1
source

You cannot use the getItemByProduct() function in the checkout/cart or checkout/quote model, since this method applies to the sales/quote model.

This method can be found in the Mage_Sales_Model_Quote class . therefore withe sales/quote . hope this is helpful.

+1
source

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


All Articles