在 Woocommerce 购物车和结帐项目中显示自定义字段的值
Display Custom Field's Value in Woocommerce Cart & Checkout items
我已经在互联网上寻找解决方案一段时间了,但找不到任何合适的解决方案。我在我的产品页面中使用了多个自定义字段,例如 'Minimum-Cooking-Time'、'Food-Availability' 等。因此,我喜欢在我的购物车和结帐页面中显示此自定义字段的值。
我尝试了功能文件中的片段并也编辑了 woocommerce 购物车文件。我尝试了几种代码,但它们没有从我的自定义字段中提取任何数据。
正如您在下面的屏幕截图中看到的,我想在每个产品的黑色矩形区域中显示 'Minimum-Cooking-Time':
我使用了以下代码:
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' 但旁边没有显示任何值。
如有任何帮助,我们将不胜感激。
谢谢。
您的问题出在 get_post_meta()
函数中,最后一个参数设置为 true
,因此您得到一个自定义字段值 as一个字符串。
然后你在 array_map()
PHP 函数之后使用 期望数组 但 NOT字符串值.
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.
Also you can set the $product_id
that you are using in get_post_meta()
function as first argument, in a much simple 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 文件或任何插件文件。
此代码功能齐全并经过测试。
我已经在互联网上寻找解决方案一段时间了,但找不到任何合适的解决方案。我在我的产品页面中使用了多个自定义字段,例如 'Minimum-Cooking-Time'、'Food-Availability' 等。因此,我喜欢在我的购物车和结帐页面中显示此自定义字段的值。
我尝试了功能文件中的片段并也编辑了 woocommerce 购物车文件。我尝试了几种代码,但它们没有从我的自定义字段中提取任何数据。
正如您在下面的屏幕截图中看到的,我想在每个产品的黑色矩形区域中显示 'Minimum-Cooking-Time':
我使用了以下代码:
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' 但旁边没有显示任何值。
如有任何帮助,我们将不胜感激。
谢谢。
您的问题出在 get_post_meta()
函数中,最后一个参数设置为 true
,因此您得到一个自定义字段值 as一个字符串。
然后你在 array_map()
PHP 函数之后使用 期望数组 但 NOT字符串值.
I think you don't need to use
array_map()
function asget_post_meta()
function with last argument set totrue
will output a string and not an unserialized array.Also you can set the
$product_id
that you are using inget_post_meta()
function as first argument, in a much simple 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 文件或任何插件文件。
此代码功能齐全并经过测试。