Display product attributes for variations on cart page in woocommerce 3

荒凉一梦 提交于 2019-12-24 10:46:07

问题


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

  • It's about a Variable Product with variations.
  • Attributes are included for variations.
  • There are no additional attributes (not set for variations).

回答1:


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.



来源:https://stackoverflow.com/questions/47732882/display-product-attributes-for-variations-on-cart-page-in-woocommerce-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!