Christian
Christian

Reputation: 13

Output a product custom field in WooCommerce Order Admin Page (ACF)

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:

Order Page Example

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.

Upvotes: 1

Views: 3963

Answers (2)

Henry
Henry

Reputation: 1

The other answer is great for simple products. The following will return the parent's custom field for variations as well. It does not allow for a custom field to be different for each variation though.

  add_action( 'woocommerce_before_order_itemmeta', 'downloadable_items', 100, 3 );
    function downloadable_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 ( $product->is_type( 'simple' ) ) {
    if ( $acf_value = get_field( 'downloadable_image', $product->get_id() ) ) {
        $acf_label = __('Download Link: ');

        // Outputing the value of the "downloadable item" for this product item
        echo '<div class="wc-order-item-custom">'  . $acf_label .'<a href="'. $acf_value 
    .'">'. $acf_value . '</a></div>';
    }} else {
    if ( $acf_value = get_field( 'downloadable_image', $product->get_parent_id() ) ) {
        $acf_label = __('Download Link: ');

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

Hopefully that helps someone else

Upvotes: 0

LoicTheAztec
LoicTheAztec

Reputation: 253784

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…


Upvotes: 1

Related Questions