maciek8
maciek8

Reputation: 35

Display product attributes for variations on cart page in woocommerce 3

How do I display a product category and it's variations on the Cart page?

Upvotes: 1

Views: 9265

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253804

To display the product attributes for variations there is 2 ways:

See this related answer for that: Product variations attributes as cart items shows up differently in WooCommerce

To display the product category you could use this hooked function:

add_filter( 'woocommerce_cart_item_name', 'customizing_cart_item_data', 10, 3);
function customizing_cart_item_data( $item_name, $cart_item, $cart_item_key ) {
    $term_names = array();

    // Get product categories
    $terms = wp_get_post_terms( $cart_item['product_id'], 'product_cat' );

    if( count($terms) > 0 ){
        foreach( $terms as $term ) $term_names[] = $term->name;

        $item_name .= '<p class="item-category" style="margin:12px 0 0; font-size: .875em;">
            <strong class="label">' . _n( 'Category', 'Categories', count($terms), 'woocommerce' ) . ': </strong>
            <span class="values">' . implode( ', ', $term_names ) . '</span>
        </p>';
    }
    return $item_name;
}

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

Tested and works.

Upvotes: 2

Related Questions