获取与 Woocommerce 订单项相关的产品自定义字段
Get product custom field related to Woocommerce order items
我正在尝试从 checkout/order 小计前的自定义字段添加数据,但 get_post_meta
未显示。我试过 $product->get_ID()
、$post_id
和 get_the_ID()
.
add_filter( 'woocommerce_order_formatted_line_subtotal', 'custom_field_test');
function bmc_test($subtotal){
global $woocommerce;
global $item_id;
//echo $values['price_currency'];
//just tried to see if it I could get display
wc_get_order_item_meta($item);
$custom_field = get_post_meta( $values['product_id'], 'custom_field', true );
return $custom_field . ' '. $subtotal;
}
您的代码中缺少一些参数和一些错误……请尝试以下操作:
add_filter( 'woocommerce_order_formatted_line_subtotal', 'custom_field_test', 10, 3 );
function custom_field_test( $subtotal, $item, $order ){
$product = $item->get_product(); // The instance of the WC_Product Object
if( $custom_field = get_post_meta( $product->get_id(), 'custom_field', true ) ) {
$subtotal = $custom_field . ' '. $subtotal;
}
return $subtotal;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
我正在尝试从 checkout/order 小计前的自定义字段添加数据,但 get_post_meta
未显示。我试过 $product->get_ID()
、$post_id
和 get_the_ID()
.
add_filter( 'woocommerce_order_formatted_line_subtotal', 'custom_field_test');
function bmc_test($subtotal){
global $woocommerce;
global $item_id;
//echo $values['price_currency'];
//just tried to see if it I could get display
wc_get_order_item_meta($item);
$custom_field = get_post_meta( $values['product_id'], 'custom_field', true );
return $custom_field . ' '. $subtotal;
}
您的代码中缺少一些参数和一些错误……请尝试以下操作:
add_filter( 'woocommerce_order_formatted_line_subtotal', 'custom_field_test', 10, 3 );
function custom_field_test( $subtotal, $item, $order ){
$product = $item->get_product(); // The instance of the WC_Product Object
if( $custom_field = get_post_meta( $product->get_id(), 'custom_field', true ) ) {
$subtotal = $custom_field . ' '. $subtotal;
}
return $subtotal;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。