Customizing add-to-cart messages based on the product IDs in WooCommerce 3
Asked Answered
P

2

1

I have two products in my website and want to display different messages (In message I want to use HTML) on adding different products to cart. Right now it displays Product successfully added to cart. I am using this code in my child's function.php which is working but is not giving me what I exactly want.

add_filter ( 'wc_add_to_cart_message', 'wc_add_to_cart_message_filter', 10, 2 );
function wc_add_to_cart_message_filter($message, $product_id = null) {
   $titles[] = get_the_title( $product_id );

$titles = array_filter( $titles );
$added_text = sprintf( _n( '%s has been successfully added to your Basket.', '%s have been added to your Basket.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) );

$message = sprintf( '%s <a href="%s" class="button">%s</a>',
               esc_html( $added_text ),
               esc_url( wc_get_page_permalink( 'cart' ) ),
               esc_html__( 'View Cart', 'woocommerce' ));

return $message;
}
Patrilocal answered 19/8, 2017 at 8:26 Comment(1)
I have two products say p1 and p2, if either of the product is added to the cart it displays a message "Your product is added to the cart and a button of 'View Cart' ". I want to display different messages on adding different product like if I add p1 to cart it should display "message1" and if add p2 to cart it should display "message2". I hope now it is making some sense.Patrilocal
N
6

Since WooCommerce 3 wc_add_to_cart_message is replaced by wc_add_to_cart_message_html as it's deprecated now. The correct way to get this working customizing add to cart messages based on product IDs (or even product categories):

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

    foreach( $products as $product_id => $quantity ){

        // (If needed) get the WC_Product object
        $product = wc_get_product( $product_id );
        // The product title
        $product_title = $product->get_title();

        // Set Here a product category Id, name or slug (for example, if needed)
        $product_category = "Clothing";
        if( has_term( 'clothing', 'product_cat', $product_id ) ){
            return __("My custom message for product category \"$product_category\" for $product_title ($product_id)", "woocommerce");
        }

        // Set HERE your first Product ID
        if( $product_id == 37 ){
            return __("My custom message 1 for $product_title ($product_id)", "woocommerce");
        }
        // Set HERE your Second Product ID
        elseif( $product_id == 40 ){
            return __("My custom message 2 for $product_title ($product_id)", "woocommerce");
        }
    }
    return $message;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested in WooCommerce 3+ and works.

Neodarwinism answered 21/8, 2017 at 20:45 Comment(0)
G
0

If anyone can't see the changes, check if any plugin already trigger this filter, so make sure the $priority is higher

add_filter( string $hook_name, callable $callback, int $priority = 10, int $accepted_args = 1 )
Gone answered 24/10, 2021 at 12:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.