Kyle Vee
Kyle Vee

Reputation: 31

Woocommerce new order notification to different email based on shipping country

Been pulling my hair out over this for a few days now. The scenario is that 1 store has 2 locations so based on the shipping country selected at checkout the new order notification needs to go to the relevant email address.

I got this to work....partially.

During testing phase I used "direct bank transfer" payment method for the sake of getting the order completed. The below code works great with that payment method but as i discovered later when an order gets placed through paypal it no longer works.

Futhermore, if you change the product to virtual then it works with paypal, but as soon as you add

add_filter( 'woocommerce_cart_needs_shipping', '__return_true' );

to bring back the shipping options then it stops working again.

Here is my code so far

    global $woocommerce;
if (strtolower($woocommerce->customer->get_shipping_country()) === 'us') {
$this->send( '[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() );
};

Hopefully someone can spot something obvious that I am missing, thanks in advance!

UPDATE: Zapier works but has some limitations, I would still like to find a solution to this that doesnt require a third party app.

Upvotes: 0

Views: 5221

Answers (4)

Javier Labbe
Javier Labbe

Reputation: 1

I tried applying the code to allow for conditional emails based on a custom checkout field - select with two values. As described and no emails went out.

if ($this->object->billing_divisions === 'sprinkler') {
  $this->send( '[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() );
}

I edited the php file again to add back the replaced code. Otherwise new order emails are not triggered for admin.

if ( $this->is_enabled() && $this->get_recipient() ) {

I've been racking my brain trying to get conditional emails working based on a custom checkout field selection.

Upvotes: 0

Nathan Carvalho
Nathan Carvalho

Reputation: 1

This works better than any other option that I have seen, thanks! I changed the shipping_country for billing_state.

Is it possible to add more than two variants? Let say something like:

    if ($this->object->billing_state === 'SC') {
    $this->send( 
        '[email protected]', 
        $this->get_subject(),     
        $this->get_content(), 
        $this->get_headers(), 
        $this->get_attachments()
    ); 
}

    if ($this->object->billing_state === 'RS') {
        $this->send( 
            '[email protected]', 
            $this->get_subject(),
            $this->get_content(), 
            $this->get_headers(), 
            $this->get_attachments() 
        );
    } 

if ($this->object->billing_state === 'PR') {
        $this->send( 
            '[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() 
        );
    }

Upvotes: 0

Kyle Vee
Kyle Vee

Reputation: 31

Found the solution.

For anyone with the same problem in future, in class-wc-email-new-order.php replace

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

with

if ($this->object->shipping_country === 'US') {
$this->send( '[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() );
}

The problem was using the $woocommerce global to grab the value from the customer when you should be grabbing the value from the order itself.

If you want to create a conditional based on a different checkout field you can find the list in class-wc-api-orders.php

Just replace

 $order->shipping_country

With

$this->object->shipping_country

Upvotes: 2

redleaf
redleaf

Reputation: 425

There is an alternative way to accomplish this using a site called Zapier. It allows you to link up multiple APIs, one of which is WooCommerce. All you would need to do is create a trigger that would send an email to [email protected] if the Shipping Country was US, and [email protected] if it wasn't, regardless of payment method.

Upvotes: 1

Related Questions