Search code examples
phpwordpresswoocommercemessagecart

Set cart permalink when cart is not empty


I have no cart link set in the backend of WooCommerce. Instead I have the cart redirected to the checkout page. This works fine, however now I end up with empty urls. When I add a product to the cart I get the message 'successfully added to the cart, see the cart here'. 'See the cart here' is linked to wc_get_page_permalink( 'cart' ), but this is not set.

Is it possible through a function to set the wc_get_page_permalink( 'cart' ) ONLY when items are in the cart?

I tried something like:

function custom_continue_shopping_redirect_url ( $product_id ) {
    $url = "http://www.url.com"; // Add your link here
    return $url;
}
add_filter('wc_add_to_cart_message', 'custom_continue_shopping_redirect_url');

But that is obviously replacing the whole add to cart message.

Thanks.


Solution

  • You missed some code. try it this way:

    add_filter( 'wc_add_to_cart_message', 'custom_continue_shopping_redirect_url', 10, 2 );
    function custom_continue_shopping_redirect_url( $message, $product_id ) {
        global $woocommerce;
    
            // $permalink = get_permalink(woocommerce_get_page_id('shop')); // replaced by:
            $permalink = get_permalink(woocommerce_get_page_id('cart')); // your link here.
            $message    = sprintf('<a href="%s" class="button wc-forwards">%s</a> %s', $permalink, __('Continue Shopping', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
        return $message;
    }
    

    You will have to fine tune your replacement link. You can also change the text…

    References: