title: Adding recipients to Woocommerce email notifications based on product variation term
body: <p>I have created a Woocommerce plugin and require it to do two things:</p>
  
  <ol>
  <li><p>Send a notification message to a specific email address, based on which product variation is in the cart.</p></li>
  <li><p>The email must contain only the relevant product, and not products that contain other attributes.</p></li>
  </ol>
  
  <p><strong>For example:</strong></p>
  
  <p>Product A has an Attribute named Chef, with chef-one and chef-two as variable Terms. The user may select Product A from chef-one or chef-two.</p>
  
  <p>If the user selects Product A from chef-one, a notification email must be sent to chefone@email.com containing the name of the product ordered (as it would show up in a regular Woocommerce notification email).</p>
  
  <p>If the user selects Product A from chef-one and Product B from chef-two, a notification email must be sent to chef-one containing only Product A, and a notification email must be sent to chef-two containing only Product B.</p>
  
  <p>I have created the plugin using the tutorial found on <a href="https://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/" rel="nofollow noreferrer">https://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/</a> and have adapted it to suit the above purpose.</p>
  
  <p>I have also adapted code found the following solutions:
  <a href="https://stackoverflow.com/questions/40633541/adding-a-custom-woocommerce-email-based-on-the-product-attribute">Adding a custom woocommerce email based on the product attribute</a>
  <a href="https://stackoverflow.com/questions/26429482/woocommerce-need-to-send-email-to-specific-address-based-on-zip-code">Woocommerce - Need to send email to specific address based on zip code</a></p>
  
  <p>Here is the code from my plugin's class file:</p>
  
  <pre><code>if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  
  /**
   * A custom WAKIKI Order WooCommerce Email class
   *
   * @since 0.1
   * @extends \WC_Email
   */
  
  class WC_Wakiki_Order_Email extends WC_Email {
  
  
  /**
   * Set email defaults
   *
   * @since 0.1
   */
  public function __construct() {
  
      // set ID, this simply needs to be a unique name
      $this-&gt;id = 'wc_wakiki_order';
  
      // this is the title in WooCommerce Email settings
      $this-&gt;title = 'WAKIKI Order';
  
      // this is the description in WooCommerce email settings
      $this-&gt;description = 'WAKIKI Order Notification emails are sent when a customer places an order on the website';
  
      // these are the default heading and subject lines that can be overridden using the settings
      $this-&gt;heading = 'WAKIKI Delivery Order';
      $this-&gt;subject = 'WAKIKI Delivery Order';
  
      // these define the locations of the templates that this email should use, we'll just use the new order template since this email is similar
      $this-&gt;template_html  = 'emails/admin-new-order.php';
      $this-&gt;template_plain = 'emails/plain/admin-new-order.php';
  
      // Trigger on new paid orders
      add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'trigger' ) );
      add_action( 'woocommerce_order_status_failed_to_processing_notification',  array( $this, 'trigger' ) );
  
      // Call parent constructor to load any other defaults not explicity defined here
      parent::__construct();
  
      // this sets the recipient to the settings defined below in init_form_fields()
      $this-&gt;recipient = $this-&gt;get_option( 'recipient' );
  
      // if none was entered, just use the WP admin email as a fallback
      if ( ! $this-&gt;recipient )
          $this-&gt;recipient = get_option( 'admin_email' );
  }
  
  
  /**
   * Determine if the email should actually be sent and setup email merge variables
   *
   * @since 0.1
   * @param int $order_id
   */
  public function trigger( $order_id ) {
  
      // Bail if no order ID is present
      if ( ! $order_id )
          return;
  
        $order = new WC_Order( $order_id );
  
        // Find the product_id
        $items = $order-&gt;get_items();
        foreach ( $items as $item ) {
            $product_id = $item['product_id'];
        }
              // From the product_id get the product attribute
              $product = new WC_Product( $product_id );  // create an object of WC_Product class
  
              $patt = $product-&gt;get_attributes();  // call get_attributes method
  
              // Condition valid to send the email (if the attributes is chef)
              if ( array_key_exists('pa_chef', $patt) ) 
  
              // Determine which email address to send to, based on Product Attribute Term)
              add_filter( 'new_order' , 'add_recipient', 20, 2 );
  
              function add_recipient( $email, $order ) {
                  $additional_email = "info@email.com";
                  $terms = get_terms("pa_chef");
                  if( $order-&gt;$term-&gt;name == "pa_chef-one" ){
                   $email = explode( ',', $email );
                   array_push( $email, $additional_email );
  }
  return $email;
  }
  
  
  
      {
              // Send the email
              // Setup order object
              $this-&gt;object = new WC_Order( $order_id );
              $this-&gt;recipient    = $this-&gt;object-&gt;billing_email;
  
              // Replace variables in the subject/headings
              $this-&gt;find[] = '{order_date}';
              $this-&gt;replace[] = date_i18n( woocommerce_date_format(), strtotime( $this-&gt;object-&gt;order_date ) );
  
              $this-&gt;find[] = '{order_number}';
              $this-&gt;replace[] = $this-&gt;object-&gt;get_order_number();
  
              if ( ! $this-&gt;is_enabled() || ! $this-&gt;get_recipient() )
                  return;
  
              // Send the email!
              $this-&gt;send( $this-&gt;get_recipient(), $this-&gt;get_subject(), $this-&gt;get_content(), $this-&gt;get_headers(), $this-&gt;get_attachments() );
  
          }
  
          else
          {
              return; //do nothing if there is not chef attribute
          }
  }
  
  
  /**
   * get_content_plain function
   *
   * @since 0.1
   * @return string
   */
  public function get_content_plain() {
      ob_start();
      woocommerce_get_template( $this-&gt;template_plain, array(
          'order'         =&gt; $this-&gt;object,
          'email_heading' =&gt; $this-&gt;get_heading()
      ) );
      return ob_get_clean();
  }
  
  
  /**
   * Initialize Settings Form Fields
   *
   * @since 2.0
   */
  public function init_form_fields() {
  
      $this-&gt;form_fields = array(
          'enabled'    =&gt; array(
              'title'   =&gt; 'Enable/Disable',
              'type'    =&gt; 'checkbox',
              'label'   =&gt; 'Enable this email notification',
              'default' =&gt; 'yes'
          ),
          'recipient'  =&gt; array(
              'title'       =&gt; 'Recipient(s)',
              'type'        =&gt; 'text',
              'description' =&gt; sprintf( 'Enter recipients (comma separated) for this email. Defaults to &lt;code&gt;%s&lt;/code&gt;.', esc_attr( get_option( 'admin_email' ) ) ),
              'placeholder' =&gt; '',
              'default'     =&gt; ''
          ),
          'subject'    =&gt; array(
              'title'       =&gt; 'Subject',
              'type'        =&gt; 'text',
              'description' =&gt; sprintf( 'This controls the email subject line. Leave blank to use the default subject: &lt;code&gt;%s&lt;/code&gt;.', $this-&gt;subject ),
              'placeholder' =&gt; '',
              'default'     =&gt; ''
          ),
          'heading'    =&gt; array(
              'title'       =&gt; 'Email Heading',
              'type'        =&gt; 'text',
              'description' =&gt; sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: &lt;code&gt;%s&lt;/code&gt;.' ), $this-&gt;heading ),
              'placeholder' =&gt; '',
              'default'     =&gt; ''
          ),
          'email_type' =&gt; array(
              'title'       =&gt; 'Email type',
              'type'        =&gt; 'select',
              'description' =&gt; 'Choose which format of email to send.',
              'default'     =&gt; 'html',
              'class'       =&gt; 'email_type',
              'options'     =&gt; array(
                  'plain'     =&gt; __( 'Plain text', 'woocommerce' ),
                  'html'      =&gt; __( 'HTML', 'woocommerce' ),
                  'multipart' =&gt; __( 'Multipart', 'woocommerce' ),
              )
          )
      );
  }
  </code></pre>
  
  <p>This is as close as I am able to get it, but it is not working. I suspect the problem lies somewhere around the line that says "Determine which email address to send to, based on Product Attribute Term". The plugin was loading until I added that section.</p>
  
  <p>Is the function supposed to be in a separate plugin file?</p>
  
  <p>I also need help in getting the email to contain only the information relevant to the vendor it is being sent to.</p>
  
  <p>Any assistance in getting this plugin to work would be greatly appreciated.</p>