Need to add custom meta to order items. Googled it and most articles says to use “woocommerce_add_order_item_meta” hook. This hook is deprecated in the newest version 2.3.7. Someone, please tell me which hook to use instead.
http://docs.woothemes.com/wc-apidocs/function-woocommerce_add_order_item_meta.html
2017/2018 THE RIGHT WAY (Using new CRUD setters and Getters methods)
Since woocommerce 3 that has improved many things making drastic changes, the action hook
woocommerce_add_order_item_meta
still work perfectly even in woocommerce version 3.3+.This hook is enabled by
WC_Checkout
class methods and related functions in the checkout process and not inWC_Order
Class where cart data is not anymore available.Let see how to work with
woocommerce_checkout_create_order_line_item
. It has 4 available arguments:$item
is an instance ofWC_Order_Item_Product
new introduced Class$cart_item_key
is the cart item unique hash key$values
is the cart item$order
an instance of the WC_Order object (This is a very useful additional argument in some specific cases)In this hook we will replace the old working functions wc_add_order_item_meta() by the new
WC_Data
update_meta_data()
method to be used with$item
argument.Example:
Finally we can do the same with old way using
woocommerce_add_order_item_meta
hook as it has nearly the same useful arguments:If you look at
wc-deprecated-functions.php
you will seeBasically, the function was renamed to
wc_add_order_item_meta()
, so if you need the function then use that. The action hook was not renamed and remains inclass-wc-checkout.php
as:It seems that the hook is now also deprecated as of version 3.0.4.
I’m getting this notification:
I have replaced the action name ‘woocommerce_add_order_item_meta’ with ‘woocommerce_new_order_item’ in an add_action statement in an offending plugin, and the deprecation notification disappears, The problem is that some parameters now appear inside a
legacy_values
array. I use the plugin YITH WooCommerce Product Add Ons, and the product meta data that should be attached to an order is not picked up by the plugin and therefore not stored with the order. So until this is fixed in the plugin you have to live with the deprecation notification.I know this has been answered and there is an accepted reply already. I just wanted to give another way to handle this without actually getting a deprecated message (see reference);
Your specific use case isn’t very clear (you didn’t specify when or where you need to add this meta info), but you can use
woocommerce_checkout_update_order_meta
during checkout.Read more in customizing checkout fields.
No it seems like the hook is also deprecated:
PHP Error:
The “woocommerce_add_order_item_meta” hook uses out of date data structures and function is deprecated since version 3.1.2. Replace with woocommerce_new_order_item.
I also cannot find it here:
https://docs.woocommerce.com/wc-apidocs/hook-docs.html
I wanted to add on to Ilgıt Yıldırım’s answer: in my case, my custom values did not exist in the item->legacy_values array. To fix this, I used the woocommerce_checkout_create_order_line_item hook to add the custom values to the item prior to calling the woocommerce_new_order_item hook. Here is an example of that:
add_action( ‘woocommerce_checkout_create_order_line_item’, ‘save_values_in_item’, PHP_INT_MAX, 4 );
function save_values_in_item( $item, $cart_item_key, $values, $order ) {
}
//THEN call the new hook:
add_action( ‘woocommerce_new_order_item’, ‘add_product_input_fields_to_order_item_meta_wc3’, PHP_INT_MAX, 3 );
function add_product_input_fields_to_order_item_meta_wc3( $item_id, $item, $order_id ) {
}
Just to make things clear, this function was deprecated, but the hook is still ok