3

I have a question about my code below. We use the code from here: Dokan plugin sends multiple email for customer for single order to make sure the system will not send order processing mails to customer for suborder and therefor only for the "parent" order. (This is fully working! => Part 1 in the code below)

Problem: However, we also have to make sure that the order completed mail is not send for a parent order if this order has sub orders. Why? Because after each sub order is completed, the parent order is automatically also marked as completed. And we do not want to send that mail if this parent order is completed.

For that we changes the hook and also the IF statement. But currently, the order completed mail is also send after all sub order are completed. (Part 2 in the code below)

In my understanding, I have to check if the order IS a Parent order and IF yes, don't send the order completed mail. if (get_post_meta( $order->get_id(), 'has_sub_order', true ) ){ return ''; }

UPDATE: I think it's not working because of the used hook woocommerce_email_recipient_customer_completed_order. For processing mail I use woocommerce_email_recipient_customer_processing_order and that works.

// PART 1
// Do not send order processing mail for sub orders
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'conditional_email_notification_order_processing', 10, 2 );
function conditional_email_notification_order_processing( $recipient, $order ) {
    if( is_admin() ) return $recipient;

    if (!get_post_meta( $order->get_id(), 'has_sub_order', true ) && !empty(wp_get_post_parent_id($order->get_id())) ){
        return '';
    }
    return $recipient;
}

// PART 2
// Do not send order completed mail for parent order (IF SUB ORDERS = TRUE)
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'conditional_email_notification_order_completed', 10, 2 );
function conditional_email_notification_order_completed( $recipient, $order ) {
    if( is_admin() ) return $recipient;

    if ( get_post_meta( $order->get_id(), 'has_sub_order', true ) && empty(wp_get_post_parent_id($order->get_id())) ){
        return '';
    }
    return $recipient;
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Nik7
  • 346
  • 2
  • 16
  • These hooks seem to be from dokan since I can't find them in woocommerce. My suggestion is that you should also check the base code. May be that is where the email is being sent. So you just need to cater to one more hook. – Shahbaz A. Mar 24 '21 at 06:48
  • 1
    Just answered below, some feed back will be highly appreciated @Nik7. – LoicTheAztec Mar 28 '21 at 00:26
  • Thanks! I will test this by tomorrow! Many thanks! :) – Nik7 Mar 28 '21 at 19:20

1 Answers1

3

The main mistake in your code is empty(wp_get_post_parent_id($order->get_id()) on your 2nd code snippet that is not empty when it is a main order that has suborders. In that case the parent order Id has 0 value (so not empty).

Try the following revisited code:

// Avoid customer processing order notification for children orders
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'filter_email_recipient_customer_processing_order', 10, 2 );
function filter_email_recipient_customer_processing_order( $recipient, $order ) {
    if ( is_a( $order, 'WC_Order' ) ) {
        $has_sub_order = $order->get_meta('has_sub_order');
        $parent_order  = $order->get_parent_id();
        
        if ( ! $has_sub_order && $parent_order > 0 ){
            return '';
        }
    }
    return $recipient;
}


// Avoid customer completed order notification for main parent order
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'filter_email_recipient_customer_completed_order', 10, 2 );
function filter_email_recipient_customer_completed_order( $recipient, $order ) {
    if ( is_a( $order, 'WC_Order' ) ) {
        $has_sub_order = $order->get_meta('has_sub_order');
        $parent_order  = $order->get_parent_id();
        
        if ( $has_sub_order && $parent_order == 0 ){
            return '';
        }
    }
    return $recipient;
}

Code goes in functions.php file of the active child theme (or active theme). It could work.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Perfect!m Thank you very much. – Nik7 Mar 29 '21 at 07:28
  • Q: I want to make sure to send completed mail for child order when they completed. So I try: add_filter( 'woocommerce_email_recipient_customer_completed_order', 'filter_email_recipient_customer_completed_order2', 10, 2 ); function filter_email_recipient_customer_completed_order2( $recipient, $order ) { if ( is_a( $order, 'WC_Order' ) ) { $has_sub_order = $order->get_meta('has_sub_order'); $parent_order = $order->get_parent_id(); if ( ! $has_sub_order && $parent_order > 0 ){ return $recipient; } } return ''; } – Nik7 Mar 29 '21 at 07:29
  • But that does not work? What do I miss here? Should be the same code as // Avoid customer processing order notification for children orders and just change "return $recipient;" and "return '';" What do i miss here? – Nik7 Mar 29 '21 at 07:30
  • 1
    @Nik7 Sorry I can't help more than that… As my answer says *"it could work."* … For me the main mistake in your code was `empty(wp_get_post_parent_id($order->get_id())` that was wrong on your 2nd code snippet as it's not empty because the value is `0` when it is a main order that has suborders. – LoicTheAztec Mar 29 '21 at 10:58