Search code examples
phpwordpresswoocommerceproductcart

Append text to product title if product has product-tag on cart in WooCommerce


I'm trying to append text to WooCommerce product title in the cart if products has a specific tag.

This is what i have. I mis the conditional code.

add_filter( 'woocommerce_cart_item_name', 'add_udstilling_below_cart_item_name', 10, 3);
function add_udstilling_below_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
    echo $item_name . ' (test)';
}

I tried something like this:

if ( has_term( 'udstillingsmodel', 'product_tag' ) ) {

}

But i need help getting it to work with products in cart.


Solution

  • function add_udstilling_below_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
        $product_id = $cart_item['product_id'];
    
        /* Uncomment for debug purposes
        $terms = wp_get_post_terms( $product_id, 'product_tag' );
        echo '<pre>', print_r($terms, 1), '</pre>';
        */
    
        if ( has_term( 'udstillingsmodel', 'product_tag', $product_id ) ) {
            $item_name = $item_name . ' (test)';
        }
    
        return $item_name;
    }
    add_filter( 'woocommerce_cart_item_name', 'add_udstilling_below_cart_item_name', 10, 3 );