Magento – Quantity increment button

magento2.1.6PHPquantity

I have configured a product with the "Qty increments" (100) / and " Minimum Qty Allowed in Shopping Cart" (100).
When I go to the product page, the quantity is 100 by default but when I click on "+", the quantity is increase to 101 and not 200.
enter image description here

enter image description here

Can you help me?

Thanks in advance.

Best Answer

Here I am guiding you adding plus/minus functionality with increment of 100 in default file. Use the code accordingly.

In catalog module edit your Quantity function which comes by default in addtocart.phtml like below code

<div class="field qty">
            <label class="label" for="qty"><span><?php /* @escapeNotVerified */ echo __('Qty') ?></span></label>
            <div class="control">
                <input type="number"
                       name="qty"
                       id="qty"
                       maxlength="12"
                       value="<?php /* @escapeNotVerified */ echo $block->getProductDefaultQty() * 1 ?>"
                       title="<?php /* @escapeNotVerified */ echo __('Qty') ?>" class="input-text qty"
                       data-validate="<?php echo $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
                       />
            </div>
        </div>

Update the code below adding class to input field and pass input id/name to buttons data field and for

<div class="field qty">

                <div class="control qty-numbers">
                    <div class="input-group">


                        <span class="input-group-btn">
                            <button type="button" class="btn btn-default btn-number quantity-left-minus"  data-type="minus" data-field="qty">
                              <span class="glyphicon glyphicon-minus"></span>
                            </button>
                        </span>


                        <input type="number"
                        name="qty"
                        class="form-control input-number"
                        id="qty"
                        min="1"
                        maxlength="12"
                         value="<?php /* @escapeNotVerified */ echo $block->getProductDefaultQty() * 1 ?>"
                                 title="<?php /* @escapeNotVerified */ echo __('Qty') ?>" class="input-text qty"
                                 data-validate="<?php echo $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
                                 />


                        <!--plus button-->
                        <span class="input-group-btn">
                            <button type="button" class="btn btn-default btn-number quantity-left-plus" data-type="plus" data-field="qty">
                                <span class="glyphicon glyphicon-plus"></span>
                            </button>
                        </span>
                    </div>
                </div>
            </div>

do it carefully and then add below script in bootstrap.js

$(document).ready(function(){

var quantitiy=100;
   $('.quantity-right-plus').click(function(e){

        // Stop acting like a button
        e.preventDefault();
        // Get the field name
        var quantity = parseInt($('#qty').val());

        // If is not undefined

            $('#qty').val(quantity + 100);


            // Increment

    });

     $('.quantity-left-minus').click(function(e){
        // Stop acting like a button
        e.preventDefault();
        // Get the field name
        var quantity = parseInt($('#qty').val());

        // If is not undefined

            // Increment
            if(quantity>100){
            $('#qty').val(quantity - 100);
            }
    });

});

This works on default, if doesn't work with your then please pass me the quantity or add to cart code.

I hope this solve your problem.

Thanks

Related Topic