在 Woocommerce 结帐页面上添加其他产品信息
Add additional product information on Woocommerce checkout page
在 WooCommerce 中,我有一个自定义产品字段 "Time",我想在结帐时输出该字段的值,特别是在产品详细信息中的产品名称下,让它像这样:Event time: (value from wcv_custom_product_field)
我试过放置:
add_filter( 'woocommerce_get_item_data', 'wc_checkout_producttime', 10, 2 );
function wc_checkout_producttime( $other_data, $cart_item )
{
$_product = $cart_item['data'];
$other_data[] = array( 'name' => 'wcv_custom_product_field', 'value' => $_product->get_wcv_custom_product_field() );
return $other_data;
}
但是我在结账时看到的是空白页。
我做错了什么,我该如何解决这个问题?
谢谢。
这是一个挂钩在 woocommerce_get_item_data
过滤器挂钩中的自定义函数,它将在购物车和结帐项目中显示您的产品自定义字段:
add_filter( 'woocommerce_get_item_data', 'display_custom_product_field_data', 10, 2 );
function display_custom_product_field_data( $cart_data, $cart_item ) {
// Define HERE your product custom field meta key <== <== <== <== <==
$meta_key = 'wcv_custom_product_field';
$product_id = $cart_item['product_id'];
$meta_value = get_post_meta( $product_id, $meta_key, true );
if( !empty( $cart_data ) )
$custom_items = $cart_data;
if( !empty($meta_value) ) {
$custom_items[] = array(
'key' => __('Event time', 'woocommerce'),
'value' => $meta_value,
'display' => $meta_value,
);
}
return $custom_items;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且有效。
在 WooCommerce 中,我有一个自定义产品字段 "Time",我想在结帐时输出该字段的值,特别是在产品详细信息中的产品名称下,让它像这样:Event time: (value from wcv_custom_product_field)
我试过放置:
add_filter( 'woocommerce_get_item_data', 'wc_checkout_producttime', 10, 2 );
function wc_checkout_producttime( $other_data, $cart_item )
{
$_product = $cart_item['data'];
$other_data[] = array( 'name' => 'wcv_custom_product_field', 'value' => $_product->get_wcv_custom_product_field() );
return $other_data;
}
但是我在结账时看到的是空白页。
我做错了什么,我该如何解决这个问题?
谢谢。
这是一个挂钩在 woocommerce_get_item_data
过滤器挂钩中的自定义函数,它将在购物车和结帐项目中显示您的产品自定义字段:
add_filter( 'woocommerce_get_item_data', 'display_custom_product_field_data', 10, 2 );
function display_custom_product_field_data( $cart_data, $cart_item ) {
// Define HERE your product custom field meta key <== <== <== <== <==
$meta_key = 'wcv_custom_product_field';
$product_id = $cart_item['product_id'];
$meta_value = get_post_meta( $product_id, $meta_key, true );
if( !empty( $cart_data ) )
$custom_items = $cart_data;
if( !empty($meta_value) ) {
$custom_items[] = array(
'key' => __('Event time', 'woocommerce'),
'value' => $meta_value,
'display' => $meta_value,
);
}
return $custom_items;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且有效。