admin 管理员组文章数量: 1184232
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
2 Answers
Reset to default 1If 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
本文标签: Extra items added to cart on refresh WooCommerce
版权声明:本文标题:Extra items added to cart on refresh, woocommerce 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1737596133a1851260.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论