简体   繁体   中英

Avoid order completed notification for parent orders in WooCommerce Dokan

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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM