Woocommerce: Translate/change order item meta key before output

Viewed 271

There's an action called 'woocommerce_checkout_create_order_line_item' where you can use the method 'add_meta_data(meta_key, meta_value)' to add meta data to the order item. Every example did use this syntax: add_meta_data(__('Some key name', 'woocommerce'), $value);

This is perfect as long as the admin and the customer have set the same language and the field is only informal. But I have to process the field programmatically later. So the key must always be the same, no matter which language is used. So I use a simple string, but it won't be translated in the admin order area, on the Thank-You page, in the shopping cart etc. anymore.

How can I override the automatic output of the order item meta data? I've tried to hook i.e. into 'woocommerce_before_order_itemmeta($item_id, $item, $product)' (Admin order page), but there I can only delete $item->delete_meta_data('my_key'), I cannot add temporary meta keys => $item->add_meta_data(__('Translated key'), $value); I also tried:

$item->set_meta_data(array('id' => 0, 'key' => __('Translated key'), 'value' => 'test123'));
print_r($item->get_meta_data());

Thanks!

1 Answers

There is no need to do the translation in the meta key.

What you could do is use the hook woocommerce_order_item_display_meta_key. There you can catch your key and output a tranlation for display.

Also you can filter keys to not display all custom meta keys if you have some keys that should not show up in the order details, but are for internal use only. This can be done with woocommerce_hidden_order_itemmeta

Here is a good post on working with order item meta data: https://www.ibenic.com/manage-order-item-meta-woocommerce/

Related