简体   繁体   English

将产品数据添加到 WooCommerce 管理订单预览

[英]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.

相关问题 在Woocommerce管理员订单预览上显示自定义数据 - Display custom data on Woocommerce admin order preview 将产品简短描述添加到 Woocommerce 管理订单预览 - Add product short description to Woocommerce admin orders preview 在 WooCommerce 管理手动订单上添加产品重量和尺寸作为订单项元数据 - Add product weight and dimensions as order item meta data on WooCommerce admin manual orders Woocommerce / Wordpress - 在管理面板上添加按钮到订单/产品页面 - Woocommerce / Wordpress - Add button to Order / Product Page on Admin Panel 在woocommerce管理订单页面中添加产品属性或自定义字段 - Add product attribute or custom field in woocommerce admin order page WooCommerce - 如何在管理订单页面中向产品列表添加列 - WooCommerce - How to add column to product list in admin order page 在 WooCommerce 管理快速订单预览中自定义内容 - Customize content in WooCommerce admin quick order preview 从订单预览中删除 WooCommerce 管理订单操作 - Remove WooCommerce admin order action from order preview WooCommerce - 将列添加到Product Cat管理表 - WooCommerce - add column to Product Cat admin table 在 WooCommerce 快速订单预览 window 的新列中显示产品缩略图 - Display product thumbnail in new column in WooCommerce quick order preview window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM