Saving custom WooCommerce cart data to order product item meta

I'm attempting to add meta data from the cart meta into the products in an order. So far I've been successful in adding a custom value to the products at the cart level (proved by outputting the values inline on the cart table), but once the final checkout has been done its not saving anywhere.

Adding to the cart (confirmed working):

function se_wc_add_product_order_type_cart( $cart_item, $product_id ) {
    $product_order_type = $_POST['product_order_type'] ?? '';
    if ( $product_order_type ) {
        $cart_item['product_order_type'] = sanitize_text_field( $product_order_type );
    }

    return $cart_item;
}
add_filter( 'woocommerce_add_cart_item_data', 'se_wc_add_product_order_type_cart', 10, 2 );

Adding to the order from the cart (not working):

function se_wc_product_add_on_order_item_meta( $item, $cart_item_key, $values, $order ) {
    $product_order_type = $values['product_order_type'] ?? '';
    if ( ! empty( $product_order_type ) ) {
        $item->add_meta_data( 'product_order_type', $product_order_type );
    }
}
add_action( 'woocommerce_checkout_create_order_line_item', ' se_wc_product_add_on_order_item_meta', 10, 4 );

There's nothing being saved to the database (assuming its supposed to go in the wp_woocommerce_order_itemmeta table), and doing a var_dump on the meta_data field from the woocommerce_order_item_get_formatted_meta_data hook is empty - presumably again where it would show up.

If it matters, the product is a variable product and the site is a multisite.

Solutions

You can use below code to add item meta to order meta:

    function se_wc_product_add_on_order_item_meta( $item_id, $values ) {
$product_order_type = $values['product_order_type'];
if ( ! empty( $product_order_type ) ) {
    wc_add_order_item_meta($item_id,'product_order_type',$product_order_type);
  }
}
add_action( 'woocommerce_new_order_item', ' se_wc_product_add_on_order_item_meta', 1, 2 );

Thanks.

Similar questions

how to pass data from woocommerce cart item to order item?
Using 'before_checkout' hook , im doing some calculations with the cart item data which is before payment. I need to send those data to order item meta in another hook like 'woocommerce_thankyou' after payment is done. Is there a way to relate the cart items and its corresponding order which is created?
Add custom Product data dynamically as item meta data on the Order
Is it possible to add a metadata on a product which is inside the Order? In this case, there would be one metadata but each product (in the order) would have different value. Example: Thanks. Update1 : I'm now stuck on how would I grab the values from the woocommerce_add_cart_item_data filter. I was able to successfully add the meta data from there...
WooCommerce: add different order item meta for each item in order
now with this function i can add new custom order item meta to all item of order. but how can i add different order item meta to each item of order? example i need to add each product price as order_itemmeta.
WooCommerce Add item to Order with item order meta
I would like to delete an existing WooComerce item and add x new WooComerce items from it. It depends on how high the $ item-> quantity is. My code looks like this: The point where I'm a little bit desperate is here: I've already tried this post from Stackoverflow: WooCommerce Add item to Order Could someone help me? Thank you in advance
Auto add order item custom meta data to order note in WooCommerce
In WooCommerce, with this code, i'm using product custom fields to add some custom metadata to order items when making a purchase. I also need to auto add some of this custom metadata to the customer Order Notes (basically so it gets sent to my shipping fulfillment service). Below is the code i'm currently using: As per the below screenshot, you'll...
Display order item meta data on WooCommerce admin order list custom column
I've been trying to search for this info on my own, but I failed. All I am trying to do is to pass data from wp_woocommerce_order_items table to my custom plugin, to be able to display coupon code in my custom columns, and can't figure out how to make it works. I've tried code below, but it doesn't work. And if I try to pass $order_id, how it was m...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.