在购物车woocomerce中显示属性值和名称

时间:2017-06-01 10:45:48

标签: php wordpress woocommerce

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

2 个答案:

答案 0 :(得分:3)

只需做一个简单的事情,如下所示,你将获得cart_item中的所有内容 -

{{1}}

}

就这么简单。谢谢。

答案 1 :(得分:0)

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

OR

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