1

This is my file:

<?php $_product = $this->getProduct(); ?>
<?php $buttonTitle = $this->__('Add to Cart'); ?>
<?php if($_product->isSaleable()): ?>
    <div class="product-pricing">
        <?php echo $this->__('This product is available for purchase in increments of %s only.', /**$this->getProductName(),*/ $this->getProductDefaultQty()) ?>
    </div>
    <div class="add-to-cart">
        <?php if(!$_product->isGrouped()): ?>
            <div class="qty" style="display: inline-flex">
                <label for="qty"><?php echo $this->__('Qty:') ?></label>
                <a class="decrement_qty" href="javascript:void(0)" >
                    <i class="fa fa-minus-circle fa-lg" aria-hidden="true" >
                    </i>
                </a>
                <input id="qty" class="input-text qty" type="text" title="Qty" value="<?php echo $this->getProductDefaultQty() * 1 ?>" maxlength="12" name="qty" />
                <a class="increment_qty" href="javascript:void(0)">
                    <i class="fa fa-plus-circle fa-lg" aria-hidden="true" ></i>
                </a>
            </div>
        <?php endif; ?>
        <button type="button" title="<?php echo $buttonTitle ?>" id="product-addtocart-button" 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; ?>

<script type="text/javascript">
//<![CDATA[
    jQuery(document).ready(function(){
    jQuery('.increment_qty').click(function() {
        var oldVal = jQuery(this).parent().find("input").val();
        if ( parseFloat(oldVal) >= 1 ) {
            var newVal = parseFloat(oldVal) + 1;
            jQuery(this).parent().find("input").val(newVal);
        }
    });

    jQuery('.decrement_qty').click(function() {
        var oldVal = jQuery(this).parent().find("input").val();
        if ( parseFloat(oldVal) >= 2 ) {
            var newVal = parseFloat(oldVal) - 1;
            jQuery(this).parent().find("input").val(newVal);
        }
        });
    });
//]]>
</script>

This is the result.

risultato

But it increment-decrement vaule by only 1.

My question is: Can I increase or decrease the quantity of the qty_increments value set in the inventory of the product?

qty_increments

In this case I need to increase or decrease by 500.

Thanks and your suggestions

2
  • you want to increment by 500 for each click, exemple 500, 1000, 1500 ... ?
    – PЯINCƎ
    Jul 25, 2017 at 15:59
  • For this product are 500. In fact if I change +1 or -1 in "var newVal = parseFloat (oldVal) + or - 1" in + or - 500, it works. But for another product is another value I put into the inventory - "qty increments". I've put the back-end screenshot.
    – Roberto
    Jul 25, 2017 at 19:59

2 Answers 2

1

I think this can be done by modifying the 1 within javascript to echo $_product->getStockItem()->getData('qty_increments')

e.g.

<script type="text/javascript">
//<![CDATA[
jQuery(document).ready(function(){
jQuery('.increment_qty').click(function() {
    var oldVal = jQuery(this).parent().find("input").val();
    if ( parseFloat(oldVal) >= 1 ) {
        var newVal = parseFloat(oldVal) + <?php echo $_product->getStockItem()->getData('qty_increments'); ?>;
        jQuery(this).parent().find("input").val(newVal);
    }
});

jQuery('.decrement_qty').click(function() {
    var oldVal = jQuery(this).parent().find("input").val();
    if ( parseFloat(oldVal) >= 2 ) {
        var newVal = parseFloat(oldVal) - <?php echo $_product->getStockItem()->getData('qty_increments'); ?>;
        jQuery(this).parent().find("input").val(newVal);
    }
    });
});
//]]>
</script>

This is not tested but have seen it used: https://community.magento.com/t5/Programming-Questions/Get-product-quantity-increments-on-Product-view-page/m-p/53475#M6765

3
  • IT WORKS. It would take some remodeling to correct some minor problems, but it may be fine. I had found that solution, but having only the basics of php and java, I had tried inserting it but with catastrophic results.
    – Roberto
    Jul 25, 2017 at 20:06
  • Awesome glad you have sorted it!
    – harri
    Jul 25, 2017 at 20:07
  • I changed the line "if (parseFloat (oldVal)> = 1) {" in "if (parseFloat (oldVal)> = 0) {" otherwise if with the - arrive at 0 the value freezes.
    – Roberto
    Jul 26, 2017 at 5:27
0

If you incease or decrease quantity by one then you can try below code:

<label for="qty"><?php echo $this->__('Qty:') ?></label>
<div class="qty_cart">
      <div class="qty-ctl">
           <button title="decrease" onclick="changeQty(0); return false;" class="decrease">decrease</button>
      </div>
      <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
      <div class="qty-ctl">
           <button title="increase" onclick="changeQty(1); return false;" class="increase">increase</button>
      </div>
</div>

<script type="text/javascript">
    function changeQty(increase) {
        var qty = parseInt($('qty').value);
        if (!isNaN(qty)) {
            qty = increase ? qty + 1 : (qty > 1 ? qty - 1 : 1);
            $('qty').value = qty;
        }
    }       
</script>

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.