1

I am trying to send an additional email notification to one of our locations for delivery, based off the shipping method ID. I found this answer code, but I am not able to change it in the correct way, to make it work with my shipping methods:

add_filter( 'woocommerce_email_recipient_new_order', 'new_order_additional_recipients', 20, 2 );
function new_order_additional_recipients( $recipient, $order ) {
    if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;

    // Set Below your email adresses
    $email1 = 'name1@domain.com';
    $email2 = 'name2@domain.com';

    // Get the shipping method Id
    $shipping_items = $order->get_items('shipping');
    $shipping_item  = reset($shipping_items);
    $shipping_method_id  = $shipping_item->get_method_id() . ':';
    $shipping_method_id .= $shipping_item->get_instance_id();

    // Adding recipients conditionally
    if ( 'flat_rate:8' == $shipping_method_id )
        $recipient .= ',' . $email1;
    elseif ( 'flat_rate:9' == $shipping_method_id )
        $recipient .= ',' . $email2;

    return $recipient;
}

This is what I tried to use and for some reason I get no email sent to the recipients based on the method ID. Do I need to add order processing into the code for this to trigger? maybe something like this:

if('customer_processing_order' !=$email_id );
if('flat_rate:8' == $shipping_method_id );

I will be grateful for your help.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
avidart
  • 11
  • 2

1 Answers1

0

The code works, so you are not getting the right slugs… Now as each shipping instance ID is unique, you can try the following:

add_filter( 'woocommerce_email_recipient_new_order', 'new_order_additional_recipients', 20, 2 );
function new_order_additional_recipients( $recipient, $order ) {
    if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;

    // Set Below your email adresses
    $email1 = 'name1@domain.com';
    $email2 = 'name2@domain.com';

    // Get the shipping method Id
    $shipping_items = $order->get_items('shipping');
    $shipping_item  = reset($shipping_items);

    // Adding recipients conditionally
    if ( $shipping_item->get_instance_id() == 8 ){
        $recipient .= ',' . $email1;
    } elseif( $shipping_item->get_instance_id() == 9 ){
        $recipient .= ',' . $email2;
    }
    return $recipient;
}

It should work


To retrieve the correct shipping method identifiers, you can use the following, that will display in frontend footer, only for admins, all the shipping method identifiers (you may need to add some products to cart):

add_action('wp_footer', 'wp_footer_test_output', 10);
function wp_footer_test_output() {
    if( current_user_can('administrator') ) {
        echo '<pre>'. print_r( WC()->session->get('shipping_for_package_0')['rates'], true ) . '</pre>';
    }
}

Now that you get the shipping method Identifiers, you can remove that code.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Question: Am I only adding in the [instance_id] => 30 to where the 8 & 9 are located in the code? Or do I also update the area $shipping_items = $order->get_items('shipping'); to include [id] => szbd-shipping-method:30 as such: $shipping_items = $order->get_items('szbd-shipping-method:30'); – avidart Aug 24 '23 at 14:14
  • I tried this and it did not work. Maybe I need to add the 'shipping' as 'szbd-shipping-method:30'? – avidart Aug 24 '23 at 15:41
  • // Adding recipients conditionally if ( $shipping_item->get_instance_id() == 30 ){ $recipient .= ',' . $email1; } elseif( $shipping_item->get_instance_id() == 30 ){ $recipient .= ',' . $email2; } return $recipient; – avidart Aug 24 '23 at 15:43
  • As an edit in your question, could you please add the raw output that my last function do when you use it, and notify me here… Then I will be able to update my code for it. – LoicTheAztec Aug 26 '23 at 08:53
  • Array ( [szbd-shipping-method:30] => WC_Shipping_Rate Object ( [data:protected] => Array ( [id] => szbd-shipping-method:30 [method_id] => szbd-shipping-method [instance_id] => 30 [label] => Chattanooga Local Delivery [cost] => 10.00 [taxes] => Array ( ) ) [meta_data:protected] => Array ( [Items] => Top Shelf THCa Flower Pre-Roll – Craft Cannabis™ - Gas O.G. × 1 ) ) – avidart Aug 27 '23 at 15:29
  • This is the shipping method identifier that was displayed on the frontend footer - the other shipping method is USPS that I did not include since I couldn't comment the whole array. Please let me know if you need anything else. Thanks for your help – avidart Aug 27 '23 at 15:31