I want to send different order email to different recipients:
- If the shipping country is Israel the it email recipient should be [email protected]
- If it is France, Spain, Italy, etc, the recipient should be [email protected] and [email protected]
- Otherwise it should be [email protected]
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( '[email protected]', $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( '[email protected], [email protected]', $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
} else {
$this->send( '[email protected]', $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.
CodePudding user response:
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 = '[email protected]';
} elseif ( in_array( $country, array( 'FR', 'BE', 'LU', 'NL', 'IT', 'PT', 'ESP' ) ) ) {
$recipient = '[email protected], [email protected]';
} else {
$recipient = '[email protected]';
}
return $recipient;
}
add_filter( 'woocommerce_email_recipient_new_order', 'filter_woocommerce_email_recipient_new_order', 10, 3 );