Reputation: 105
i have spent the last 3 hours trying to fix a problem with a woocommerce deprecated hook and i'm going crazy because i have tried hundred different options to make it work, and it's not happening.
This is the actual code, it suppose to save the value of custom fields. Any idea about how to make it work with a non obsolete hook?
add_action('woocommerce_add_order_item_meta','save_in_order_item_meta', 10, 3 );
function save_in_order_item_meta( $item_id, $values, $cart_item_key ) {
if( isset( $values['custom_data'] ) ) {
woocommerce_new_order_item( $item_id, $values['custom_data']['label'], $values['custom_data']['value'] );
}
}
Any help is welcome. Thank you
Edit; Already tried.
add_action( 'woocommerce_add_order_item_meta', 'custom_add_order_item_meta', 20, 3 ); function custom_add_order_item_meta(
$item_id, $values, $cart_item_key ) { // Get cart item custom data and update order item meta if( isset( $values['custom_data'] ) ) {
wc_add_order_item_meta( $item_id, $values['custom_data']['label'], $values['custom_data']['value'] ); } }
add_action( 'woocommerce_add_order_item_meta', 'custom_add_order_item_meta', 20, 3 );
function custom_add_order_item_meta( $item_id, $values, $cart_item_key ) {
$custom_field_value = $custom_field_value;
if ( ! empty( $custom_field_value ) ){
wc_add_order_item_meta( $item_id, $values['custom_data']['label'], $values['custom_data']['value'] );
}
}
Upvotes: 1
Views: 3514
Reputation: 254373
Hook woocommerce_add_order_item_meta
is replaced by woocommerce_checkout_create_order_line_item
, so with your code (assuming that the cart object contains your custom cart item data):
add_action('woocommerce_checkout_create_order_line_item', 'save_custom_order_item_meta_data', 10, 4 );
function save_custom_order_item_meta_data( $item, $cart_item_key, $values, $order ) {
if( isset( $values['custom_data']['label'] ) && isset( $values['custom_data']['value'] ) ) {
$item->update_meta_data( $values['custom_data']['label'], $values['custom_data']['value'] );
}
}
Code goes in function.php file of your active child theme (or active theme). It should works.
Related:
woocommerce_checkout_create_order_line_item
action hookUpvotes: 1