Add to Cart Button Not Working and Refreshes with Cache: Wordpress / Woocommerce

Viewed 12

I have seen around 15 articles on the internet with this exact issue. When adding a product to the cart in WooCommerce, the page will only refresh. Of course there are many conflicts that could be causing this issue; but some you might not want to disable. I have wrote my own solution for this problem and need to share it publicly for anyone else having this issue.

1 Answers

This code will store a cookie in the users browser containing the current cart amount. IF the cart amount increases in comparison with the cookie, the browser will redirect to the cart page (unless already on the cart page). IF the cart amount decreases (the user has removed something from the cart), then there will be no redirect to the cart. Simply, this is a functions.php solution to redirect to /cart/ on cart value increase.

// If Cart Value Increases, Redirect to Cart
 add_action( 'wp_head', 'cart_updated' );
function cart_updated() { 

$uri = $_SERVER['REQUEST_URI'];
$amount = WC()->cart->cart_contents_total;

if ((($amount > 0) & ($_COOKIE['amount']) < $amount) & ($uri != "/cart")) {
    setcookie("amount", $amount, time()+30*24*60*60, "/");
    header("Location: https://ellasbubbles.com/cart/");
}

else if (($amount > 0) & ($_COOKIE['amount']) != $amount) {
     setcookie("amount", $amount, time()+30*24*60*60, "/");
}
}
Related