user2208907
user2208907

Reputation: 1

Geting quantity for a single item in cart magento

Hello i need to get the quantity of the item added in cart for the item i am viewing in product detail

atm i am using this

<?php  $cart = Mage::getModel('checkout/cart')->getQuote();
                                            $result = array();

                                            foreach ($cart->getAllItems() as $item) {

                                                $result['qty'] = $item->getQty();

                                            }
                                            $qty=$result['qty'];
                                            //echo $qty ; 

                                            if(!$qty == 0){
                                               echo '<span>'.$qty.'</span>';
                                            }else{
                                                 echo "<span>0</span>";
                                            }?>

but it returns only the last qty added/updated in the cart, and i need the specific single product qty

Upvotes: 0

Views: 901

Answers (1)

Marius
Marius

Reputation: 15206

Try this:

//$product is current product that you are viewing
$qty = null;
foreach ($cart->getAllItems() as $item) {
    if ($product->getId() == $item->getProductId()) {
        $qty = $item->getQty();
    }
}
if (!is_null($qty)) {
    //$qty is what you are looking for
}
else {
    //the current product is not in the cart
}

Upvotes: 1

Related Questions