When a customer views their cart, each product that they have added will be listed. By default, Magento provides info for each product, e.g. name, image, quantity, price. Customers can change the quantity in the text box and submit the “Update Shopping Cart” button. That is a lot of effort for the user, we want to make this process as smooth as possible.
We can make it easier by adding +1 and -1 links to change the quantity of each item. We will do this with some very simple JavaScript which takes the current value and adjusts it. Once this has been done, we submit the form through JavaScript to reduce customer effort.
In my implementation I have used images for the links, but obviously you could use text, e.g (↑ ↓). The following two code snippets should surround the input quantity box on the shopping cart/basket page:
<input name="cart[<?php echo $_item->getId() ?>][qty]" value="<?php echo $this->getQty() ?>" size="4" title="<?php echo $this->__('Qty') ?>" class="input-text qty" maxlength="12" id="cart[<?php echo $_item->getId() ?>][qty]"/>
This can be found in app/design/frontend/[interface_name]/[theme_name]/template/checkout/cart/item/default.phtml
Increase by one:
<a onclick="changeItemQuantity( <?php echo $_item->getId() ?>, 1 ); return false;" href="#"><img alt="add-arw" src="<?php echo $this->getSkinUrl('images/add-arw.png') ?>"></a>
Decrease by one:
<a onclick="changeItemQuantity( <?php echo $_item->getId() ?>, -1 ); return false;" href="#"><img alt="add-arw" src="<?php echo $this->getSkinUrl('images/add-arw.png') ?>"></a>
Now the JavaScript function. We do not want it duplicated for every item so you can either put it in your theme’s JavaScript file or just below the table in the parent template – app/design/frontend/[interface_name]/[theme_name]/template/checkout/cart.phtml
After the quantity is changed, we submit the cart form so that the totals are kept up to date. When decreasing quantity, Magento removes the item after form submission if the quantity is 0.
function changeItemQuantity( id , num ) { var qty_id = "cart[" + id + "][qty]"; var currentVal = parseInt( $(qty_id).value ); if ( currentVal != NaN ) { $(qty_id).value = currentVal + num; $("products-table-basket").submit(); } }
There, a tiny bit of JavaScript which will help to ease the customer’s journey to purchasing from your Magento Store!
Related posts:
Worked perfectly! I am using Magento 1.5.1.0. Thank you for the post.
I wrote a new Extension for this:
http://www.magentocommerce.com/magento-connect/kevinhorst/extension/6991/kh_cartqtybuttons
Hi,
Do you know if this is meant to work with 1.4 as I’m getting an error on the final line of the java-script:
$(“products-table-basket”).submit();
Nowhere in the cart.phtml can I see any reference to $product-table-basket. Am I missing something?
Thanks,
Ian
Ah yes, from 1.4 they removed the id from the /checkout/cart/updatePost/ form. You should just be able to add it back in, worked for me.
if you want to use this on the cart page here’s my approach:
<select name="cart[getId() ?>][qty]" class="input-text qty" id="qty" onchange="form.submit()">
getQty() == $value) $selected = ' selected="selected"';
else $selected = '';
?>
<option value="">
it formatted the code
Check it here http://pastebin.com/ipGfqgLs
Where do you add the javascript to? I tried adding it under the table above the other script, along with trying to add it to other locations all over the page to no avail. Does this work on 1.6?