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?
question from:
https://stackoverflow.com/questions/65892137/change-order-item-custom-meta-data-displayed-label-and-value-in-woocommerce-admi 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…