[英]Get product custom field related to Woocommerce order items
I am trying to add data from custom field in front of the subtotal on the checkout/order, but the get_post_meta
is not display.我试图在结帐/订单的小计前面添加自定义字段中的数据,但未显示get_post_meta
。 I have tried $product->get_ID()
, $post_id
and get_the_ID()
.我试过$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;
}
There is some missing arguments and some mistakes in your code… Try the following instead:您的代码中缺少一些参数和一些错误……请尝试以下操作:
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;
}
Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.