0

I have a problem in product listing quantity counter, sometimes it will work and sometimes it will not. I want to create custom JQuery script to enable product counter increment and decrement, But the problem is that, it is a listing page so if I create script others will be affected. Please correct me if I'm wrong.

Below attached is screenshot.

enter image description here

Here's my script that is working in product detail page only.

 var $currentQTY=parseInt($('#qty').val());

    $(".qty-inc").click(function(){
        $currentQTY=$currentQTY+1;
        $('#qty').val($currentQTY);
        // console.log($currentQTY);
    });

    $(".qty-dec").click(function(){
        $currentQTY=$currentQTY-1;
        if ($currentQTY<1){
            $currentQTY=0;
        }
        $('#qty').val($currentQTY);
        // console.log($currentQTY);
    });

Thanks

5
  • Were you able to resolve the issue ? if yes, please post your answer Nov 7, 2017 at 7:49
  • Hello @ManashviBirla yes I resolved this one in my end. Nov 7, 2017 at 7:59
  • can you please post your solution here, so that it would be of help.! Nov 7, 2017 at 9:24
  • Hello @ManashviBirla I'm a little bit busy right now in my work. But If I have free time I will post it in here. Thanks Nov 7, 2017 at 9:28
  • Hi @ManashviBirla I have now posted the solution of my problem below. please check it. I just added a jquery codes to addtocart.phtml found on Magento_Catalog/templates/product/view/addtocart.phtml Nov 7, 2017 at 9:41

2 Answers 2

0

Others get affected because id of each input box is same.

You can achieve whatever you want by doing following -

You will have to define each input box with different-2 id and give a common class name for each input box (.qty).

For example -

Input boxes id will be like - #qty_1, #qty_2

Class name will be like - .qty

Note - Suffix in the input box id can be the product id of the respective product.

Then put both increment and decrement buttons and input box in the same division (By doing this you can easily find the respective input box). Then you can use the object of clicked element (this) in the function to identify the clicked element.

Assuming that your structure of division is something like following -

<div>
<input class="qty" id="qty_1" />
<button class="qty-inc">+</button>
<button class="qty-dec">-</button>
</div>

Javascript code Example -

$(".qty-inc").click(function() {
   // This will return the quantity of the box which is clicked
  // this is the object of element, which has been clicked
   var currentQTY = parseInt($(this).parent().find(".qty").val());
   // Increasing the quantity
   currentQTY = currentQTY + 1;
   // Setting the quantity of only that element,
   //which has been clicked
   $(this).parent().find(".qty").val(currentQTY);
});

You can do the same with .qty-dec , make sure to remove

var currentQTY=parseInt($('#qty').val());

line as the replacement of that line is included inside the click function.

0

Here's how I solved my problem.

This is the codes.

enter image description here

4
  • which file is this ? Nov 7, 2017 at 9:41
  • app/design/frontend/Magento_Catalog/templates/product/view/addtocart.phtml Nov 7, 2017 at 9:42
  • @ManashviBirla click accept my answer if this answer helped you. so that others can see this and help them if they will encounter a problem like this in the future. Thanks Nov 7, 2017 at 9:46
  • You're welcome man! Just sharing what I got. Thanks Nov 7, 2017 at 10:11

Your Answer

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

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