Nik7
Nik7

Reputation: 386

Add sub order information in order confirmation mail (WooCommerce / Dokan)

We want to display the sub order of a parent order of a customer orders items from different vendors. Without that, it's hard for the customer to understand that his order is splittet by vendor. So we want to display each vendor from the whole order and then the sub order number for each vendor.

That we need for a result like this:

Parent order: #001

Vendor 1 (Sub): #002
Vendor 2 (Sub): #003
Vendor 3 (Sub): #004

Current code:

function action_woocommerce_email_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
    // Get the order ID
    $order_id = $order->get_id();
           
    // Check if order has sub orders
    if ( $order->get_post_meta( 'has_sub_orders' ) ) {
    
        // Display parent order number
        echo '<h2>' . __( 'Parent Order', 'woocommerce' ) . ' #' . $order . '</h2>';
    
        
        // Outout sub order information
        echo '<strong>' . __( 'Note: ', 'dokan'  ) . '</strong>';
        echo '<span>' . __( 'This order has products from multiple vendors/sellers. So we divided this order into multiple seller orders. Each order will be handled by their respective seller independently.', 'dokan' ) . '</span><br>';



        // Get sub order from parent order
        $sub_orders = get_children( 
            array(  
                'post_type'   => 'shop_order',    
                'post_status' => array( 
                    'wc-pending', 
                    'wc-completed', 
                    'wc-processing', 
                    'wc-on-hold' 
                )
            ) 
        );

        // Output each sub order
        foreach ( $sub_orders as $order_post ) {
            
            // Get sub order number
            $new_order  = new WC_Order( $order_post->ID );
            
            // Get vendor store name from sub order
            $order_seller_id  = dokan_get_seller_by_order( $new_order );
            $vendor = dokan()->vendor->get( $order_seller_ld );
            $shop_name = $seller->get_shop_name();
            
            // Output
            echo __( 'Vendor: ', 'dokan'  ) . $shop_name . ' #'.$new_order->get_order_number() . '<br>';

            }
    }


}

add_action( 'woocommerce_email_before_order_table', 'action_woocommerce_email_before_order_table', 10, 4 );

Upvotes: 0

Views: 2240

Answers (1)

twofed
twofed

Reputation: 547

The function for defining suborders is missing a parameter that points to the parent order:

$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' 
        )
    ) 
);

Upvotes: 2

Related Questions