Output a product custom field in WooCommerce Order

2020-03-07 06:40发布

问题:

With WooCommerce, I'm using the Advanced Custom Fields plugin to keep a track of where I physically store each product.

When an order comes in, I want to be able to look at the admin edit order page and see where the items are stored. So I can grab it to ship.

Here's a picture of what I want to see:

Hopefully, it makes sense.

I just want to recall the individual product ACF variable (BinLocation) for each product on the Edit Order Admin Page for Wordpress. Any help on this?

Thanks.

回答1:

Updated: Using a custom function hoocked in woocommerce_before_order_itemmeta action hook, you will be able to achieve what you are looking for:

add_action( 'woocommerce_before_order_itemmeta', 'storage_location_of_order_items', 10, 3 );
function storage_location_of_order_items( $item_id, $item, $product ){
    // Only on backend order edit pages
    if( ! ( is_admin() && $item->is_type('line_item') ) ) return;

    // Get your ACF product value (replace the slug by yours below)
    if( $acf_value = get_field( 'BinLocation', $product->get_id() ) ) {
        $acf_label = __('Stored in: ');

        // Outputing the value of the "location storage" for this product item
        echo '<div class="wc-order-item-custom"><strong>' . $acf_value .  $acf_label . '</strong></div>';
    }
}

Code goes in any php file of your active child theme (or theme) or also in any plugin php file.

This code is tested and works in WooCommerce version 3 and up…