How to add a meta_data to each line item with the regular price in WooCommerce rest API

Viewed 30

I need to have the regular price available in the line_items of the WooCommerce rest API to see if the product has a discount and to know what the price without discount is.

I tried wc_add_order_item_meta but I can't get it to work.

1 Answers

I managed to do it in the following way and it worked wonderfully:

function add_order_item_meta($item_id, $values) { 
  $item_data = $values->get_data(); 
  $product_id = $item_data['product_id']; 
  $variation_id = $item_data['variation_id']; 
  $product_id = $variation_id > 0 ? $variation_id : $product_id; 
  $product = wc_get_product($product_id); 
  wc_add_order_item_meta( $item_id, "item_regular_price", $product->get_regular_price()); 
} 
add_action('woocommerce_new_order_item', 'add_order_item_meta', 10, 2);

I hope this solution can help someone, bye!

Related