Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Continue Shopping button from Added to Cart Notice

Tags:

woocommerce

Currently when someone adds a product to our website it says: "X Product" has been added to your cart | and then has a "Continue Shopping" button in that notice that is on the right.

Since we only sell 2 products we want to remove the continue shopping button completely but still say the rest of the message, and keep "X Product" as a link.

I've been using the following code (but it replaces Continue Shopping with Checkout and I'd prefer to just remove the button completely instead). I just can't figure out how to remove the button but still keep the rest of the message exactly the same:

add_filter( 'woocommerce_continue_shopping_redirect', 'my_changed_woocommerce_continue_shopping_redirect', 10, 1 );
function my_changed_woocommerce_continue_shopping_redirect( $return_to ){

    $return_to = wc_get_page_permalink( 'checkout' );

    return $return_to;
}


add_filter( 'wc_add_to_cart_message_html', 'my_changed_wc_add_to_cart_message_html', 10, 2 );
function my_changed_wc_add_to_cart_message_html($message, $products){

    if (strpos($message, 'Continue shopping') !== false) {
        $message = str_replace("Continue shopping", "Checkout", $message);
    }

    return $message;

}
like image 817
Questioners12 Avatar asked Mar 01 '18 21:03

Questioners12


1 Answers

Use preg_replace to find the string containing the full link and return the new message with all the original HTML minus the link.

add_filter('wc_add_to_cart_message_html','remove_continue_shoppping_button',10,2);

function remove_continue_shoppping_button($message, $products) {
    if (strpos($message, 'Continue shopping') !== false) {
        return preg_replace('/<a.*<\/a>/m','', $message);
    } else {
        return $message;
    }
}
like image 183
Steve Eldridge Avatar answered Nov 05 '22 20:11

Steve Eldridge