【问题标题】:Display product attributes for variations on cart page in woocommerce 3在 woocommerce 3 中显示购物车页面变体的产品属性
【发布时间】:2018-01-19 07:01:14
【问题描述】:

如何在购物车页面上显示产品类别及其变体?

  • 这是关于具有变化的可变产品。
  • 包含变体的属性。
  • 没有其他属性(未针对变体设置)。

【问题讨论】:

    标签: php wordpress woocommerce attributes cart


    【解决方案1】:

    显示变体的产品属性有两种方式:

    请参阅此相关答案:Product variations attributes as cart items shows up differently in WooCommerce

    要显示产品类别,您可以使用这个钩子函数:

    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;
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    经过测试并且有效。

    【讨论】:

      猜你喜欢
      • 2020-12-13
      • 1970-01-01
      • 2022-08-17
      • 2017-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      相关资源
      最近更新 更多