在Woocommerce购物车中显示自定义字段的值结帐项目 [英] Display Custom Field's Value in Woocommerce Cart & Checkout items

查看:62
本文介绍了在Woocommerce购物车中显示自定义字段的值结帐项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在寻找一种解决方案,但是找不到合适的解决方案。我在产品页面中使用了几个自定义字段,例如最短烹饪时间,食物可获取性等。因此,我想在购物车和结帐页面中显示此自定义字段的值。

I have been looking for a solution for a while all over internet but couldn't find any proper solution. I am using several custom fields in my product page like, 'Minimum-Cooking-Time', 'Food-Availability' etc. So, I like to show this custom field's value in my cart and checkout page.

我尝试了功能文件中的代码片段,还编辑了woocommerce购物车文件。我尝试了一些代码,但是它们没有从我的自定义字段中提取任何数据。

I tried snippets in function file and editing woocommerce cart file too. I have tried several codes but they are not pulling any data from my custom fields.

如下面的屏幕快照所示,我想在每个产品的黑色矩形区域中显示最小烹饪时间:

As you can see in the screenshot below, I want to show 'Minimum-Cooking-Time' in that black rectangular area for every product:

我使用了以下代码:

add_filter( 'woocommerce_get_item_data', 'wc_add_cooking_to_cart', 10, 2 ); 
function wc_add_cooking_to_cart( $other_data, $cart_item ) { 
    $post_data = get_post( $cart_item['product_id'] );  

    echo '<br>';
    $Add = 'Cook Time: ';
    echo $test;
    $GetCookTime = get_post_meta( $post->ID, 'minimum-cooking-time', true );

    $GetCookTime = array_filter( array_map( function( $a ) {return $a[0];}, $GetCookTime ) );

    echo $Add;
    print_r( $GetCookTime );

    return $other_data; 

}

但这显示标签 Cook Time,但不显示

But, this shows the label 'Cook Time' but not showing any value beside it.

任何帮助将不胜感激。

谢谢。

推荐答案

您的问题出在 get_post_meta() 函数,最后一个参数设置为 true ,因此您将获得自定义字段值作为字符串

然后您正在使用 array_map() PHP函数,它正在期望数组 >,但不是字符串值

Your problem is in get_post_meta() function which last argument is set to true, so you get a custom field value as a string.
Then you are using just after the array_map() PHP function that is expecting an array but NOT a string value.


我认为您不需要使用 array_map() 用作 get_post_meta() 函数的最后一个参数设置为 true 一个字符串而不是一个非序列化的数组。

I think you don't need to use array_map() function as get_post_meta() function with last argument set to true will output a string and not an unserialized array.

还可以设置您正在使用的 $ product_id get_post_meta()

Also you can set the $product_id that you are using in get_post_meta() function as first argument, in a much simple way.

因此,您的代码应以这种方式工作:

So your code should work, this way:

// Render the custom product field in cart and checkout
add_filter( 'woocommerce_get_item_data', 'wc_add_cooking_to_cart', 10, 2 );
function wc_add_cooking_to_cart( $cart_data, $cart_item ) 
{
    $custom_items = array();

    if( !empty( $cart_data ) )
        $custom_items = $cart_data;

    // Get the product ID
    $product_id = $cart_item['product_id'];

    if( $custom_field_value = get_post_meta( $product_id, 'minimum-cooking-time', true ) )
        $custom_items[] = array(
            'name'      => __( 'Cook Time', 'woocommerce' ),
            'value'     => $custom_field_value,
            'display'   => $custom_field_value,
        );

    return $custom_items;
}

代码位于您的活动子主题(或主题)的function.php文件中,或者

此代码具有完整的功能并经过测试。

这篇关于在Woocommerce购物车中显示自定义字段的值结帐项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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