/**
  * Test will pass if the total purchase amount of a customer is above/below the specified value
  *
  * @param FUE_Sending_Queue_Item $item
  * @param array $condition
  * @return bool|WP_Error
  */
 public function test_total_purchases_condition($item, $condition)
 {
     $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));
     }
     $total = $customer->total_purchase_price;
     $value = floatval($condition['value']);
     if ($condition['condition'] == 'total_purchases_above') {
         $result = $total > $value;
     } else {
         $result = $total < $value;
     }
     if (!$result) {
         return new WP_Error('fue_email_conditions', sprintf(__('Condition "%s" failed. Total Purchases: %s / Condition value: %s', 'follow_up_emails'), $condition['condition'], $total, $value));
     }
     return $result;
 }
 /**
  * Get all matching storewide emails against the provided $order and $triggers and sort by priority
  *
  * @param WC_Order  $order
  * @param array     $triggers
  * @param bool      $always_send
  * @return array    Array of matched FUE_Email
  */
 protected function get_matching_storewide_emails($order, $triggers, $always_send = false)
 {
     $matched_emails = array();
     $category_ids = $this->get_category_ids_from_order($order);
     $emails = fue_get_emails('storewide', FUE_Email::STATUS_ACTIVE, array('meta_query' => array('relation' => 'AND', array('key' => '_interval_type', 'value' => $triggers, 'compare' => 'IN'), array('key' => '_product_id', 'value' => 0), array('key' => '_category_id', 'value' => 0))));
     foreach ($emails as $email) {
         // excluded categories
         $meta = maybe_unserialize($email->meta);
         $excludes = isset($meta['excluded_categories']) ? $meta['excluded_categories'] : array();
         if (count($excludes) > 0) {
             foreach ($category_ids as $cat_id) {
                 if (in_array($cat_id, $excludes)) {
                     continue 2;
                 }
             }
         }
         if ($this->exclude_customer_based_on_purchase_history(fue_get_customer_from_order($order), $email)) {
             continue;
         }
         $matched_emails[] = $email;
     }
     return $matched_emails;
 }
 /**
  * Send emails that matches the provided triggers to the queue
  * @param int $booking_id
  * @param array $triggers
  */
 private function create_email_order($booking_id, $triggers = array())
 {
     /**
      * @var $booking WC_Booking
      * @var $order WC_Order
      */
     $booking = get_wc_booking($booking_id);
     $last_status = get_post_meta($booking_id, '_last_status', true);
     $order = WC_FUE_Compatibility::wc_get_order($booking->order_id);
     $emails = fue_get_emails('wc_bookings', FUE_Email::STATUS_ACTIVE, array('meta_query' => array(array('key' => '_interval_type', 'value' => $triggers, 'compare' => 'IN'))));
     foreach ($emails as $email) {
         if (!empty($email->meta['bookings_last_status']) && $email->meta['bookings_last_status'] != $last_status) {
             continue;
         }
         if ($this->is_category_excluded($booking, $email)) {
             continue;
         }
         // A booking can have no order linked to it
         if ($order) {
             $customer = fue_get_customer_from_order($order);
             if (Follow_Up_Emails::instance()->fue_wc->wc_scheduler->exclude_customer_based_on_purchase_history($customer, $email)) {
                 continue;
             }
         }
         if ($email->interval_type == 'before_booking_event') {
             $start = strtotime(get_post_meta($booking_id, '_booking_start', true));
             $time = FUE_Sending_Scheduler::get_time_to_add($email->interval_num, $email->interval_duration);
             $send_on = $start - $time;
         } elseif ($email->interval_type == 'after_booking_event') {
             $start = strtotime(get_post_meta($booking_id, '_booking_end', true));
             $time = FUE_Sending_Scheduler::get_time_to_add($email->interval_num, $email->interval_duration);
             $send_on = $start + $time;
         } else {
             $send_on = $email->get_send_timestamp();
         }
         $insert = array('send_on' => $send_on, 'email_id' => $email->id, 'product_id' => $booking->product_id, 'order_id' => $booking->order_id, 'meta' => array('booking_id' => $booking_id));
         if ($order) {
             $user_id = WC_FUE_Compatibility::get_order_user_id($order);
             if ($user_id) {
                 $user = new WP_User($user_id);
                 $insert['user_id'] = $user_id;
                 $insert['user_email'] = $user->user_email;
             }
         }
         // Remove the nonce to avoid infinite loop because doing a
         // remove_action on WC_Bookings_Details_Meta_Box doesnt work
         unset($_POST['wc_bookings_details_meta_box_nonce']);
         if (!is_wp_error(FUE_Sending_Scheduler::queue_email($insert, $email))) {
             // Tell FUE that an email order has been created
             // to stop it from sending storewide emails
             if (!defined('FUE_ORDER_CREATED')) {
                 define('FUE_ORDER_CREATED', true);
             }
         }
     }
 }