2

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?

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 3

4

The solution would be to lose the ?add-to-cart=25part 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.

1
  • Basicaly yes, but where do you hook it? I can't echo the script as DOM on woocommerce_add_to_cart hook as it is called also on ajax calls and would inject DOM to JSONs
    – Fanky
    Commented Mar 4, 2023 at 12:24
2

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
 }
}
1
  • Works really well - excellent contribution. Still not sure why the underlying issue, essentially a dysfunctional relationship between ajax remove from cart and basic add_to_cart link usage, isn't better handled.
    – CK MacLeod
    Commented Nov 4, 2023 at 15:38
1

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' );
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.