Add custom URL redirect to Woocommerce cart

Brief

For a client of mine we added bespoke Add to Cart buttons outside the loop. When a user added the item to their cart the parameter ‘add-to-cart’ still remained in the URL string.┬áSo when the updated their cart by adding or removing the product it would keep adding a single item back in.

The solution is to create a custom re-direct once a product has been added to the cart. This code could also be used to bypasss the cart and go straight to the checkout.

Solution:

  1. Add the following to your theme's functions.php file

    function redirect_add_product_to_cart() { 
        
        // Custom URL
        return <YOUR URL>; 
    
        // This links directly to your checkout page and bypasses the cart
        //return get_permalink( get_option('woocommerce_checkout_page_id') ); 
    
    }
    add_filter( 'woocommerce_add_to_cart_redirect', 'redirect_add_product_to_cart' );