rasti-at-webkinder
9/18/2016 - 10:31 AM

Wordpress filter to customize message in wc_add_to_cart_message. Add this into your theme's functions.php file. WordPress 4.5.2. WooCommerce

Wordpress filter to customize message in wc_add_to_cart_message. Add this into your theme's functions.php file. WordPress 4.5.2. WooCommerce 2.5.5.

// Custom add to cart message
add_filter('wc_add_to_cart_message','custom_add_to_cart_message',10,2);

function custom_add_to_cart_message( $message, $product_id ) {
    
    // Get title of the added product(s)
    $titles = array();

	if ( is_array( $product_id ) ) {
		foreach ( $product_id as $id ) {
			$titles[] = get_the_title( $id );
		}
	} else {
		$titles[] = get_the_title( $product_id );
	}

	$titles = array_filter( $titles );
    
    // Set the text
    $added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) );
    
    // Set button links
    $shop_button = wc_get_page_permalink( 'shop' );
    $cart_button = wc_get_page_permalink( 'cart' );
    
    // Construct message
    if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) { // When the autoredirect option is set to yes
        $message   = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $shop_button ), esc_html__( 'Continue Shopping', 'woocommerce' ), esc_html( $added_text ) );
    } else { // Otherwise
        $message   = sprintf( '<a href="%s" class="button wc-forward">%s</a><a href="%s" class="button wc-forward">%s</a> %s', esc_url( $cart_button ), esc_html__( 'View Cart', 'woocommerce' ), esc_url( $shop_button ), esc_html__( 'Continue Shopping', 'woocommerce' ), esc_html( $added_text ) ) ;
    }
    
    // Output message
    return $message;
}