questions

Add product data to WooCommerce admin order preview

Rate this post

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

Help me figure it out, I don’t understand why this is happening.

 

✔️Solution:

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). Tested and works.

Leave a Reply

Your email address will not be published.

Back to top button