Hide Amount of Items from Add to Cart Message in WooCommerce

Need to hide the amount of items added to the cart in the success message that appears after adding an item  in WooCommerce, you can use the wc_add_to_cart_message_html filter. You can add the following snippet to your functions.php.

add_filter( 'wc_add_to_cart_message_html','codeithub_wb_custom_add_to_cart_message', 10, 2 );
/**
 * Hide number of items added as to message when added to cart.
 *
 */
function codeithub_wb_custom_add_to_cart_message( $message, $products ) {
	$titles = array();
	$count  = 0;
	$show_qty = false;

	if ( ! is_array( $products ) ) {
		$products = array( $products => 1 );
		$show_qty = false;
	}

	if ( ! $show_qty ) {
		$products = array_fill_keys( array_keys( $products ), 1 );
	}

	foreach ( $products as $product_id => $qty ) { // diff in loop, ternary operator removed
		$titles[] =  sprintf( _x( '“%s”', 'Item name in quotes', 'woocommerce' ), strip_tags( get_the_title( $product_id ) ) );
		$count += $qty;
	}

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

	// Output success messages
	if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) {
		$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) );
		$message   = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue shopping', 'woocommerce' ), esc_html( $added_text ) );
	} else {
		$message   = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View cart', 'woocommerce' ), esc_html( $added_text ) );
	}


	return $message;

}

Before:

See also  Display % Discount on Shop and Loop Pages
Add to cart message in Woocommerce

After:

Hide Amount of Items

Leave a Reply

Your email address will not be published. Required fields are marked *