1

I used Increment & Decrement qty in Shopping Cart page

   <div class="qty-ctl">    
    <button title="Decrease Qty" onclick="changeQty1('<?php echo 'qty_'.$_item->getId() ?>',0); return false;" class="decrease"><?php echo $this->__('decrease') ?></button>    
</div>      

    <input id="<?php echo 'qty_'.$_item->getId() ?>" 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" />

<div class="qty-ctl">         
    <button title="Increase Qty" onclick="changeQty1('<?php echo 'qty_'.$_item->getId() ?>',1); return false;" class="increase"><?php echo $this->__('increase') ?></button>       
</div>  

Script

     <script type="text/javascript">
             function changeQty(id, num) {
    var qty_id = id;

    var currentVal = parseInt($(qty_id).value);
    if (currentVal != NaN)
    {
        if (!e) e = window.event;
    var keyCode = e.keyCode || e.which;
    if(keyCode != 13) {
        $(qty_id).value = currentVal + num;
    }
         document.getElementById("scart").submit();
    }
}





function changeQty1(id, num) {
        var qty_id = id;

        var currentVal = parseInt($(qty_id).value);
        if (currentVal != NaN)
        {

            if(num != 0) {
                $(qty_id).value = currentVal + 1;
            }else {
                $(qty_id).value = currentVal - 1;
           }
             document.getElementById("scart").submit();
        }
    }
            </script>

in here Manual entry is working but +/- Button Not working i don't know what is the problem?

1

try that code..

<script type="text/javascript">
function changeQty(id, num) {
    var qty_id = id;

    var currentVal = parseInt($(qty_id).value);
    if (currentVal != NaN)
    {
        if (!e) e = window.event;
    var keyCode = e.keyCode || e.which;
    if(keyCode != 13) {
        $(qty_id).value = currentVal + num;
    }

    }
}

make different function for both for button use changeQty1

function changeQty1(id, num) {
        var qty_id = id;

        var currentVal = parseInt($(qty_id).value);
        if (currentVal != NaN)
        {

            if(num != 0) {
                $(qty_id).value = currentVal + 1;
            }else {
                $(qty_id).value = currentVal - 1;
           }
             document.getElementById("scart").submit();
        }
    }


<div class="qty-ctl">    
        <button title="Decrease Qty" type="button"  onclick="changeQty1('<?php echo 'qty_'.$_item->getId() ?>',0); return false;" class="decrease"><?php echo $this->__('decrease') ?></button>    
    </div>      

        <input id="<?php echo 'qty_'.$_item->getId() ?>" 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" onblur="document.getElementById('scart').submit()" />

    <div class="qty-ctl">         
        <button title="Increase Qty" type="button" onclick="changeQty1('<?php echo 'qty_'.$_item->getId() ?>',1); return false;" class="increase"><?php echo $this->__('increase') ?></button>       
    </div> 
| improve this answer | |
  • increment/decrement button not working – Magento 2 Feb 4 '16 at 7:23
  • do you have live links website? – Qaisar Satti Feb 4 '16 at 7:24
  • try now updated the code.. – Qaisar Satti Feb 4 '16 at 7:30
  • bro now manual enter qty not working if i enter 5 the press enter it automatically reduced 4 – Magento 2 Feb 4 '16 at 7:33
  • updated code check and feedback me – Qaisar Satti Feb 4 '16 at 7:36
0

You need to use code like below

<div class="quantity"> 
        <button id="subtractQty" class="decrease"><?php echo $this->__('decrease') ?></button> 
        <input readonly 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="qty"/>
        <button id="addQty" class="increase"><?php echo $this->__('increase') ?></button>
</div> 

script:

<script type="text/javascript">
    jQuery("#addQty").click(function() {
        var value = parseInt(jQuery("#qty").val())+ parseInt(1);    
        jQuery("#qty").val(value);                                  
    });

    jQuery("#subtractQty").click(function() {
        if(jQuery("#qty").val() > 1 ) {
            var value = parseInt(jQuery("#qty").val())- parseInt(1);    
            jQuery("#qty").val(value);      
        }
    });                                

</script>
| improve this answer | |
  • This code not works when you have 2 products or more because you play with id's – osrecio Feb 3 '16 at 17:54
  • But Manual enter qty not working – Magento 2 Feb 3 '16 at 18:03
  • I have updated my answer please try with this one – Prashant Valanda Feb 3 '16 at 18:11
  • Let as assume i have 2 product in cart 1st product qty i type 10 and then enter it automatically change into 9 second is working fine – Magento 2 Feb 4 '16 at 6:30

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.