user3273784
user3273784

Reputation: 161

Add the product category to Cross sells product title in Woocommerce

I want to add the Category Name to the end of Cross sell items that appear while browsing shopping cart. But, I can't figure out the hook for this, so not sure how to approach with a function.

Is there a hook for Cross sell items? I've changed the names of products in the cart no issue with the woocommerce_cart_item_name hook, so hoping to do the same with a function for Cross sell items.

Is it doable? Any track on this is useful.

Upvotes: 0

Views: 2070

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253901

Note: Upsells are in single product pages. In cart page they are Cross-sells.

You can simply use the following:

remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title', 10 );
add_action( 'woocommerce_shop_loop_item_title', 'cross_sells_loop_product_title', 10 );
function cross_sells_loop_product_title() {
    $title = get_the_title();

    if( is_cart() ) {
        $product_categories = wp_get_post_terms( get_the_id(), 'product_cat', ['fields' => 'names'] );
        $title .=  ' - ' . reset( $product_categories );
    }
    echo '<h2 class="woocommerce-loop-product__title">' . $title . '</h2>';
}

Code goes in function.php file of your active child theme (or active theme). Tested and work.

enter image description here

Upvotes: 1

Related Questions