Search code examples
phpwordpresswoocommercedokan

How can I send the 'Customer Completed' email to the customer's email in WooCommerce?


I'm currently using WordPress, WooCommerce, and Dokan. I have a requirement to resend the "Customer Completed" email to the seller instead of the customer. Unfortunately, the code I wrote is still sending the email to the customer.

Here's the code I have so far:

$email_class = WC()->mailer()->get_emails()['WC_Email_Customer_Completed_Order'];

             // Send the email to seller
             $vendor_id = get_post_meta( $order_id, '_dokan_vendor_id', true );
             if ( $vendor_id ) {
                 $seller_email = dokan()->vendor->get( $vendor_id )->get_email();
                 if ( $seller_email ) {
                     $email_class->recipient = $seller_email;
                     $email_class->trigger($order_id);
                 }
             }

I would greatly appreciate it if someone could assist me in resolving this issue.

Thank you!


Solution

  • That's because the recipient is being set inside the trigger(), so $email_class->recipient = $seller_email; will be overwritten. you need to use recipient filter hook instead.

    It should be like that:

    function filter_completed_order_recipient( $recipient, $order, $email ) {
        $vendor_id = get_post_meta( $order->get_id(), '_dokan_vendor_id', true );
        if ( $vendor_id ) {
            $seller_email = dokan()->vendor->get( $vendor_id )->get_email();
            if ( $seller_email ) {
                $recipient = $seller_email;
            }
        }
        return $recipient;
    }
    
    $email_class = WC()->mailer()->get_emails()['WC_Email_Customer_Completed_Order'];
    
    add_filter( 'woocommerce_email_recipient_customer_completed_order', 'filter_completed_order_recipient', 100, 3 );
    
    $email_class->trigger( $order_id );
    
    remove_filter( 'woocommerce_email_recipient_customer_completed_order', 'filter_completed_order_recipient', 100 );