Magento – Update cart does not work properly

cartquantity

When I'm clicking the "update cart"-button on the configure cart item page, the quantity of the item is added instead of update. For example: if I've got 2 apples in my cart and enter as new quantity 3, then the cart is update to 5 apples instead of 3.

This is the code of my updatecart.phtml:

<?php $_product = $this->getProduct(); ?>
<?php $buttonTitle = $this->__('Update Cart'); ?>
<?php if ($_product->isSaleable()): ?>
    <div class="add-to-cart">
        <?php if (!$_product->isGrouped()): ?>
        <label for="qty"><?php echo $this->__('Qty:') ?></label>
        <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
        <?php endif; ?>
        <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>
        <?php echo $this->getChildHtml('', true, true) ?>
    </div>
<?php endif; ?>

Best Answer

First, you want to make sure the block type for this template is checkout/cart, that way all the given methods are available. Then the following should be able to produce what you want it to do:

<form action="<?=Mage::getUrl('checkout/cart/updatePost'); ?>">
  <?php foreach($this->getItems() as $_item): ?>
<?php if ($_product->isSaleable()): ?>

  <div class="add-to-cart">

      <?php if ( ! $_item->getProduct()->isGrouped()): ?>

      <label for="qty"><?php echo $this->__('Qty:') ?></label>
      <input type="text" name="cart[<?=$_item->getId()][qty]" maxlength="12" value="<?php echo $_item->getQty(); ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
      <?php endif; ?>

      <button type="button" title="<?php echo $buttonTitle ?>" class="button btn-cart" name="update_cart_action" value="update_qty">
      <span><span><?=$this->__('Update Cart');?></span></span>
      </button>
      <?php echo $this->getChildHtml('', true, true) ?>
  </div>

<?php endif; // isSaleable() ?>
  <?php endforeach; // $this->getItems()
</form>

What happens here is that the correct form is being used, you'll be looping through the cart items and have an update field for each one. There's work to be done for you, right now there's no label or anything to show what product goes with the qty field you may be updating. But this should work.

Be aware, this is untested.