Yunie992
Yunie992

Reputation: 21

Show product meta in order items table on WooCommerce admin order details page

I am using the following code to show custom product meta in the order items table on the WooCommerce admin order details page:

add_action('woocommerce_admin_order_item_headers', 'tempiconsegna_admin_order_item_headers');
function tempiconsegna_admin_order_item_headers() { 
    $column_name = 'Tempi Consegna'; 
    echo '<th>' . $column_name . '</th>'; 
} 

add_action('woocommerce_admin_order_item_values', 'tempiconsegna_admin_order_item_values', 10, 3);
function tempiconsegna_admin_order_item_values($_product, $item, $item_id = null) {
    $value = get_post_meta($_product->post->ID, 'prefix-tempiconsegna', 1); 
    echo '<td>' . $value . '</td>';
}

It displays "prefix-tempiconsegna" which are custom metas like:


My problem is that if I change the availability in the product, it changes also in previous orders.

How do i make this displaying the value at the moment of the order without changing when I update the availability of the product?

Upvotes: 1

Views: 2731

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29650

Your current code contains 2 errors:

  • Attempt to read property "post" on null
  • Attempt to read property "ID" on null

To answer your question: that's because you're using get_post_meta() and the productID, so if you adjust the data for the product it will also change the data where it is displayed, in your case the current and previous orders.

To prevent this, you have to add the data per order line item, this can be done via:

function action_woocommerce_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
    // The WC_Product instance Object
    $product = $item->get_product();
    
    // Get value
    $value = $product->get_meta( 'prefix-tempiconsegna' );
    
    // NOT empty
    if ( ! empty ( $value ) ) {
        $item->update_meta_data( 'prefix-tempiconsegna', $value );
    }
}
add_action( 'woocommerce_checkout_create_order_line_item', 'action_woocommerce_checkout_create_order_line_item', 10, 4 );

Then to display this in WooCommerce admin order details page, use:

// Add header
function action_woocommerce_admin_order_item_headers( $order ) {
    // Set the column name
    $column_name = __( 'Tempi Consegna', 'woocommerce' );
    
    // Display the column name
    echo '<th class="my-class">' . $column_name . '</th>';
}
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 1 );

//Add content
function action_woocommerce_admin_order_item_values( $product, $item, $item_id ) {
    // Only for "line_item" items type, to avoid errors
    if ( ! $item->is_type('line_item') ) return;

    // Get value
    $value = $item->get_meta( 'prefix-tempiconsegna' );
    
    // NOT empty
    if ( ! empty ( $value ) ) {
        echo '<td>' . $value . '</td>';
    } else {
        echo '<td>Meta not found!</td>';        
    }
}
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );

Upvotes: 2

Related Questions