/**
  * Run the $email through several checks and return true if it passes the conditional validation
  *
  * @param FUE_Email $email
  * @param WC_Order  $order
  * @return bool
  */
 protected function customer_email_matches_order($email, $order)
 {
     $wpdb = Follow_Up_Emails::instance()->wpdb;
     $meta = maybe_unserialize($email->meta);
     // check for order total triggers first and
     // filter out emails that doesn't match the trigger conditions
     if ($email->trigger == 'order_total_above') {
         if (!isset($meta['order_total_above']) || $order->order_total < $meta['order_total_above']) {
             return false;
         }
     } elseif ($email->trigger == 'order_total_below') {
         if (!isset($meta['order_total_below']) || $order->order_total > $meta['order_total_below']) {
             return false;
         }
     } elseif ($email->trigger == 'total_orders') {
         $mode = $meta['total_orders_mode'];
         $requirement = $meta['total_orders'];
         if (isset($meta['one_time']) && $meta['one_time'] == 'yes') {
             // get the correct email address
             if (WC_FUE_Compatibility::get_order_user_id($order) > 0) {
                 $user = new WP_User(WC_FUE_Compatibility::get_order_user_id($order));
                 $user_email = $user->user_email;
             } else {
                 $user_email = $order->billing_email;
             }
             $search = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*)\n                    FROM {$wpdb->prefix}followup_email_orders\n                    WHERE email_id = %d\n                    AND user_email = %s", $email->id, $user_email));
             if ($search > 0) {
                 return false;
             }
         }
         // get user's total number of orders
         $customer = fue_get_customer(WC_FUE_Compatibility::get_order_user_id($order), $order->billing_email);
         $num_orders = 0;
         if ($customer) {
             $num_orders = $customer->total_orders;
         }
         if ($mode == 'less than' && $num_orders >= $requirement) {
             return false;
         } elseif ($mode == 'equal to' && $num_orders != $requirement) {
             return false;
         } elseif ($mode == 'greater than' && $num_orders <= $requirement) {
             return false;
         }
     } elseif ($email->trigger == 'total_purchases') {
         $mode = $meta['total_purchases_mode'];
         $requirement = $meta['total_purchases'];
         if (isset($meta['one_time']) && $meta['one_time'] == 'yes') {
             // get the correct email address
             if (WC_FUE_Compatibility::get_order_user_id($order) > 0) {
                 $user = new WP_User(WC_FUE_Compatibility::get_order_user_id($order));
                 $user_email = $user->user_email;
             } else {
                 $user_email = $order->billing_email;
             }
             $search = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*)\n                            FROM {$wpdb->prefix}followup_email_orders\n                            WHERE email_id = %d\n                            AND user_email = %s", $email->id, $user_email));
             if ($search > 0) {
                 return false;
             }
         }
         // get user's total amount of purchases
         if (WC_FUE_Compatibility::get_order_user_id($order) > 0) {
             $purchases = $wpdb->get_var($wpdb->prepare("SELECT total_purchase_price FROM {$wpdb->prefix}followup_customers WHERE user_id = %d", WC_FUE_Compatibility::get_order_user_id($order)));
         } else {
             $purchases = $wpdb->get_var($wpdb->prepare("SELECT total_purchase_price FROM {$wpdb->prefix}followup_customers WHERE email_address = %s", $order->billing_email));
         }
         if ($mode == 'less than' && $purchases >= $requirement) {
             return false;
         } elseif ($mode == 'equal to' && $purchases != $requirement) {
             return false;
         } elseif ($mode == 'greater than' && $purchases <= $requirement) {
             return false;
         }
     } elseif ($email->interval_type == 'purchase_above_one') {
         // look for duplicate emails
         if (WC_FUE_Compatibility::get_order_user_id($order) > 0) {
             $wp_user = new WP_User(WC_FUE_Compatibility::get_order_user_id($order));
             $user_email = $wp_user->user_email;
         } else {
             $user_email = $order->billing_email;
         }
         $num = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*)\n                FROM {$wpdb->prefix}followup_email_orders\n                WHERE email_id = %d\n                AND user_email = %s", $email->id, $user_email));
         if ($num > 0) {
             return false;
         }
     }
     return true;
 }
 /**
  * Get the orders matching the conditions of the storewide email
  * @param FUE_Email $email
  * @return array
  */
 private function get_orders_for_storewide_email($email)
 {
     $wpdb = Follow_Up_Emails::instance()->wpdb;
     $trigger = $email->trigger;
     $orders = array();
     if ($trigger == 'cart') {
         // cart is an unsupported trigger
         return $orders;
     }
     if (in_array($email->trigger, $this->get_order_statuses())) {
         // count the number of orders matching the email's order status trigger
         // and exclude those Order IDs that are in the email queue, sent or unsent
         if (WC_FUE_Compatibility::is_wc_version_gte_2_2()) {
             $status = 'wc-' . $email->trigger;
             $orders = $wpdb->get_col($wpdb->prepare("SELECT ID\n                        FROM {$wpdb->posts} p\n                        WHERE p.post_status = %s\n                        AND (\n                            SELECT COUNT(id)\n                            FROM {$wpdb->prefix}followup_email_orders\n                            WHERE order_id = p.ID\n                            AND email_id = %d\n                        ) = 0", $status, $email->id));
         } else {
             $excluded_ids = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT order_id\n                    FROM {$wpdb->prefix}followup_email_orders\n                    WHERE order_id > 0\n                    AND email_id = %d", $email->id));
             $orders = get_posts(array('post_status' => 'publish', 'tax_query' => array(array('taxonomy' => 'shop_order_status', 'field' => 'slug', 'terms' => $email->trigger)), 'post__not_in' => $excluded_ids, 'fields' => 'ids', 'posts_per_page' => -1));
         }
         // filter out the orders that don't match the email's product or category filter
         if ($email->product_id) {
             $order_ids = implode(',', $orders);
             $orders = $wpdb->get_col($wpdb->prepare("SELECT order_id\n                    FROM {$wpdb->prefix}followup_order_items\n                WHERE order_id IN ({$order_ids})\n                    AND (\n                        product_id = %d\n                        OR\n                        variation_id = %d\n                    )", $email->product_id, $email->product_id));
         }
         if ($email->category_id) {
             $order_ids = implode(',', $orders);
             $orders = $wpdb->get_col($wpdb->prepare("SELECT order_id\n                    FROM {$wpdb->prefix}followup_order_categories\n                    WHERE order_id IN ({$order_ids})\n                    AND category_id = %d", $email->category_id));
         }
         // remove orders with products/categories that are in the email exclusions list
         if (!empty($email->meta['excluded_categories'])) {
             foreach ($orders as $idx => $order_id) {
                 if (in_array($order_id, $email->meta['excluded_categories'])) {
                     unset($orders[$idx]);
                 }
             }
             // reset the indices
             $orders = array_merge($orders, array());
         }
         if (!empty($email->meta['excluded_customers_products'])) {
             // exclude orders from customers who have purchased any one of these products
             $product_ids = implode(',', $email->meta['excluded_customers_products']);
             foreach ($orders as $idx => $order_id) {
                 $order = WC_FUE_Compatibility::wc_get_order($order_id);
                 $user_id = WC_FUE_Compatibility::get_order_user_id($order);
                 if ($user_id) {
                     $customer = fue_get_customer($user_id);
                 } else {
                     $customer = fue_get_customer(0, $order->billing_email);
                 }
                 if (!$customer) {
                     continue;
                 }
                 $sql = "SELECT COUNT(*)\n                                FROM {$wpdb->prefix}followup_order_items i\n                                WHERE o.followup_customer_id = %d\n                                AND o.order_id = i.order_id\n                                AND (\n                                    i.product_id IN ( {$product_ids} )\n                                    OR\n                                    i.variation_id IN ( {$product_ids} )\n                                )";
                 $found = $wpdb->get_var($wpdb->prepare($sql, $customer->id));
                 if ($found > 0) {
                     unset($orders[$idx]);
                 }
             }
         }
         if (!empty($email->meta['excluded_customers_categories'])) {
             $product_ids = implode(',', $email->meta['excluded_customers_products']);
             foreach ($orders as $idx => $order_id) {
                 $order = WC_FUE_Compatibility::wc_get_order($order_id);
                 $user_id = WC_FUE_Compatibility::get_order_user_id($order);
                 if ($user_id) {
                     $customer = fue_get_customer($user_id);
                 } else {
                     $customer = fue_get_customer(0, $order->billing_email);
                 }
                 if (!$customer) {
                     continue;
                 }
                 $sql = "SELECT COUNT(*)\n                            FROM {$wpdb->prefix}followup_order_items i, {$wpdb->prefix}followup_customer_orders o\n                            WHERE o.followup_customer_id = %d\n                            AND o.order_id = i.order_id\n                            AND (\n                                i.product_id IN ( {$product_ids} )\n                                OR\n                                i.variation_id IN ( {$product_ids} )\n                            )";
                 $found = $wpdb->get_var($wpdb->prepare($sql, $customer->id));
                 if ($found > 0) {
                     unset($orders[$idx]);
                 }
             }
         }
     } elseif ($trigger == 'first_purchase') {
         // get the order IDs of customers with only 1 order
         $customer_orders = $wpdb->get_col("SELECT order_id\n                    FROM {$wpdb->prefix}followup_customer_orders\n                    GROUP BY followup_customer_id\n                    HAVING COUNT(followup_customer_id) = 1");
         if (count($customer_orders) > 0) {
             $queue_orders = $wpdb->get_col($wpdb->prepare("SELECT order_id\n                    FROM {$wpdb->prefix}followup_email_orders\n                    WHERE order_id IN (" . implode(',', $customer_orders) . ")\n                    AND email_id = %d", $email->id));
             // exclude orders that are already in the queue
             foreach ($customer_orders as $customer_order) {
                 if (!in_array($customer_order, $queue_orders)) {
                     $orders[] = $customer_order;
                 }
             }
         }
     } elseif ($trigger == 'product_purchase_above_one') {
         // Get the orders of customers with more than 1 order
         $customers = $wpdb->get_col("SELECT followup_customer_id\n                    FROM {$wpdb->prefix}followup_customer_orders\n                    GROUP BY followup_customer_id\n                    HAVING COUNT(followup_customer_id) > 1");
         foreach ($customers as $customer_id) {
             $customer_orders = $wpdb->get_col($wpdb->prepare("SELECT order_id\n                        FROM {$wpdb->prefix}followup_customer_orders\n                        WHERE followup_customer_id = %d\n                        ORDER BY order_id ASC", $customer_id));
             if (count($customer_orders) > 0) {
                 // drop the customer's first order
                 $customer_orders = array_slice($customer_orders, 1);
                 $queue_orders = $wpdb->get_col($wpdb->prepare("SELECT order_id\n                        FROM {$wpdb->prefix}followup_email_orders\n                        WHERE order_id IN (" . implode(',', $customer_orders) . ")\n                        AND email_id = %d", $email->id));
                 // exclude orders that are already in the queue
                 foreach ($customer_orders as $customer_order) {
                     if (!in_array($customer_order, $queue_orders)) {
                         $orders[] = $customer_order;
                     }
                 }
             }
         }
     }
     // Remove the orders that match the email's send_once property
     $email_meta = $email->meta;
     $adjust_date = false;
     if (!empty($email_meta)) {
         if (isset($email_meta['adjust_date']) && $email_meta['adjust_date'] == 'yes') {
             $adjust_date = true;
         }
         foreach ($orders as $idx => $order_id) {
             $order = WC_FUE_Compatibility::wc_get_order($order_id);
             // send email only once per customer
             if (isset($email_meta['one_time']) && $email_meta['one_time'] == 'yes') {
                 $count_sent = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*)\n                    FROM {$wpdb->prefix}followup_email_orders\n                    WHERE `user_email` = %s\n                    AND `email_id` = %d", $order->billing_email, $email->id));
                 if ($count_sent > 0) {
                     // do not send more of the same emails to this user
                     unset($orders[$idx]);
                     continue;
                 }
             }
             // adjust date only applies to non-guest orders
             if ($adjust_date && $order->customer_user > 0) {
                 // check for similar existing and unsent email orders
                 // and adjust the date to send instead of inserting a duplicate row
                 $similar_emails = $this->fue->scheduler->get_items(array('email_id' => $email->id, 'user_id' => $order->customer_user, 'product_id' => $email->product_id, 'is_cart' => 0, 'is_sent' => 0));
                 if (count($similar_emails) > 0) {
                     unset($orders[$idx]);
                     continue;
                 }
             }
         }
         // reset the indices
         $orders = array_values($orders);
     }
     return $orders;
 }
Esempio n. 3
0
/**
 * Get the FUE Customer based on user ID or email address used in the $order
 *
 * @param int|WC_Order $order
 * @return object
 * @since 4.1
 */
function fue_get_customer_from_order($order)
{
    if (is_numeric($order)) {
        $order = WC_FUE_Compatibility::wc_get_order($order);
    }
    return fue_get_customer(WC_FUE_Compatibility::get_order_user_id($order), $order->billing_email);
}