Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress Woocommerce product is being added to cart on page refresh

I have a problem using woocommerce.

After I add a product to cart the link in browser becomes link/?add-to-cart=72,and if i refresh the page the product is added again in cart.

Every refresh addes the product in cart. I disabled all plugins except woocommerce and still the same.

Any ideea on how to fix this ?Thank You.

like image 909
Agnes Pandek Avatar asked Oct 23 '17 05:10

Agnes Pandek


1 Answers

I had the same issue once, here is the code that you should add to your theme’s functions.php file, or to your own custom plugin:

add_action('add_to_cart_redirect', 'cipher_add_to_cart_redirect');
 
function cipher_add_to_cart_redirect($url = false) {
 
     // If another plugin beats us to the punch, let them have their way with the URL
     if(!empty($url)) { return $url; }
 
     // Redirect back to the original page, without the 'add-to-cart' parameter.
     // We add the `get_bloginfo` part so it saves a redirect on https:// sites.
     return get_bloginfo('wpurl').add_query_arg(array(), remove_query_arg('add-to-cart'));
 
}

It will add a redirection when users added products to their cart. I hope this helps.

like image 144
Asfandyar Khan Avatar answered Nov 03 '22 00:11

Asfandyar Khan