wc_add_to_cart_message()
Add to cart messages.
Hooks from the function
Returns
Mixed.
Usage
wc_add_to_cart_message( $products, $show_qty, $return );
- $products(int|array) (required)
- Product ID list or single product ID.
- $show_qty(true|false)
- Should quantities be shown? Added in 2.6.0.
Default:false - $return(true|false)
- Return message rather than add it.
Default:false
wc_add_to_cart_message() wc add to cart message code WC 10.7.0
function wc_add_to_cart_message( $products, $show_qty = false, $return = false ) {
$titles = array();
$count = 0;
if ( ! is_array( $products ) ) {
$products = array( $products => 1 );
$show_qty = false;
}
if ( ! $show_qty ) {
$products = array_fill_keys( array_keys( $products ), 1 );
}
$product_id = null;
foreach ( $products as $product_id => $qty ) {
/**
* Filters the quantity HTML for the add to cart message.
*
* @since 2.6.1
* @param string $qty_html The quantity HTML.
* @param int $product_id The product ID.
*/
$title = apply_filters( 'woocommerce_add_to_cart_qty_html', ( 1 !== $qty ? wc_stock_amount( $qty ) . ' × ' : '' ), $product_id );
/**
* Filters the item name in quotes for the add to cart message.
*
* @since 2.6.1
* @param string $item_name_in_quotes The item name in quotes.
* @param int $product_id The product ID.
*/
$title .= apply_filters(
'woocommerce_add_to_cart_item_name_in_quotes',
sprintf(
/* translators: %s: product name */
_x( '“%s”', 'Item name in quotes', 'woocommerce' ),
wp_strip_all_tags( get_the_title( $product_id ) )
),
$product_id
);
$titles[] = $title;
$count += $qty;
}
$titles = array_filter( $titles );
/* translators: %s: product name */
$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.
$wp_button_class = wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '';
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( '%s <a href="%s" class="button wc-forward%s">%s</a>', esc_html( $added_text ), esc_url( $return_to ), esc_attr( $wp_button_class ), esc_html__( 'Continue shopping', 'woocommerce' ) );
} elseif ( ! CartCheckoutUtils::has_cart_page() ) {
$message = sprintf( '%s', esc_html( $added_text ) );
} else {
$message = sprintf( '%s <a href="%s" class="button wc-forward%s">%s</a>', esc_html( $added_text ), esc_url( wc_get_cart_url() ), esc_attr( $wp_button_class ), esc_html__( 'View cart', 'woocommerce' ) );
}
if ( has_filter( 'wc_add_to_cart_message' ) ) {
wc_deprecated_function( 'The wc_add_to_cart_message filter', '3.0', 'wc_add_to_cart_message_html' );
$message = apply_filters( 'wc_add_to_cart_message', $message, $product_id );
}
$message = apply_filters( 'wc_add_to_cart_message_html', $message, $products, $show_qty );
if ( $return ) {
return $message;
} else {
wc_add_notice( $message, apply_filters( 'woocommerce_add_to_cart_notice_type', 'success' ) );
}
}