admin管理员组文章数量:1124385
We use this type of Add to Cart URL on our site a lot of times: /?add-to-cart=25. The problem is after it redirects to the Cart page, if the client changes his/her mind and removes the recently added product, the Cart page loads and adds it again since "?add-to-cart=25" is on the URL.
My question is is it possible to remove the "?add-to-cart=product-id" part after the redirection to Cart?
We use this type of Add to Cart URL on our site a lot of times: http://yourdomain.com/cart/?add-to-cart=25. The problem is after it redirects to the Cart page, if the client changes his/her mind and removes the recently added product, the Cart page loads and adds it again since "?add-to-cart=25" is on the URL.
My question is is it possible to remove the "?add-to-cart=product-id" part after the redirection to Cart?
Share Improve this question edited Sep 27, 2017 at 8:01 kireirina asked Sep 27, 2017 at 7:32 kireirinakireirina 311 silver badge3 bronze badges 2- 1 Sounds like a caching problem. You should check with your specific plugin's dev/support team for assistance. – WebElaine Commented Sep 27, 2017 at 15:43
- It's not a caching problem. If I go to mydomain.com/cart/ and then refresh the page, there is no problem. But if I got to mydomain.com/cart/?add-to-cart=25 and then refresh the page, the cart will try to add the same product once again - it will add a product on each refresh. It's a problem of keeping the $_GET query variable "add-to-cart" in the URL. – dev_masta Commented Jan 12, 2018 at 0:18
3 Answers
Reset to default 4The solution would be to lose the ?add-to-cart=25
part of the URL.
The simplest way to do that is with javascript History API:
history.pushState(null, "", location.href.split("?")[0]);
Docs: https://developer.mozilla.org/en-US/docs/Web/API/History_API
The DOM window object provides access to the browser's history through the history object. It exposes useful methods and properties that let you move back and forth through the user's history, as well as -- starting with HTML5 -- manipulate the contents of the history stack.
It was introduced in HTML5 so older browsers might not support it.
Combining @xnagyg's hook and @dev_masta's JS, this gets rid of ?add-to-cart
in the cart page so you can freely use refresh and back button. Also doesn't affect AJAX functionality
add_action('woocommerce_add_to_cart_redirect', 'refresh_function');
function refresh_function(){
if ( is_page( 'cart' ) || is_cart() ) {
?>
<script>
history.pushState(null, "", location.href.split("?")[0]);
</script>
<?php
}
}
This is my solution (redirect to the checkout page if the user clicked on a custom add-to-cart button):
function xnagyg_redirect_checkout_add_cart( $url ) {
//continue only if we found add-to-cart in the URL!!!
if ( empty( $_GET['add-to-cart'] ) || ! is_numeric( $_GET['add-to-cart'] ) ) {
return false; //do not redirect
}
$url = get_permalink( get_option( 'woocommerce_checkout_page_id' ) ); //Redirect to Checkout page
return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'xnagyg_redirect_checkout_add_cart' );
本文标签: woocommerce offtopicRemove quotaddtocartproductidquot from Add to Cart URL when on the Cart page
版权声明:本文标题:woocommerce offtopic - Remove "?add-to-cart=product-id" from Add to Cart URL when on the Cart page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736619661a1945545.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论