Replace woocommerce_add_order_item_meta hook in Woocommerce 3.4
I have custom code use woocommerce_add_order_item_meta hook. But woocommerce 3.4.0 show error log: "woocommerce_add_order_item_meta is deprecated since version 3.0.0! Use woocommerce_new_order_item instead." How to fix it? Thank you very much. My code:
// add data design to order
function tshirt_order_meta_handler( $item_id, $values, $cart_item_key ) {
if( WC()->session->__isset( $cart_item_key.'_designer' ) ) {
wc_add_order_item_meta( $item_id, "custom_designer", WC()->session->get( $cart_item_key.'_designer') );
}
}
add_action( 'woocommerce_add_order_item_meta', 'tshirt_order_meta_handler', 1, 3 );
Solution 1:
Updated
Since Woocommerce version 3, woocommerce_checkout_create_order_line_item
action hook now replace outdated woocommerce_add_order_item_meta
hook in a much better way using the new introduced CRUD getters and setters methods:
// Save custom data to order item meta data
add_action( 'woocommerce_checkout_create_order_line_item', 'tshirt_order_meta_handler', 20, 4 );
function tshirt_order_meta_handler( $item, $cart_item_key, $values, $order ) {
$custom_designer = WC()->session->get( $cart_item_key.'_designer' );
if( ! empty($custom_designer) ) {
$item->update_meta_data( 'custom_designer', $custom_designer );
}
}
Code goes in function.php file of your active child theme (or active theme). It should works.
See this related thread:
Woocommerce: Which hook to replace deprecated "woocommerce_add_order_item_meta"