0

I'm looking to have product quantities increased and decreased dynamically in cart when I press the plus and minus sign bypassing the add to cart functionality altogether.

I have looked thought many forums but can find no reference of this being done before.

Can someone point me towards a good starting point to start writing a module like.

2
  • Please use the search function. Here is a great post about writing a module: magento.stackexchange.com/questions/8344/…
    – SR_Magento
    Jan 19, 2015 at 18:30
  • Im actually more interested in any information on the functionality im looking for and not so much on creating a general module.
    – David
    Jan 19, 2015 at 19:45

2 Answers 2

1

Basically, what you need to do is something like this:

Create two buttons for each product (next to the predefined quantity or somewhere like that). One will increment the product in the cart the other will decrement. I also recommend that these buttons contain product-specific identifiers attached to them. Upon a click action these buttons will activate a javascript function that will send information to a controller via an AJAX request. This controller will handle the reduction or it's converse based upon the data variables it is passed. It will also send information back to the javascript function with which you will use to update the page with various jQuery method calls.

Hope this helps. Good luck.

1
  • To add to this, I do the same, however I don't send a AJAX on click, as users spamming the add/decrease button can cause issues. On clicking my +/- I show a update Qty/Totals button which issues the Ajax request on click (and is part of my continue functions when the continue is clicked instead) May 27, 2015 at 7:36
0

Here is how I do this on my 1.9.2.2 Website : (I think it must be the same solution as @barry-carlyon, but I will detail mine)

First, I give an id (form-update-post) to the form of the cart page : (checkout/cart.phtml)

<form action="<?php echo $this->getFormActionUrl() ?>" method="post" id="form-update-post">

Then, I add two buttons, and I add an id to the qty field of the items : (checkout/cart/item/default.phtml)

<a href="javascript:decreaseQty(<?php echo $_item->getId() ?>)" class="decrement_qty"><span class="fa fa-minus"></span></a>
<input id="quantity-item-<?php echo $_item->getId() ?>" name="cart[<?php echo $_item->getId() ?>][qty]"
   data-cart-item-id="<?php echo $this->jsQuoteEscape($_item->getSku()) ?>"
   value="<?php echo $this->getQty() ?>" size="4" title="<?php echo $this->__('Qty') ?>" class="input-text qty" maxlength="12" />
<a href="javascript:increaseQty(<?php echo $_item->getId() ?>)" class="increment_qty"><span class="fa fa-plus"></span></a>

Notice that the link of the buttons are javascript functions : (javascript:increaseQty, javascript:decreaseQty)

The last step is to add those Javascript functions in the cart page : (checkout/cart.phtml)

<script type="text/javascript">
    function increaseQty(id){
        var input = jQuery("#quantity-item-"+id);
        input.val(parseInt(input.val())+1);
        var form = jQuery("#form-update-post");
        if(form !== undefined){
            form.submit();
        }
    }

    function decreaseQty(id){
        var input = jQuery("#quantity-item-"+id);
        if(input.val() > 1){
            input.val(input.val()-1);
        }
        var form = jQuery("#form-update-post");
        if(form !== undefined){
            form.submit();
        }
    }
</script>

What it is doing is increasing or decreasing by one the field of the quantity for the given item, then submitting the form of the cart. This way, the page will reload, and the new quantity will be saved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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