Product AddToCart Product list page magento 1.9.1

I am using JavaScript function to add product to cart, below is my JavaScript code :

<script>
        function myFunction() {
        jQuery.ajax({url: '<?php echo $this->helper("checkout/cart")->getAddUrl($_product) ?>', 
        success: function(data) {
        console.log('Data Sent');
        }
        })  
        }
        </script>

Ans the button code is:

<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="myFunction()"><span><?php echo $this->__('Add to Cart') ?></span></button>

But when I click addtocart the different product is getting added to cart, please can anybody let me know what is the issue in the above code ?

Solutions

This is because you added java-script code in bottom of file so it will always add last item from listing page.

so you need to put the JS code inside the foureach loop or you need to dynamically call URL of add to cart so update code below and try again

<script>
        function myFunction(addtocarturl) {
        jQuery.ajax({url: addtocarturl, 
        success: function(data) {
        console.log('Data Sent');
        }
        })  
        }
    </script>

and button code also

<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="myFunction('<?php echo $this->helper("checkout/cart")->getAddUrl($_product) ?>')"><span><?php echo $this->__('Add to Cart') ?></span></button>

Hope this will help you!

Similar questions

Magento 2: unable to override product/view/addtocart.phtml from my module
I am trying to customize the product/view/addtocart.phtml template in my extension. I was already able to customize other templates but for this one it just won't work. I found the template is defined in among much more XML is this bit: I know the XML which determines what template is loaded for the "product.info.addtocart". Because if I change the...
How to override addtocart.phtml for all type of product in magento 2
I have done this but my code is not working for all types of product. layout file Template
Magento 2 - How to move the product price block before addtocart button?
I am using custom theme and trying to move the "product.info.price" block before the addtocart button. I have tried, direct into addtocart.phtml file but not working. app\design\frontend\my_package\my_theme\Magento_Catalog\templates\product\view\addtocart.phtml If I used : Then it moves the price but it only appears on simple products without custo...
Having problems extending product view / addtocart class's (php and phtm)
I need to preset the product quantity on the product page. NB1: my products have new yes/no attribute defined in backend called formula_price-active. NB2: for formula_price_active product, I pass parameters via product url to pre-configure my product page I then extended the view.php class to add/overload custom functions : and the view.phtml to ad...
unable to access a product in addtocart html file
I created a addtocart.phtml wit fly effect. This is working fine in product view page. But in product listing page I am unable to get the product details. I mean in product view page if print echo '<pre>'; print_r($_product); I can get product details but in listing page I am unable to get. listing page code as below: confix.xml in product li...
Addtocart Rest api for Configurable product in magento2?
Addtocart Rest api for Configurable product in magento2 ?

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.