0

I have different simple products each one have a Qty Increments .

I want to increment (+/-) each product by its Qty Increments on product page. my increment code by 1 is :

<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>

please help me

4
  • Please can you be more expolicit ? what do you mean by "I want to increment (+/-) each product by its Qty Increments on product page." ?
    – PЯINCƎ
    Aug 10, 2017 at 12:29
  • please you can take a look to harmonieflorale.fr/produit/noeud-de-chaise-lycra-choco on qty block Aug 10, 2017 at 12:40
  • Whats the problem? Seems to work!?!
    – sv3n
    Aug 10, 2017 at 16:21
  • I want to do like this website for quantity increment NB:each product has its own quantity increment step Aug 10, 2017 at 16:27

3 Answers 3

0

You can do like below code:

You can get Qty Increments using the below code with Product Object:

$qtyIncrements = $productObj->getQtyIncrements();

If you are not getting the Qty Increments value then you need to load Product Inventory stock data. using the below code:

$productStockObj = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
$qtyIncrements = $productStockObj->getQtyIncrements();

HTML:

<div class="col-sm-3 col-xs-4">
    <div class="details-qty qty shop-list">
        <span class="mini_decqty qty_btn btn btn-default" data-id="<?php echo $productId; ?>" data-field="item-<?php echo $productId; ?>-qty" data-type="minus" data-qty="<?php echo $qtyIncrements; ?>">-</span>
        <input type="number" size="4" class="item-qty" maxlength="12" id="item-<?php echo $productId; ?>-qty" value="<?php echo $qty; ?>" name="item-<?php echo $productId; ?>-qty" readonly="readonly">
        <span class="mini_incqty qty_btn btn btn-default" data-id="<?php echo $productId; ?>" data-field="item-<?php echo $itemId; ?>-qty" data-type="plus" data-qty="<?php echo $qtyIncrements; ?>">+</span>
    </div>
</div>

JS Code:

$(document).delegate("span.qty_btn", "click", function(e) {
    fieldName = $(this).attr('data-field');
    var incQty = parseInt($(this).attr('data-qty'));
    dataId = $(this).attr('data-id');
    type = $(this).attr('data-type');
    var input = $("input[name='" + fieldName + "']");
    var currentVal = parseInt(input.val());
    if (type === 'minus') {
        if ((currentVal - incQty) < 1) {
            //Noting will happen as It leads to 0 qty here
        } else {
            input.val(currentVal - incQty).change();
        }
    } else if (type === 'plus') {
        input.val(currentVal + incQty).change();
    }
});
8
  • i dont want to increment by 1 , i want that each product will increment by its qty increment Aug 10, 2017 at 12:37
  • for exmple 10 20 30 if my qty increments is 10 Aug 10, 2017 at 12:37
  • You can fetch that qty increment and add in span as data-increment preoperty like we are using data-type and in function can use this attribute value for increment and decrement Aug 10, 2017 at 12:40
  • can I have more information about that please, I dont get how to do that exactly Aug 10, 2017 at 12:43
  • I have updated my answer, hope this help Aug 10, 2017 at 12:51
0

It also interests me. I have Magento 2.2.6 but I can not find a solution. I looked everywhere on the Internet, without success. I also asked a question here:

my question

0

You can try below code for increment and decrement qty in product detail page.

<div>
  <a class="decrement_qty" href="javascript:void(0)">
    <img src="images/minus-qty.png" width="16" height="16" alt="&amp;ndash;" />
  </a>

  <input name="qty" size="4" value="1" />

  <a class="increment_qty" href="javascript:void(0)">
    <img src="images/plus-qty.png" width="16" height="16" alt="&amp;plus;" />
  </a>
</div>


<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>

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.