WooCommerce Hook: woocommerce_cart_item_name
Marc Wagner
September 11, 2024
There are a variety of hooks in WooCommerce that allow developers to customize the behavior of WooCommerce without changing the core code.
One of the most useful hooks is woocommerce_cart_item_name
, which is used to change or customize the name of a product in the shopping cart.
In this blog post, I’ll show you how to use the WooCommerce Hook woocommerce_cart_item_name
and give you some practical PHP examples.
What is the woocommerce_cart_item_name
Hook? #
The woocommerce_cart_item_name
hook allows you to change the name of a product in the shopping cart view.
This can be useful if you want to add additional information such as product variants, custom fields or even HTML elements to improve the display for the user.
Hook syntax #
The basic syntax of the hook is as follows:
add_filter( 'woocommerce_cart_item_name', 'meine_funktion_name_anpassen', 10, 3 ); function meine_funktion_name_anpassen( $produkt_name, $warenkorb_item, $warenkorb_item_key ) { // Hier kannst du den Produktnamen anpassen. return $produkt_name; }
Parameters:
- $product_name (string): The current product name.
- $cart_item (array): Information about the product in the shopping cart, e.g. quantity, ID, etc.
- $cart_item_key (string): A unique key for the product in the shopping cart.
Example 1: Extending product names with additional text #
In the first example, we add a user-defined text to the product name. This is useful if, for example, you want to add a note such as “Special offer” to the product name.
add_filter( 'woocommerce_cart_item_name', 'sonderangebot_text_hinzufuegen', 10, 3 ); function sonderangebot_text_hinzufuegen( $produkt_name, $warenkorb_item, $warenkorb_item_key ) { $neuer_name = $produkt_name . ' - <span style="color: red;">Sonderangebot!</span>'; return $neuer_name; }
In this example, the text “Special offer!” is added to each product name in the shopping cart, formatted with a red text. This draws more attention to special products.
Example 2: Customize product name based on category #
In the next example, I adjust the product name based on the category of the product. Let’s say you only want to change the name for products in the “Sale” category.
add_filter( 'woocommerce_cart_item_name', 'produkt_name_nach_kategorie_anpassen', 10, 3 ); function produkt_name_nach_kategorie_anpassen( $produkt_name, $warenkorb_item, $warenkorb_item_key ) { $produkt_id = $warenkorb_item['product_id']; $kategorien = wp_get_post_terms( $produkt_id, 'product_cat' ); foreach ( $kategorien as $kategorie ) { if ( $kategorie->slug == 'sale' ) { $produkt_name .= ' - <span class="sale-hinweis">Im Sale!</span>'; } } return $produkt_name; }
In this example, the product name is only adjusted for products in the “Sale” category. If the product is in this category, a note “On sale!” is added.
Example 3: Adding user-defined fields to the product name #
Another useful example is adding custom fields to the product name. Let’s say you have a custom field called “Product code” that you want to display in the shopping cart.
add_filter( 'woocommerce_cart_item_name', 'benutzerdefinierte_felder_hinzufuegen', 10, 3 ); function benutzerdefinierte_felder_hinzufuegen( $produkt_name, $warenkorb_item, $warenkorb_item_key ) { $produkt_id = $warenkorb_item['product_id']; $produkt_code = get_post_meta( $produkt_id, '_produkt_code', true ); if ( ! empty( $produkt_code ) ) { $produkt_name .= '<br><small>Produkt-Code: ' . esc_html( $produkt_code ) . '</small>'; } return $produkt_name; }
Here the custom field “_product_code” is retrieved and added to the product name if it contains a value. This can be useful for specialized stores where additional information about the product should be visible directly in the shopping cart.
Conclusion #
The woocommerce_cart_item_name
hook is extremely flexible and can be used to improve the user experience in the shopping cart.
With the PHP examples shown, you can customize the product name, add additional information or even change the style of the display.
Use this hook to further customize your WooCommerce store and offer your customers a personalized shopping experience.
Good luck with the implementation!
Artikel von:
Marc Wagner
Hi Marc here. I’m the founder of Forge12 Interactive and have been passionate about building websites, online stores, applications and SaaS solutions for businesses for over 20 years. Before founding the company, I already worked in publicly listed companies and acquired all kinds of knowledge. Now I want to pass this knowledge on to my customers.