Magento – Get quantity of specific item (simple product) in cart by ID

quantityshopping-cart

Seems straight forward but I am seeing every scenario on Google except for the one I need. I just need to "query" Magento (or its quote model?) and return that number in the shopping cart based on the ID I provide via PHP. I believe Magento loops through the product data as line items so I can't lock down the qty field that is plainly visible right next to the item I just added to the cart.

Background: I have created an upsell module I've and I need to multiply the upsells by the number of base products in the cart.

Best Answer

Ultimately, there are many ways to select the specific products or product IDs of the cart. Whatever method you use is dependent upon what your needs are. I will summarize the last lines of code that allowed me to provide an ID anywhere on the cart page and return the qty of that cart product:

$_item = 123; // Whatever your product ID is. You can use any method like $_product->getProductId() to get to this point.
$_product= Mage::getSingleton('catalog/product')->load($_item);
$theqty = $this->escapeHtml($_product->getQty());
echo $theqty; // Prints quantity out
Related Topic