WC_Cart::get_item_product_name
Gets the display name for a cart item.
For variations, selected "Any" attribute values that are missing from the stored variation name are appended so the name matches fully defined variations. The stored product and variation names are not modified.
Method of the class: WC_Cart{}
No Hooks.
Returns
string. The product name including any selected "Any" attribute values, or an empty string when no product can be resolved from the arguments.
Usage
$WC_Cart = new WC_Cart(); $WC_Cart->get_item_product_name( $cart_item, $product );
- $cart_item(array) (required)
- Cart item.
- $product(WC_Product|null)
- Optional product object to use as the name source, e.g. the result of the woocommerce_cart_item_product
Default:cart item's product
Changelog
| Since 11.2.0 | Introduced. |
WC_Cart::get_item_product_name() WC Cart::get item product name code WC 11.2.1
public function get_item_product_name( $cart_item, $product = null ) {
if ( ! $product instanceof WC_Product ) {
$product = is_array( $cart_item ) && isset( $cart_item['data'] ) && $cart_item['data'] instanceof WC_Product ? $cart_item['data'] : null;
}
if ( ! $product instanceof WC_Product ) {
return '';
}
$variation = isset( $cart_item['variation'] ) && is_array( $cart_item['variation'] ) ? $cart_item['variation'] : array();
return wc_get_container()->get( SelectedVariationName::class )->get_product_name( $product, $variation, true );
}