woocommerce_add_to_cart_item_name_in_quotes
Filters the product name when it's displayed in quotes within the cart item.
add_filter( 'woocommerce_add_to_cart_item_name_in_quotes', $callback, 10, 1 );
Description
Allows customization of the product name when added to the cart, specifically when enclosed in quotes. Developers can modify the product name string before it's displayed in cart-related messages, offering control over how product titles appear to users in certain contexts.
Usage
add_filter( 'woocommerce_add_to_cart_item_name_in_quotes', 'your_function_name', 10, 1 );
Parameters
-
$product_id(mixed) - - **$product_id** `mixed`
Return Value
string public function custom_add_to_cart_message_html( $message, $products, $show_qty ) { $override = get_option( 'ulgm_add_to_cart_message', '' ); if ( ! empty( $override ) && ulgm_filter_has_var( 'add-seats' ) ) { $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 ); } $found_license = false; foreach ( $products as $product_id => $qty ) { translators: %s: product name */ $product = wc_get_product( $product_id ); if ( $product instanceof WC_Product && $product->is_type( 'license' ) ) { $found_license = true; break; } } if ( ! $found_license ) { return $message; } foreach ( $products as $product_id => $qty ) { translators: %s: product name */
Examples
add_filter( 'woocommerce_add_to_cart_item_name_in_quotes', 'my_custom_product_name_in_quotes', 10, 2 );
/**
* Modifies the product name when displayed in quotes in the add-to-cart message.
* This example adds a specific prefix to product names that are of a certain type (e.g., 'subscription').
*
* @param string $product_name The current product name in quotes.
* @param int $product_id The ID of the product.
* @return string The modified product name in quotes.
*/
function my_custom_product_name_in_quotes( $product_name, $product_id ) {
$product = wc_get_product( $product_id );
// Check if the product is a subscription product
if ( $product && class_exists( 'WC_Product_Subscription' ) && $product instanceof WC_Product_Subscription ) {
$product_name = sprintf( _x( 'Subscription: “%s”', 'Subscription product name in quotes', 'my-text-domain' ), $product_name );
}
return $product_name;
}
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/woocommerce/woocommerce-license.php:455
public function custom_add_to_cart_message_html( $message, $products, $show_qty ) {
$override = get_option( 'ulgm_add_to_cart_message', '' );
if ( ! empty( $override ) && ulgm_filter_has_var( 'add-seats' ) ) {
$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 );
}
$found_license = false;
foreach ( $products as $product_id => $qty ) {
/* translators: %s: product name */
$product = wc_get_product( $product_id );
if ( $product instanceof WC_Product && $product->is_type( 'license' ) ) {
$found_license = true;
break;
}
}
if ( ! $found_license ) {
return $message;
}
foreach ( $products as $product_id => $qty ) {
/* translators: %s: product name */
$titles[] = ( $qty > 1 ? absint( $qty ) . ' × ' : '' ) . apply_filters( 'woocommerce_add_to_cart_item_name_in_quotes', sprintf( _x( '“%s”', 'Item name in quotes', 'woocommerce' ), strip_tags( get_the_title( $product_id ) ) ), $product_id );
$count += $qty;
}
$titles = array_filter( $titles );
// The custom message is just below
$added_text = sprintf( str_replace( '{{product}}', '%s', $override ), 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( '%s', esc_html( $added_text ) );
}
}
return $message;
}