add order item meta

This topic contains 2 reply and 2 voices, and was last updated by Nayem 9 years, 4 months ago
Viewing 2 Posts - 1 through 2 (of 2 total)
Author Posts
July 1, 2015 at 1:24 am 53814
Nayem I am attempting to add order item meta. I have successfully stored the meta using the following code: function add_order_item_meta($item_id, $values, $cart_item_key) { $addMeta = 'some value'; wc_add_order_item_meta($item_id, '_meta_key', $addMeta, false); } add_action('woocommerce_add_order_item_meta', 'add_order_item_meta', 10, 3); This works well for the parent order, however when dokan splits the order when there are multiple sellers this breaks. for example: if a customer makes an order with two products from different sellers and the main order has the order id of 1. dokan then breaks this order up creating order 2 and 3. These items in the new orders(order 2 and 3) are given different order_item_id's from the parent order (order 1). I need the meta values to be stored with the order_item_id's for order 2 and 3. Is there a hook for this that I am missing?
July 2, 2015 at 3:03 am 53975
Bryan Bryan

I have solved the issue. I am not sure if there is a better way to do this than what I did.

Please let me know if there is a better way to do this with a different hook!?!?

What I did was include the following code:

function add_sub_order_item_meta($order_id) {
    $sub_orders = get_children( array( 'post_parent' => $order_id, 'post_type' => 'shop_order' ) );
    if ( $sub_orders ) {
        foreach ($sub_orders as $sub) {
            $order = new WC_Order( $sub->ID );
			
			//get the order items
			$order_item = $order->get_items();
			
			foreach( $order_item as $item_id => $product ) {
				//get values for vars
				$addMeta = 'some value';
                                //add new meta
				wc_add_order_item_meta($item_id, '_meta_key', $addMeta, false);
			}
        }
    }
}
add_action('woocommerce_payment_complete', 'add_sub_order_item_meta', 10, 1);
July 2, 2015 at 12:26 pm 54018
Nayem Nayem

Hello Bryan,

I am very glad to know that you have solved your issue. I hope this code will help our others user.

Thanks.

Viewing 2 Posts - 1 through 2 (of 2 total)