3

I want to add some meta data to the order item in WooCommerce. These meta fields are for internal use only and shouldn't be visible.

We have some extra fields in the product like an extra fee. I want to use that fee later to work with after I export the orders.

I found a very good answer here: https://stackoverflow.com/a/41988701/1788961

add_action('woocommerce_checkout_create_order_line_item', 'add_custom_hiden_order_item_meta_data', 20, 4 );
function add_custom_hiden_order_item_meta_data( $item, $cart_item_key, $values, $order ) {

    // Set user meta custom field as order item meta
    if( $meta_value = get_user_meta( $order->get_user_id(), 'billing_enumber', true ) )
        $item->update_meta_data( 'pa_billing-e-number', $meta_value );
}

But with this example, the content from the meta fields will appear in the order details for the customer.

Is there a way to make these fields only visible in the backend and usable for internal functions?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Cray
  • 5,307
  • 11
  • 70
  • 166
  • Before you proceed much further down this path you should know that `woocommerce_add_order_item_meta`, both as hook and as function, is deprecated, as discussed here: https://stackoverflow.com/questions/29666820/woocommerce-which-hook-to-replace-deprecated-woocommerce-add-order-item-meta . If you can specify your request a little more - like when the meta needs to be added or found - we can probably answer your question more usefully (unless you figure it out for yourself, maybe atter wrapping your head around the examples provided). – CK MacLeod Jun 13 '20 at 19:13
  • I have edited my question with my use case. Until now I have no solution for that. And I need it in the order for different reasons. – Cray Jun 13 '20 at 19:19

2 Answers2

7

Updated

The simple way set any meta value as hidden order item meta data only visible on admin Order edit pages is to add an underscore at the beginning of the meta key like:

add_action('woocommerce_checkout_create_order_line_item', 'add_custom_hiden_order_item_meta_data', 20, 4 );
function add_custom_hiden_order_item_meta_data( $item, $cart_item_key, $values, $order ) {

    // Set user 'billing_enumber' custom field as admin order item meta (hidden from customer)
    if( $meta_value = get_user_meta( $order->get_user_id(), 'billing_enumber', true ) )
        $item->update_meta_data( '_billing_enumber', $meta_value );
}

Then to have a clean label name for this meta key on admin order items, you can use:

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 ) {

    // Set user meta custom field as order item meta
    if( $meta->key === '_billing_enumber' && is_admin() )
        $display_key = __("Billing E Number", "woocommerce" );

    return $display_key;    
}

This code goes in function.php file of your active child theme (or cative theme). Tested and works.

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Helped me a lot. Thank you! I found out that there is even a hook to edit the displayed value. `woocommerce_order_item_display_meta_value` – Earl Grey Aug 20 '21 at 09:55
0
add_action('woocommerce_add_order_item_meta','mau_add_values_to_order_item_meta',1,2);
if(!function_exists('mau_add_values_to_order_item_meta'))
{
  function mau_add_values_to_order_item_meta($item_id, $values)
  {
        global $woocommerce,$wpdb;
        
        $postcode = $values['postcode'];
        $extend_date_delivery = $values['extend_date_delivery'];
        $extend_date_collection = $values['extend_date_collection'];
        $pro_rental_type_choose = $values['pro_rental_type_choose'];
        if(!empty($postcode))
        {
            wc_add_order_item_meta($item_id,'postcode',$postcode);  
        }
        if(!empty($extend_date_delivery))
        {
            wc_add_order_item_meta($item_id,'extend_date_delivery',$extend_date_delivery);  
        }
        if(!empty($extend_date_collection))
        {
            wc_add_order_item_meta($item_id,'extend_date_collection',$extend_date_collection);  
        }
        if(!empty($pro_rental_type_choose))
        {
            wc_add_order_item_meta($item_id,'pro_rental_type_choose',$pro_rental_type_choose);  
        }
  }
}

add_filter( 'woocommerce_order_item_display_meta_key', 'mau_change_shipping_note_title', 20, 3 );
function mau_change_shipping_note_title( $key, $meta, $item ) {
    if ( 'postcode' === $meta->key ) { $key = __( 'Postcode', 'your_textdomain'); }
    if ( 'extend_date_delivery' === $meta->key ) { $key = __( 'Delivery Date', 'your_textdomain'); }
    if ( 'extend_date_collection' === $meta->key ) { $key = __( 'Collection Date', 'your_textdomain'); }
    if ( 'pro_rental_type_choose' === $meta->key ) { $key = __( 'Rental Type', 'your_textdomain'); }
     
    return $key;
}
Maulik patel
  • 2,546
  • 2
  • 20
  • 26