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

送分小仙女□ 提交于 2019-12-19 04:43:11

问题


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…




来源:https://stackoverflow.com/questions/45001375/output-a-product-custom-field-in-woocommerce-order-admin-page-acf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!