skip to Main Content

I’m using woocommerce and dokan plugin in wordpress.
After I try to order product in multi vendor, I get the sub order number in thank you page.
I try to get sub order number by running this code below but not work.

//example order id
$order = wc_get_order(1234);

$order_id = $order->get_id();

$sub_orders = get_children( 
   array(    
     'post_parent' => $order_id,    
     'post_type'   => 'shop_order',    
     'post_status' => array( 
        'wc-pending', 
        'wc-completed', 
        'wc-processing', 
        'wc-on-hold' 
       )
   ) 
);

when i try var_dump($sub_order) i got 0 array result.
is any way to get sub order?

2

Answers


  1. Try this:

    $parent_order = wc_get_order(1234);
    
    $sub_orders = get_children([
        'post_parent' => dokan_get_prop( $parent_order, 'id' ),
        'post_type'   => 'shop_order',
    ]);
    

    Note that if the parent order contains products only from one vendor your $sub_orders variable will be empty.

    Login or Signup to reply.
  2. I found this function in plugin:

    /**
     * 
     * @global object $wpdb
     * @param type $parent_order_id
     * @return type
     */
    function dokan_get_suborder_ids_by ($parent_order_id){
        
        global $wpdb;
         
         $sql = "SELECT ID FROM " . $wpdb->prefix . "posts
                 WHERE post_type = 'shop_order'
                 AND post_parent = " . $parent_order_id;
         
         $sub_orders = $wpdb->get_results($sql);
    
        if ( ! $sub_orders ) {
            return null;
        }
        
        return $sub_orders;
    

    I think you can use her for getting sub orders, for example:

    $order = wc_get_order(1234);
    
    $order_id = $order->get_id();
    
    $sub_orders = dokan_get_suborder_ids_by($order_id);
    
    foreach($sub_orders as $sub_order) {
        $child_order = wc_get_order($sub_order);
        var_dump($child_order);
    }
    

    Hope help you

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search