Skip to content

Instantly share code, notes, and snippets.

@neilgee
Last active September 17, 2017 05:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neilgee/e037ee587b318a7e7369359ec48c39d6 to your computer and use it in GitHub Desktop.
Save neilgee/e037ee587b318a7e7369359ec48c39d6 to your computer and use it in GitHub Desktop.
WooCommerce Hide Number of Items When Added to Cart
<?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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment