在WooCommerce Cart中响应特定的产品属性和元数据

时间:2022-10-25 10:15:16

Update (related to author comments):

更新(与作者评论相关):

I would like to customize WooCommerce cart.php to display some meta-data that are working just fine on the product page using Essential Grid premium plugin.

我想定制WooCommerce cart。php显示一些元数据,使用基本的网格高级插件在产品页面上运行良好。

I would like to display some product attributes fields and also some custom meta fields that I have created with the meta field creator of Essential Grid plugin.

我想显示一些产品属性字段和一些自定义元字段,这些字段是我与基本网格插件的元字段创建者创建的。

For testing, I'm using 'Height' attribute (so 'pa_height') and the custom field 'Age' which slug is 'eg-age-cal'.

对于测试,我使用'Height'属性(即'pa_height')和自定义字段'Age',其中slug是'eg-age-cal'。

Currently, I have tried using the following:

目前,我尝试使用以下方法:

<?php echo get_post_meta($product_id, 'pa_height', true );?>

And also:

还有:

<?php echo get_post_meta($product_id, 'eg-age-cal', true );?>

But these does not seem to work.

但这些似乎并不奏效。

I have managed to get the code to work using:

我已经设法使代码工作使用:

<?php echo get_post_meta($product_id, '_regular_price', true );?>

So I know the code is working.

所以我知道代码在起作用。

I just need help working out, how can I get the values from these Custom Attribute and Custom Field.

我只需要帮助计算,如何从这些自定义属性和自定义字段中获取值。

Thanks.

谢谢。

1 个解决方案

#1


4  

Update (compatibility with WC 3+)

更新(与WC 3+兼容)

After your explanations in your comment below, I just discover that you are using Essential Grid premium plugin (a commercial plugin) to create some custom fields and attributes related to your wooCommerce products.

在你的评论后面的解释之后,我发现你正在使用基本的Grid premium插件(一个商业插件)来创建一些与你的wooCommerce产品相关的自定义字段和属性。

At this point, I can't help, because I have never used this plugin before, and I don't know where data is stored within this plugin, in the database.

此时,我无能为力,因为我以前从未使用过这个插件,我也不知道这个插件的数据存储在什么地方,在数据库中。

I think that you can't use usuals WordPress/WooCommerce functions to get this data, and that is the reason that you will not get any data using get_post_meta() as usual…

我认为您不能使用WordPress/WooCommerce常规函数来获取这些数据,这就是为什么您不能像往常一样使用get_post_meta()获取任何数据的原因……

The best way to get helped is:
- to search/explore your database for that custom fields data.
- to search/ask in Essential Grid plugin authors support threads.

获得帮助的最好方法是:-搜索/探索您的数据库以获得自定义字段数据。-在必要的网格插件中搜索/询问作者支持线程。


My original answer:

我最初的回答:

For attributes defined in your products, using get_post_meta() function with the $product_id variable, you need to use it this way to get the data you want (this is an array of values):

对于在产品中定义的属性,使用带有$product_id变量的get_post_meta()函数,需要使用它来获取所需的数据(这是一个值数组):

// getting the defined product attributes
$product_attr = get_post_meta( $product_id, '_product_attributes' );

// displaying the array of values (just to test and to see output)
echo var_dump( $product_attr );

You can also use the function get_attributes() (more recommended), this way:

还可以使用函数get_attributes()(更推荐),方法如下:

// Creating an object instance of the product
$_product = new WC_Product( $product_id );

// getting the defined product attributes
$product_attr = $_product->get_attributes();

// displaying the array of values (just to test and to see output)
echo var_dump( $product_attr );

All code is tested and working.

所有代码都经过测试并工作。

NOW CART DATA IS SET IN COOKIES AND SESSIONS and you will need to use WC()->cart syntax to get cart data and items

现在CART数据是在cookie和会话中设置的,您需要使用WC()-> CART语法来获取CART数据和项目

So you can use this kind of code to get the items (products) in cart:

所以您可以使用这种代码来获取购物车中的物品(产品):

foreach ( WC()->cart->get_cart() as $cart_item ) {
    $product = $cart_item['data'];
    if(!empty($product)){

        // getting the defined product attributes
        $product_attr = $_product->get_attributes();

        // displaying the attributes array of values (just to test and to see output)
        echo var_dump( $product_attr ) . '<br>';
    }
}

This will display the attributes array of values for each product in CART.

这将显示CART中每个产品的值的属性数组。


A solution based on this thread, using wc_get_product_terms() inside the same code snippet to get your attribute:

基于此线程的解决方案,使用同一个代码片段中的wc_get_product_terms()获取属性:

foreach ( WC()->cart->get_cart() as $cart_item ) {
    $product = $cart_item['data'];
    if(!empty($product)){

        // compatibility with WC +3
        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

        // Getting "height" product attribute
        $myAttribute = array_shift( wc_get_product_terms( $product_id, 'pa_height', array( 'fields' => 'names' ) ) );
        echo $myAttribute . '<br>';
    }
}

References:

引用:

#1


4  

Update (compatibility with WC 3+)

更新(与WC 3+兼容)

After your explanations in your comment below, I just discover that you are using Essential Grid premium plugin (a commercial plugin) to create some custom fields and attributes related to your wooCommerce products.

在你的评论后面的解释之后,我发现你正在使用基本的Grid premium插件(一个商业插件)来创建一些与你的wooCommerce产品相关的自定义字段和属性。

At this point, I can't help, because I have never used this plugin before, and I don't know where data is stored within this plugin, in the database.

此时,我无能为力,因为我以前从未使用过这个插件,我也不知道这个插件的数据存储在什么地方,在数据库中。

I think that you can't use usuals WordPress/WooCommerce functions to get this data, and that is the reason that you will not get any data using get_post_meta() as usual…

我认为您不能使用WordPress/WooCommerce常规函数来获取这些数据,这就是为什么您不能像往常一样使用get_post_meta()获取任何数据的原因……

The best way to get helped is:
- to search/explore your database for that custom fields data.
- to search/ask in Essential Grid plugin authors support threads.

获得帮助的最好方法是:-搜索/探索您的数据库以获得自定义字段数据。-在必要的网格插件中搜索/询问作者支持线程。


My original answer:

我最初的回答:

For attributes defined in your products, using get_post_meta() function with the $product_id variable, you need to use it this way to get the data you want (this is an array of values):

对于在产品中定义的属性,使用带有$product_id变量的get_post_meta()函数,需要使用它来获取所需的数据(这是一个值数组):

// getting the defined product attributes
$product_attr = get_post_meta( $product_id, '_product_attributes' );

// displaying the array of values (just to test and to see output)
echo var_dump( $product_attr );

You can also use the function get_attributes() (more recommended), this way:

还可以使用函数get_attributes()(更推荐),方法如下:

// Creating an object instance of the product
$_product = new WC_Product( $product_id );

// getting the defined product attributes
$product_attr = $_product->get_attributes();

// displaying the array of values (just to test and to see output)
echo var_dump( $product_attr );

All code is tested and working.

所有代码都经过测试并工作。

NOW CART DATA IS SET IN COOKIES AND SESSIONS and you will need to use WC()->cart syntax to get cart data and items

现在CART数据是在cookie和会话中设置的,您需要使用WC()-> CART语法来获取CART数据和项目

So you can use this kind of code to get the items (products) in cart:

所以您可以使用这种代码来获取购物车中的物品(产品):

foreach ( WC()->cart->get_cart() as $cart_item ) {
    $product = $cart_item['data'];
    if(!empty($product)){

        // getting the defined product attributes
        $product_attr = $_product->get_attributes();

        // displaying the attributes array of values (just to test and to see output)
        echo var_dump( $product_attr ) . '<br>';
    }
}

This will display the attributes array of values for each product in CART.

这将显示CART中每个产品的值的属性数组。


A solution based on this thread, using wc_get_product_terms() inside the same code snippet to get your attribute:

基于此线程的解决方案,使用同一个代码片段中的wc_get_product_terms()获取属性:

foreach ( WC()->cart->get_cart() as $cart_item ) {
    $product = $cart_item['data'];
    if(!empty($product)){

        // compatibility with WC +3
        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

        // Getting "height" product attribute
        $myAttribute = array_shift( wc_get_product_terms( $product_id, 'pa_height', array( 'fields' => 'names' ) ) );
        echo $myAttribute . '<br>';
    }
}

References:

引用: