Home Forums Theme Support Dokan add order item meta

This topic is: not resolved

This topic contains 2 replies, has 2 voices, and was last updated by  Nayem 3 years, 10 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #53814

    Bryan
    Member
    Post count: 20

    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?

    #53975

    Bryan
    Member
    Post count: 20

    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);
    #54018

    Nayem
    Moderator
    Post count: 6480

    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 3 posts - 1 through 3 (of 3 total)

The topic ‘add order item meta’ is closed to new replies.