$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Extra items added to cart on refresh, woocommerce|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Extra items added to cart on refresh, woocommerce

matteradmin9PV0评论

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

Post a comment

comment list (0)

  1. No comments so far