How To: Add Product Meta to Line Items in Orders (Woocommerce 3.0+)

From time to time, developers or store owners want to sync across hidden meta values or custom fields from Products to corresponding Line Items in Orders.

There were a few ways to do this before Woocommerce 3.0, and some may still work, but with CRUD functions being the new (and recommended) way forward, we were looking for another way to quickly replace our old ‘order meta syncing’ functions.

This is what we’ve come up with and it’s pretty lightweight (lines extended below for readability) so we figured we’d share it, incase it can save you some development/research time.

add_action( 'woocommerce_checkout_create_order_line_item', 'ektgn_meta_to_line_item', 20, 4 );

function ektgn_meta_to_line_item( $item, $cart_item_key, $values, $order )
{
	$_p = $item->get_product();
	$key = 'meta_key_value';

	if ( false !== ( $value = $_p->get_meta( $key, true ) ) )
	{
		$item->add_meta_data( $key ,  , true ); 
	}
				
}

Worth noting that we haven’t tested it fully with variable products and it might not work superbly, but it’s a good starting point nonetheless.

Let us know how it works for you.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© ektagon LLC
New Jersey, USA