Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

php - 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:

Before

After:

After

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

question from:https://stackoverflow.com/questions/65892137/change-order-item-custom-meta-data-displayed-label-and-value-in-woocommerce-admi

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...