简体   繁体   中英

Change order item custom meta data displayed label and value in WooCommerce Admin orders

In the Woocommerce admin screen, I'm attempting to use the order line meta data to display a button which will open up a new window with the URL of the dropship supplier. I have successfully pulled the supplier URL from the product on order and pushed it to the order line item.

I am able to change the meta data to a button but the consequence of that is the other custom fields which contain the custom options are wiped.

This is the full code which I have added to the functions.php file

add_action( 'woocommerce_checkout_create_order_line_item', 'custom_checkout_create_order_line_item', 20, 4 );
function custom_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
    // Get a product custom field value
    $custom_field_value = get_post_meta( $item->get_product_id(), 'supplier_url', true );
    // Update order item meta
    if ( ! empty( $custom_field_value ) ){
        $item->update_meta_data( '_supplier', $custom_field_value );
    }
}

add_filter('woocommerce_order_item_display_meta_key', 'filter_wc_order_item_display_meta_key', 20, 3 );
function filter_wc_order_item_display_meta_key( $display_key, $meta, $item ) {

    // Change display_key
    if( $meta->key === '_supplier' && is_admin() )
        $display_key = __("Supplier", "woocommerce" );

    return $display_key;    
}

add_filter( 'woocommerce_order_item_display_meta_value', 'change_order_item_meta_value', 20, 3 );
function change_order_item_meta_value( $value, $meta, $item ) {

    // Display supplier meta value as a button
    if( $meta->key === '_supplier' && is_admin() ) {
        $display_value = __('<a class="button" target="_blank" href="'.$value.'">Order</a>', 'woocommerce' );

        return $display_value;    
    }
}

These images show the before and after of using the last block of code.

Before:

前

After:

后

Where have I gone wrong with my code and is what i'm trying to achieve possible?

The main mistake is on last function where $display_value should be replaced with just $value and then return $value; should be located at the end before last closing bracket.

I have also revisited all your code:

add_action( 'woocommerce_checkout_create_order_line_item', 'custom_checkout_create_order_line_item', 20, 4 );
function custom_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
    $supplier_url = $values['data']->get_meta( 'supplier_url' ); // Get product custom field value
    
    // add product custom field as custom order item meta data
    if ( ! empty($supplier_url) ){
        $item->update_meta_data( '_supplier', $supplier_url );
    }
}

add_filter('woocommerce_order_item_display_meta_key', 'filter_wc_order_item_display_meta_key', 20, 3 );
function filter_wc_order_item_display_meta_key( $display_key, $meta, $item ) {

    // Change displayed label for specific order item meta key
    if( is_admin() && $item->get_type() === 'line_item' && $meta->key === '_supplier' ) {
        $display_key = __("Supplier", "woocommerce" );
    }
    return $display_key;
}

add_filter( 'woocommerce_order_item_display_meta_value', 'change_order_item_meta_value', 20, 3 );
function change_order_item_meta_value( $value, $meta, $item ) {

    // Change displayed value for specific order item meta key
    if( is_admin() && $item->get_type() === 'line_item' && $meta->key === '_supplier' ) {
        $value = __('<a class="button" target="_blank" href="'.$value.'">Order</a>', 'woocommerce' );
    }
    return $value;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM