简体   繁体   中英

How can I add an extra button the the wc_add_to_cart_message in Woocommerce?

I want to add a continue shopping button to the Add to Cart message that pops up after someone adds a product to the cart. I can change the current "View cart" button to a continue shopping button using this code:

add_filter( 'wc_add_to_cart_message', 'custom_add_to_cart_message' );
function custom_add_to_cart_message() {
    global $woocommerce;
    $return_to  = get_permalink(woocommerce_get_page_id('shop'));
    $message    = sprintf('<a href="%s" class="button wc-forwards">%s</a> %s', $return_to, __('Continue shopping', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
    return $message;
}

But I want to add an additional button and keep the "View Cart" button too.

Thanks

Add the follows code snippet in your active theme's functions.php -

function modify_wc_add_to_cart_message_html( $message, $products ) {
    $message = sprintf( '<a href="%s" tabindex="1" class="button wc-forward" style="margin-left: 10px;">%s</a> %s',esc_url( wc_get_page_permalink( 'shop' ) ), esc_html__( 'Continue shopping', 'woocommerce' ), $message );
    return $message;
}
add_filter( 'wc_add_to_cart_message_html', 'modify_wc_add_to_cart_message_html', 99, 2 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM