wc_add_to_cart_message

Add to cart messages.

Description

wc_add_to_cart_message( (int|array) $products, (bool) $show_qty = false ); 

Parameters (2)

0. $products (int|array)
The products.
1. $show_qty — Optional. (bool) => false
Should qty's be shown? Added in 2.6.0

Usage

  1. if ( !function_exists( 'wc_add_to_cart_message' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-cart-functions.php'; 
  3.  
  4. // The products. 
  5. $products = null; 
  6.  
  7. // Should qty's be shown? Added in 2.6.0 
  8. $show_qty = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wc_add_to_cart_message($products, $show_qty); 
  12.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-cart-functions.php  
  1. function wc_add_to_cart_message( $products, $show_qty = false ) { 
  2. $titles = array(); 
  3. $count = 0; 
  4.  
  5. if ( ! is_array( $products ) ) { 
  6. $products = array( $products ); 
  7. $show_qty = false; 
  8.  
  9. if ( ! $show_qty ) { 
  10. $products = array_fill_keys( array_keys( $products ), 1 ); 
  11.  
  12. foreach ( $products as $product_id => $qty ) { 
  13. $titles[] = ( $qty > 1 ? absint( $qty ) . ' × ' : '' ) . sprintf( _x( '“%s”', 'Item name in quotes', woocommerce ), strip_tags( get_the_title( $product_id ) ) ); 
  14. $count += $qty; 
  15.  
  16. $titles = array_filter( $titles ); 
  17. $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 ) ); 
  18.  
  19. // Output success messages 
  20. if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) { 
  21. $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' ) ); 
  22. $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 ) ); 
  23. } else { 
  24. $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 ) ); 
  25.  
  26. wc_add_notice( apply_filters( 'wc_add_to_cart_message', $message, $product_id ) );