[英]Add product data to WooCommerce admin order preview
I used this code for displaying the product attributes in the order details/editor我使用此代码在订单详细信息/编辑器中显示产品属性
add_action( 'woocommerce_admin_order_item_headers', 'custom_admin_order_items_headers', 20, 1 );
function custom_admin_order_items_headers( $order ){
echo '<th>';
echo __('Location', 'woocommerce') . '</th>';
}
add_action('woocommerce_admin_order_item_values', 'my_woocommerce_admin_order_item_values', 10, 3);
function my_woocommerce_admin_order_item_values($product, $item, $item_id = null) {
echo '<td>' . get_the_term_list( $product->get_id(), 'pa_location', '', ',', '' ) . '</td>';
}
And it seems to work, but there is an error in the admin panel:它似乎有效,但管理面板中出现错误:
Fatal error: Uncaught Error: Call to a member function get_id() on null致命错误:未捕获的错误:在 null 上调用成员函数 get_id()
Help me figure it out, I don't understand why this is happening.帮我弄清楚,我不明白为什么会这样。
To avoid your issue, you need to target only order "line" items on your 2nd function, this way:为避免您的问题,您只需在第二个函数中定位订单“行”项目,如下所示:
add_action('woocommerce_admin_order_item_values', 'my_woocommerce_admin_order_item_values', 10, 3);
function my_woocommerce_admin_order_item_values($product, $item, $item_id = null) {
// Only for "line_item" items type, to avoid errors
if( ! $item->is_type('line_item') ) return;
echo '<td>' . get_the_term_list( $product->get_id(), 'pa_location', '', ',', '' ) . '</td>';
}
Code goes in functions.php file of the active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.
测试和工作。
Related: Add product short description to Woocommerce admin orders preview相关: 将产品简短描述添加到 Woocommerce 管理订单预览
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.