I have a small problem with product QTY, i have an ajax update cart extension and i need to display product qty, everything is ok until I add two products in cart. For example if I add first product in cart the quantity is perfect, but when I add the second product the quantity for all product is the last product quantity. I have the following code:

$cart_items = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
    foreach($cart_items as $items) {
            $qtyr = $items->getQty();

           $htmlItem .=' <div class="qty-btngroup">
                            <input type="text" class="input-text qty" maxlength="12" title="Qty" size="4" value="'.$qtyr.'" name="" readonly="readonly">              

        $_response = Mage::getModel('ajaxminicart/response')

How can I solve this, I need the qty to be display for each product ..

Update: I paste here my entire public function: http://codepad.org/2ovDqy4i

  • second (}) should closed at end d code
    – Amit Bera
    Mar 18, 2015 at 13:14
  • Hi amit thank you for your answer, but if I close { at the end, the products is duplicate
    – Robert
    Mar 18, 2015 at 13:17

1 Answer 1


You need to move that bracket out and place it after $htmlItem. Like this..

$cart_items = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
foreach($cart_items as $items) {
    $qtyr = $items->getQty();

    Mage::log($items->getSku()); // EXAMINE THIS

    $htmlItem .= ' <div class="qty-btngroup">'
        . '<input type="text" class="input-text qty" maxlength="12" title="Qty" size="4" value="'.$qtyr.'" name="" readonly="readonly">'
        . ' </div>';
$_response = Mage::getModel('ajaxminicart/response')
  • Hi thank you but is not working, the second product and the quantity input field is replicating.
    – Robert
    Mar 18, 2015 at 13:36
  • look here i paste my entire public function here: codepad.org/2ovDqy4i
    – Robert
    Mar 18, 2015 at 13:38
  • I'm pretty sure you have a misplaced bracket. Just make sure your each iteration of the foreach loop covers all of that $htmlItem.
    – laketuna
    Mar 18, 2015 at 13:43
  • look at the code here codepad.org/2ovDqy4i I add the code with your modifications but there instead 2 products I have 4 :)
    – Robert
    Mar 18, 2015 at 13:44
  • You see 4 products instead of 2 because you're looping through the quote items TWICE. SEe lines 32 and 129.
    – laketuna
    Mar 18, 2015 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.