【问题标题】:Display atributes value and name in cart woocomerce在购物车 woocommerce 中显示属性值和名称
【发布时间】:2017-06-01 10:45:48
【问题描述】:

简单的问题如何在购物车woocomerce中显示产品属性:例如颜色:红色,不确定是否有一些代码要添加,如钩子或一些代码到fundctions.php或者可以通过woocomerce设置完成,还没有找到任何有用的在线信息,任何帮助表示赞赏。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    只需做如下简单的事情,你就会把所有东西都放在你的cart_item中-

    add_filter('woocommerce_cart_item_name', 'add_variations_in_cart', 10, 3);
    function add_variations_in_cart($name, $cart_item, $item_key){
        $product_variation = '';
        if(!empty($cart_item['variation_id']) && $cart_item['variation_id'] != 0 ){
           if(is_array($cart_item['variation']) && !empty($cart_item['variation'])){
              foreach ($cart_item['variation'] as $key => $value) {
                 $product_variation .= '<br>'.ucfirst(str_replace('attribute_pa_', '', $key)).' : '.ucfirst($value);
            }
        }
    }
    
    echo $name.$product_variation; 
    

    }

    就这么简单。谢谢。

    【讨论】:

    • 您可以根据需要在 $product_variation 中传递您的 html 结构。根据您的说法,$product_variation .= '
      '.ucfirst(str_replace('attribute_pa_', '', $key)).' : '.ucfirst($value).'';
    【解决方案2】:

    这个插件https://wordpress.org/plugins/woocommerce-show-attributes/

    WooCommerce:显示购物车页面上每个项目下方列出的所有产品属性

    在function.php中添加以下代码

    /**
        * WooCommerce: show all product attributes listed below each item on Cart page
        */
        function sm_woo_cart_attributes($cart_item, $cart_item_key){
    
            $item_data = $cart_item_key['data'];
            $attributes = $item_data->get_attributes();
    
    
            if ( ! $attributes ) {
                return $cart_item;
            }
    
            $out = $cart_item . '<br />';
    
            foreach ( $attributes as $attribute ) {
    
                if ( $attribute['is_taxonomy'] ) {
    
                // skip variations
                    if ( $attribute['is_variation'] ) {
                        continue;
                    }
    
                    // backwards compatibility for attributes which are registered as taxonomies
    
                    $product_id = $item_data->id;
                    $terms = wp_get_post_terms( $product_id, $attribute['name'], 'all' );
    
                    // get the taxonomy
                    $tax = $terms[0]->taxonomy;
    
                    // get the tax object
                    $tax_object = get_taxonomy($tax);
    
                    // get tax label
                    if ( isset ($tax_object->labels->name) ) {
                        $tax_label = $tax_object->labels->name;
                    } elseif ( isset( $tax_object->label ) ) {
                        $tax_label = $tax_object->label;
                    }
    
                    foreach ( $terms as $term ) {
                        $out .= $tax_label . ': ';
                        $out .= $term->name . '<br />';
                    }
    
                } else {
    
                    // not a taxonomy 
    
                    $out .= $attribute['name'] . ': ';
                    $out .= $attribute['value'] . '<br />';
                }
            }
            echo $out;
        }
    
        add_filter( 'woocommerce_cart_item_name', sm_woo_cart_attributes, 10, 2 );
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多