Woocommerce在购物车页面上获取特定的属性值 [英] Woocommerce get specific attribute value on cart page

查看:60
本文介绍了Woocommerce在购物车页面上获取特定的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在woocommerce购物车页面中获取特定的属性值. 我在表格中做了类似的自定义列.

I'm trying to get specific attribute value in the woocommerce cart page. I made custom columns in the table like those.

<td class="product-color" data-title="<?php esc_attr_e( 'Color', 'woocommerce' ); ?>">
                        <?php
                            echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
                        ?>
                    </td>

                    <td class="product-size" data-title="<?php esc_attr_e( 'Size', 'woocommerce' ); ?>">
                        <?php
                            echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
                        ?>
                    </td>

                    <td class="product-price" data-title="<?php esc_attr_e( 'Price', 'woocommerce' ); ?>">
                        <?php
                            echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
                        ?>
                    </td>

当您看到所有这些都获得了产品价格时.但我正在尝试获取颜色和大小的属性. 我搜索并找到了此内容:

AS you see all of them get the product price. but I'm trying to get attribute of color and size. I searched and found this :

foreach( wc_get_product_terms( $product->id, 'pa_size' ) as $attribute_value ){
// Outputting the attibute values one by one
echo $attribute_value . '<br>';

}

我这样尝试过:

I tried it like that :

<?php

                            echo apply_filters( 'woocommerce_cart_item_color', WC()->$product->get_attributes( $_product ),
                            $cart_item, $cart_item_key );
                        ?>

但是没用

我的最新尝试:

my latest try:

$test = $_product->get_attributes();
foreach($test['pa_size']['options'] as $size){
                            if ($size !== NULL) {
                        echo apply_filters( 'woocommerce_cart_item_price',  $size , $cart_item, $cart_item_key );
                                var_dump($size);

                        }   else {
                                echo "Not Specified";
                            }
                        }

我得到的结果是int(48)int(47)

and I got this result int(48)int(47)

任何人都可以帮我吗.

推荐答案

如何在购物车页面中获取属性和值?

请在function.php文件中添加以下功能

please add below function in function.php file

<?php
/**
* WooCommerce: show all product attributes listed below each item on Cart page 
* ------
*/    

function wp_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 ) {

        // skip variations
        if ( $attribute->get_variation() ) {
            continue;
        }
        $name = $attribute->get_name();
        if ( $attribute->is_taxonomy() ) {

            $product_id = $item_data->get_id();
            $terms = wp_get_post_terms( $product_id, $name, 'all' );

            if ( ! empty( $terms ) ) {
                if ( ! is_wp_error( $terms ) ) {

                    // get the taxonomy
                    $tax = $terms[0]->taxonomy;

                    // get the tax object
                    $tax_object = get_taxonomy($tax);

                    // get tax label
                    if ( isset ( $tax_object->labels->singular_name ) ) {
                        $tax_label = $tax_object->labels->singular_name;
                    } elseif ( isset( $tax_object->label ) ) {
                        $tax_label = $tax_object->label;
                        // Trim label prefix since WC 3.0
                        $label_prefix = 'Product ';
                        if ( 0 === strpos( $tax_label,  $label_prefix ) ) {
                            $tax_label = substr( $tax_label, strlen( $label_prefix ) );
                        }
                    }
                    $out .= $tax_label . ': ';
                    $tax_terms = array();
                    foreach ( $terms as $term ) {
                        $single_term = esc_html( $term->name );
                        array_push( $tax_terms, $single_term );
                    }
                    $out .= implode(', ', $tax_terms). '<br />';

                }
            }

        } else {

            // not a taxonomy 

            $out .= $name . ': ';
            $out .= esc_html( implode( ', ', $attribute->get_options() ) ) . '<br />';
        }
    }
    echo $out;
}

add_filter( 'woocommerce_cart_item_name', 'wp_woo_cart_attributes', 10, 2 );
?>

这篇关于Woocommerce在购物车页面上获取特定的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆