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, the wc_add_to_cart_message_html filter has you covered.

woocommerce-hide-amount-item-message

Add the below code snippet to your functions.php

<?php //<~ don't add me
add_filter( 'wc_add_to_cart_message_html','wb_custom_add_to_cart_message', 10, 2 );
/**
* Hide number of items added as to message when added to cart.
*
*/
function 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( '&ldquo;%s&rdquo;', '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;
}
view raw hide-cart-item.php hosted with ❤ by GitHub

On line 24 a ternary operator has been removed which displayed the quantity if greater than 1 – now when you add an item the message will just reference the product title.

woocommerce-hide-amount-item-message-filter

You can also now adjust the other text that is used by adjusting the values in the $added_text variable.

2 Comments

  1. Sallie Goetsch on September 19, 2017 at 2:51 pm

    Your code snippet seems to have disappeared.

    • Neil Gowran on September 19, 2017 at 8:28 pm

      That’s weird, can see it here in an incognito not logged in.

Leave a Comment