Add WooCommerce Email Recipients Conditionally

,



First of all, for some reason this filter is run on the WooCommerce > Emails settings page. However, in this context, the order object for the email is not set (since it’s obviously not being run as part of an order). No bueno.

We’ll get an error that basically kills our email settings page, which we absolutely don’t want, and that’s why this isn’t quite straight-forward (and why you should always test your snippets!).

We’ll need to avoid this with a preliminary pre-flight check. I’ll $_GET the page we’re on, and bail out if we’re on any WooCommerce settings page, so we can avoid errors to keep our settings intact.

$page = $_GET['page'] = isset( $_GET['page'] ) ? $_GET['page'] : '';
if ( 'wc-settings' === $page ) {
    return $recipient; 
}

My second sanity check that the order we’re passed in is an instance of WC_Order probably isn’t necessary, but it’s used just in case a custom order type triggers this email as well.

Next, we’ll get the items in the order, get the products from the line item, and if the line item is a product, we’ll check it to see if it’s a non-virtual, shippable product.

Fortunately, the WC_Product class has the exact method I’ll need here — $product->needs_shipping() will check if I have a virtual product (no shipping) or not, so we can use this to base our recipient addition on.

If we do have a product that requires shipping, we’ll add our recipient and bail out of our loop checking order items (after all, once we know we have at least one shipped product, the recipient will be added and we don’t need to add it again — we’re done).

Return your additional recipient like: , new@email.com — the filter expects a comma-separated list of emails, so you should add your recipient to the end of the list with an additional comma (another pre-flight check you need to be aware of — know what you should be returning!).

Finally, don’t forget to return the original recipient in case our order doesn’t contain a shippable product, as we want to return the original recipient list back.


<?php 
function sv_conditional_email_recipient( $recipient, $order ) { 
 $page = $_GET['page'] = isset( $_GET['page'] ) ? $_GET['page'] : '';
 if ( 'wc-settings' === $page ) {
  return $recipient; 
 } 
 
 if ( ! $order instanceof WC_Order ) {
  return $recipient; 
 }
 
 $items = $order->get_items();
 
 // check if a shipped product is in the order 
 foreach ( $items as $item ) {
  $product = $order->get_product_from_item( $item ); 
  // add our extra recipient if there's a shipped product - commas needed!
  // we can bail if we've found one, no need to add the recipient more than once
  if ( $product && $product->needs_shipping() ) {
   $recipient .= ', warehouse-manager@example.com';
   return $recipient;
  }
 }
 
 return $recipient;
}
add_filter( 'woocommerce_email_recipient_new_order', 'sv_conditional_email_recipient', 10, 2 );



Related Post


Latest Post


Recent Posts Widget

Make sure to never miss a thing...

Get the latest news from the creative industry along with other creative goodies, conveniently delivered to social media.