Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

php - Show product meta in order items table in Order Details

I need to add custom column for order items and show specific product meta in this column. I mean something like image below, I can't find any action from woocommerce to add this column! enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the following code:

// Add custom column headers here
add_action('woocommerce_admin_order_item_headers', 'my_woocommerce_admin_order_item_headers');
function my_woocommerce_admin_order_item_headers() {
    // set the column name
    $column_name = 'Test Column';

    // display the column name
    echo '<th>' . $column_name . '</th>';
}

// Add custom column values here
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) {
    // get the post meta value from the associated product
    $value = get_post_meta($_product->post->ID, '_custom_field_name', 1);

    // display the value
    echo '<td>' . $value . '</td>';
}

I've commented it so it should be clear enough, but in a nutshell this code adds a custom column, named "Test Column", and this column pulls the value from the custom field of the product, called "_custom_field_name".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...