/**
  * Test will pass if the customer is buying from the store (all or a specific product) for the first time.
  *
  * If a `product_id` property is present, this test will pass if that product is being
  * purchased for the first time. It will fail otherwise.
  *
  * @param FUE_Sending_Queue_Item $item
  * @return bool|WP_Error
  */
 public function test_first_purchase_condition($item)
 {
     $customer = fue_get_customer_from_order($item->order_id);
     if (!$customer) {
         return new WP_Error('fue_email_conditions', sprintf(__('Customer data could not be found (Order #%d)', 'follow_up_emails'), $item->order_id));
     }
     if (empty($item->product_id)) {
         $count = $this->fue_wc->count_customer_purchases($customer->id, $item->product_id);
     } else {
         $count = $this->fue_wc->count_customer_purchases($customer->id);
     }
     if ($count != 1) {
         return new WP_Error('fue_email_conditions', sprintf(__('first_purchase condition failed for queue #%d (purchases: %d)', 'follow_up_emails'), $item->id, $count));
     }
     return true;
 }
 /**
  * Queue customer emails
  * @param WC_Order $order
  * @return array
  */
 protected function queue_customer_emails($order)
 {
     $wpdb = Follow_Up_Emails::instance()->wpdb;
     $queued = array();
     $user_id = WC_FUE_Compatibility::get_order_user_id($order);
     if ($user_id > 0) {
         $fue_customer = fue_get_customer($user_id);
         if (!$fue_customer) {
             FUE_Addon_Woocommerce::record_order($order);
             $fue_customer = fue_get_customer($user_id);
         }
         $fue_customer_id = $fue_customer->id;
     } else {
         $fue_customer = fue_get_customer(0, $order->billing_email);
         $fue_customer_id = $fue_customer->id;
     }
     if ($fue_customer_id) {
         /**
          * Look for and queue first_purchase and product_purchase_above_one emails
          * for the 'storewide' email type
          */
         $product_ids = $this->get_product_ids_from_order($order);
         foreach ($product_ids as $product_id) {
             // number of time this customer have purchased the current item
             $num_product_purchases = $this->fue_wc->count_customer_purchases($fue_customer_id, $product_id['product_id']);
             if ($num_product_purchases == 1) {
                 // First Purchase emails
                 $queued = array_merge($queued, $this->queue_first_purchase_emails($product_id['product_id'], 0, $order));
             } elseif ($num_product_purchases > 1) {
                 // Purchase Above One emails
                 $queued = array_merge($queued, $this->queue_purchase_above_one_emails($product_id['product_id'], 0, $order));
             }
             // category match
             $cat_ids = wp_get_post_terms($product_id['product_id'], 'product_cat', array('fields' => 'ids'));
             if ($cat_ids) {
                 foreach ($cat_ids as $cat_id) {
                     $num_category_purchases = $this->fue_wc->count_customer_purchases($fue_customer_id, 0, $cat_id);
                     if ($num_category_purchases == 1) {
                         // first time purchasing from this category
                         $queued = array_merge($queued, $this->queue_first_purchase_emails($product_id['product_id'], $cat_id, $order));
                     } elseif ($num_category_purchases > 1) {
                         // purchased from this category more than once
                         $queued = array_merge($queued, $this->queue_purchase_above_one_emails($product_id['product_id'], $cat_id, $order));
                     }
                 }
             }
             // end category match
         }
         // storewide first purchase
         $num_storewide_purchases = $this->fue_wc->count_customer_purchases($fue_customer_id);
         if ($num_storewide_purchases == 1) {
             // first time ordering
             $queued = array_merge($queued, $this->queue_first_purchase_emails(0, 0, $order));
         }
     }
     // look for customer emails
     // check for order_total
     $triggers = array('order_total_above', 'order_total_below', 'total_orders', 'total_purchases');
     $emails = fue_get_emails('customer', FUE_Email::STATUS_ACTIVE, array('meta_query' => array('relation' => 'AND', array('key' => '_interval_type', 'value' => $triggers, 'compare' => 'IN'))));
     foreach ($emails as $email) {
         if (!$this->customer_email_matches_order($email, $order)) {
             continue;
         }
         $insert = array('send_on' => $email->get_send_timestamp(), 'email_id' => $email->id, 'order_id' => $order->id);
         if (!is_wp_error(FUE_Sending_Scheduler::queue_email($insert, $email))) {
             $queued[] = $insert;
         }
     }
     // special trigger: last purchased
     $queued = array_merge($queued, $this->queue_customer_last_purchased_emails($order));
     return $queued;
 }