phpwordpresswoocommercecarthook-woocommerce

Change the cart item name for products in WooCommerce


I Would like to change the product variation text that appears in the cart once the product is added to cart. I suspect I'm suppose to use the woocommerce_get_item_data filter.

But I'm unsure how the code inside the function is suppose to be.

Any help on this will be appreciated.


Solution

  • You will need to use a custom function hooked in woocommerce_cart_item_name filter hook.

    1. For product variations:
    add_filter( 'woocommerce_cart_item_name', 'custom_variation_item_name', 10, 3 );
    function custom_variation_item_name( $item_name,  $cart_item,  $cart_item_key ){
        // Change item name only if is a product variation
        if( $cart_item['data']->is_type('variation') ){
            // HERE customize item name
            $item_name = esc_html__('my custom item name', 'woocommerce');
    
            // For cart page we add back the product link
            if ( is_cart() ) {
                $item_name = sprintf( '<a href="%s">%s</a>', esc_url( $cart_item['data']->get_permalink() ), $item_name );
            }
        }
        return $item_name;
    }
    
    1. For All products:
    add_filter( 'woocommerce_cart_item_name', 'custom_cart_item_name', 10, 3 );
    function custom_cart_item_name( $item_name,  $cart_item,  $cart_item_key ){
        // HERE customize the cart item name
        $item_name = esc_html__('my custom item name', 'woocommerce');
    
        // For cart page we add back the product link
        if ( is_cart() ) {
            $item_name = sprintf( '<a href="%s">%s</a>', esc_url( $cart_item['data']->get_permalink() ), $item_name );
        }
        return $item_name;
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Note that this doesn't work for cart and checkout Blocks.