WooCommerce Hook: woocommerce_cart_item_name

Marc Wagner, September 11, 2024

The­re are a varie­ty of hooks in Woo­Com­mer­ce that allow deve­lo­pers to cus­to­mi­ze the beha­vi­or of Woo­Com­mer­ce wit­hout chan­ging the core code. One of the most useful hooks is woocommerce_cart_item_name, which is used to chan­ge or cus­to­mi­ze the name of a pro­duct in the shop­ping cart.

In this blog post, I’ll show you how to use the Woo­Com­mer­ce Hook woocommerce_cart_item_name and give you some prac­ti­cal PHP examp­les.

What is the woocommerce_cart_item_name Hook? #

The woocommerce_cart_item_name hook allows you to chan­ge the name of a pro­duct in the shop­ping cart view. This can be useful if you want to add addi­tio­nal infor­ma­ti­on such as pro­duct vari­ants, cus­tom fields or even HTML ele­ments to impro­ve the dis­play for the user.

Hook syntax #

The basic syn­tax of the hook is as fol­lows:

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: #

  1. $product_name (string): The cur­rent pro­duct name.
  2. $cart_item (array): Infor­ma­ti­on about the pro­duct in the shop­ping cart, e.g. quan­ti­ty, ID, etc.
  3. $cart_item_key (string): A uni­que key for the pro­duct in the shop­ping cart.

Example 1: Extending product names with additional text #

In the first exam­p­le, we add a user-defi­ned text to the pro­duct name. This is useful if, for exam­p­le, you want to add a note such as “Spe­cial offer” to the pro­duct 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 exam­p­le, the text “Spe­cial offer!” is added to each pro­duct name in the shop­ping cart, for­mat­ted with a red text. This draws more atten­ti­on to spe­cial pro­ducts.

Example 2: Customize product name based on category #

In the next exam­p­le, I adjust the pro­duct name based on the cate­go­ry of the pro­duct. Let’s say you only want to chan­ge the name for pro­ducts in the “Sale” cate­go­ry.

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 exam­p­le, the pro­duct name is only adjus­ted for pro­ducts in the “Sale” cate­go­ry. If the pro­duct is in this cate­go­ry, a note “On sale!” is added.

Example 3: Adding user-defined fields to the product name #

Ano­ther useful exam­p­le is adding cus­tom fields to the pro­duct name. Let’s say you have a cus­tom field cal­led “Pro­duct code” that you want to dis­play in the shop­ping 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 cus­tom field “_product_code” is retrie­ved and added to the pro­duct name if it con­ta­ins a value. This can be useful for spe­cia­li­zed stores whe­re addi­tio­nal infor­ma­ti­on about the pro­duct should be visi­ble direct­ly in the shop­ping cart.

Conclusion #

The woocommerce_cart_item_name hook is extre­me­ly fle­xi­ble and can be used to impro­ve the user expe­ri­ence in the shop­ping cart. With the PHP examp­les shown, you can cus­to­mi­ze the pro­duct name, add addi­tio­nal infor­ma­ti­on or even chan­ge the style of the dis­play.

Use this hook to fur­ther cus­to­mi­ze your Woo­Com­mer­ce store and offer your cus­to­mers a per­so­na­li­zed shop­ping expe­ri­ence.

Good luck with the imple­men­ta­ti­on!

Avatar of Marc Wagner
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.

Similar Topics

Comments

Leave A Comment