Search code examples
wordpresswoocommercehook-woocommerceordersemail-notifications

Different recipients based on country for WooCommerce new order email notification


I want to send different order email to different recipients:

  • If the shipping country is Israel the it email recipient should be abc@example.com
  • If it is France, Spain, Italy, etc, the recipient should be def@example.com and ghi@example.com
  • Otherwise it should be jkl@example.com

In WooCommerce > includes > emails > class-wc-email-new-order.php file, line no. 110 @version 2.0.0, I have changed the following code:

$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );

With this code:

if ($this->object->shipping_country == 'IL') {
  $this->send( 'abc@example.com', $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
} elseif( $this->object->shipping_country == 'FR' || $this->object->shipping_country == 'BE' || $this->object->shipping_country == 'LU' || $this->object->shipping_country == 'NL' || $this->object->shipping_country == 'IT' || $this->object->shipping_country == 'PT' || $this->object->shipping_country == 'ESP' ) {
  $this->send( 'def@example.com, ghi@example.com', $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
} else {
  $this->send( 'jkl@example.com', $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
}

it worked, but I don't want to make changes in plugin's core file. How can I do that using hooks?

Also when the shipping country field is blank, the billing country should be used instead.


Solution

  • Please never edit core files!

    When you modify core files you run the risk of breaking the plugin and possibly your WordPress installation. In addition, it makes it impossible for the plugin developer to provide support for you since they have no knowledge of what you’ve changed.

    Use instead the woocommerce_email_recipient_{$email_id} filter composite hook, where {$email_id} = new_order

    So you get:

    function filter_woocommerce_email_recipient_new_order( $recipient, $order, $email ) {
        // Avoiding backend displayed error in WooCommerce email settings
        if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
        
        // Get shipping country
        $country = $order->get_shipping_country();
    
        // When empty
        if ( empty( $country ) ) {
            // Get billing country
            $country = $order->get_billing_country();
        }
    
        // Checks if a value exists in an array
        if ( in_array( $country, array( 'IL' ) ) ) {
             $recipient = 'abc@example.com';    
        } elseif ( in_array( $country, array( 'FR', 'BE', 'LU', 'NL', 'IT', 'PT', 'ESP' ) ) ) {
            $recipient = 'def@example.com, ghi@example.com';
        } else {
            $recipient = 'jkl@example.com';
        }
    
        return $recipient;
    }
    add_filter( 'woocommerce_email_recipient_new_order', 'filter_woocommerce_email_recipient_new_order', 10, 3 );