Saving a product meta field to order item meta in WooCommerce

First time hopefully I've done this correctly. :)

I'm currently working on a snippet to save a custom product field (purchase price) into the order item meta. The reason that we want to do this is that the purchase price changes during the time where orders are accepted. In order to produce accurate reporting - we need to know what the purchase price field (custom product field) was set to when the order was placed. Simply exporting the custom field (_wcj_purchase_price) gives the current value, which may not be current.

The custom field that stores the purchase price is called _wcj_purchase_price and I am trying to create a custom order item meta field called _purchase_price which is added at the time the order is created.

This is the code that I've got so far, from my attempts, but it's not working quite right. I am sure the answer is obvious to someone, but I'm very stuck at the moment!

add_action( 'wc_add_order_item_meta', 'save_item_sku_order_itemmeta', 10, 3 );

function save_item_sku_order_itemmeta( $item_id, $values, $cart_item_key ) {
        $purchasep = get_post_meta( $values[ 'product_id' ], '_wcj_purchase_price', true );
        wc_add_order_item_meta( $item_id, '_purchase_price', $purchasep , false );

Any ideas or advice? Please let me know if anything is unclear.

Thank you very much!


Are you sure that there is a filter called "wc_add_order_item_meta"?

Correct filter for this case can be woocommerce_new_order.

add_action('woocommerce_new_order', function ($order_id) {
     // your code here
}, 10, 1);

For exampe, you can use such code:

add_action('woocommerce_new_order', function ($order_id) {
     $order = wc_get_order( $order_id );
     foreach( $order->get_items() as $item_id => $item ){
       $product_id = $item->get_product_id();
       $purchasep = get_post_meta( $product_id, '_wcj_purchase_price', true );
       wc_add_order_item_meta($item_id, '_purchase_price', $purchasep , false );
 }, 10, 1);

Similar questions

Meta query field order together with post_date order causes posts without the meta field to be unordered
I've managed to get wordpress to do a descending order on a meta_query field called start_date which is saved in Ymd format in the database by the ACF plugin. However in my query the other post types that doesn't have this field are unordered and also seems to always come after all the posts which contain the meta field. Example on how the order cu...
woocommerce - only allow 1 item in cart and add current item by replacing the older item
I want to limit the cart to only 1 item. If the user has 0 items in the cart it will add the item to the cart. If the user has the previous item in the cart then it must be replaced the current item. PS: I found a similar question but no answer for it. WooCommerce: Only 1 product in cart. Replace if one is added
Add order item image only if the product exist on Woocommerce order view
On woocommerce My account view order pages I was able to add the product image using this answer code: Add the product image to Woocommerce my account order view But I have an issue related to imported orders where the product doesn’t exist. So this make an error with a blank page, when viewing those orders. Any idea on how to avoid this problem?
Woocommerce Display a short description in the order overview via the Permalink Product Link in order-details-item.php
I would like to have the respective short description of the product / article displayed in WOOCOMMERCE in the order overview "Order-Details-Item" (template / woocommerce / order / order-details-item.php) about the product Permalink. I found the following code on the web to allow a brief description. Here is the code for this: But how do I incorpor...
Save Order item custom field in Woocommerce Admin order pages
I have add custom fields in back office order for each line products : (source: My code: My problem is that I don't know how to save this fields. Any help?
why after saving meta value it's saving all the values the one that i clicked?
I have a code that save meta post and show them to the visitors: until now every thing is ok. now i want: after a visitor click "save this in meta field", the "name" and one more data ("chap") should save in a new meta key (i know it's better to save in user meta and now i'm just trying to save in post meta). the code ...

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.