Extra items added to cart on refresh, woocommerce

I have a client site, , where there are AJAX 'add to cart' buttons auto-generated by WooCommerce.If I visit t

I have a client site, /, where there are AJAX 'add to cart' buttons auto-generated by WooCommerce.

If I visit the page with an empty cart and click ADD it adds a single item to the cart as expected. The page is reloaded and a popup cart slides into the window.

If I refresh the browser a second item is added to the cart without clicking the ADD button.

How to prevent such bug?

  • This is not a query param problem, there url is clean
  • I had a second product on the same page, clicking ADD (p1) and then after the reload clicking ADD (p2), p1 gets added a second time so I have a total of 3 items in the cart after two clicks

I have a client site, https://www.ndsclub.com/product/bcaa-page/, where there are AJAX 'add to cart' buttons auto-generated by WooCommerce.

If I visit the page with an empty cart and click ADD it adds a single item to the cart as expected. The page is reloaded and a popup cart slides into the window.

If I refresh the browser a second item is added to the cart without clicking the ADD button.

How to prevent such bug?

  • This is not a query param problem, there url is clean
  • I had a second product on the same page, clicking ADD (p1) and then after the reload clicking ADD (p2), p1 gets added a second time so I have a total of 3 items in the cart after two clicks
Share Improve this question asked Aug 21, 2018 at 12:16 theblindprophettheblindprophet 1011 silver badge4 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

If you look on the network tab of the developer tools from your browser, you will see that the product is NOT added by AJAX.

Yes, there is an AJAX-Request but that sends a nonce to "woosb_custom_data" and adds no product to your cart. (blue cross on the screenshot)

Normaly the "add to cart" AJAX-Request should go to an address like "example.com/?wc-ajax=add_to_cart".

The product is add via a normal POST-Request. (red cross on the screenshot)

If you reload the page the browser will send the POST-Request again. Try the reload test with an firefox browser and the browser will ask you if you want to send the POST-Request again.

By the way there is an problem with your cart page.

https://www.ndsclub.com/cart/

If you remove all item from the cart (hit the red cross for every item) the cart-view will be white and there is no navigation back to your shop.

In the case of Simple Product

Simply add the following script at the bottom of
/woocommerce/single-product/add-to-cart/variation-add-to-cart-button.php

In the case of Variable Product

Simply add the following script at the bottom of
/woocommerce/single-product/add-to-cart/variation-add-to-cart-button.php

<script>
    if ( window.history.replaceState ) {
    window.history.replaceState( null, null, window.location.href );
    }
</script>

This will prevent the form to execute on page refresh thus keeping your cart as it was before the page refresh. Tried & tested with Woocommerce 7.1.0

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1737596132a3958377.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信