/**
  * Queue emails after an RMA's status changes
  *
  * @param int       $request_id
  * @param string    $status
  */
 public function status_updated($request_id, $status)
 {
     global $wpdb;
     $order_id = get_post_meta($request_id, '_order_id', true);
     $triggers = array('warranty_status');
     $emails = fue_get_emails('any', FUE_Email::STATUS_ACTIVE, array('meta_query' => array(array('key' => '_interval_type', 'value' => 'warranty_status'))));
     foreach ($emails as $email) {
         $interval = (int) $email->interval_num;
         $insert = array('send_on' => $email->get_send_timestamp(), 'email_id' => $email->id, 'user_id' => 0, 'order_id' => $order_id, 'is_cart' => 0);
         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);
             }
         }
     }
     FUE_Addon_Woocommerce::create_email_orders($triggers, $order_id);
 }
 /**
  * Test will pass if the customer has bought from all of the categories specified
  *
  * @param $item
  * @param $condition
  * @return bool|WP_Error
  */
 public function test_bought_categories_condition($item, $condition)
 {
     $wpdb = Follow_Up_Emails::instance()->wpdb;
     $categories = array_filter(array_map('absint', $condition['categories']));
     $customer = fue_get_customer_from_order($item->order_id);
     $result = true;
     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($categories)) {
         foreach ($categories as $category) {
             if (!$this->fue_wc->customer_purchased_from_category($customer, $category)) {
                 // no purchases found for this product
                 $wc_category = get_term($category, 'product_cat');
                 return new WP_Error('fue_email_conditions', sprintf(__('Customer has not purchased from a required category (%s)', 'follow_up_emails'), $wc_category->name));
             }
         }
     }
     return true;
 }
"
        data-action="fue_wc_json_search_subscription_products"
        data-allow_clear="true"
        value="<?php 
echo $product_id;
?>
"
        data-selected="<?php 
echo esc_attr($product_name);
?>
"
    >
</p>
<?php 
$display = 'display: none;';
$has_variations = !empty($email->product_id) && FUE_Addon_Woocommerce::product_has_children($email->product_id) ? true : false;
if ($has_variations) {
    $display = 'display: inline-block;';
}
?>
<p class="form-field product_include_variations" style="<?php 
echo $display;
?>
">
    <input type="checkbox" name="meta[include_variations]" id="include_variations" value="yes" <?php 
if (isset($email->meta['include_variations']) && $email->meta['include_variations'] == 'yes') {
    echo 'checked';
}
?>
 />
    <label for="include_variations" class="inline"><?php 
Example #4
0
 /**
  * Order importer for WooCommerce. Process 10 orders at a time
  */
 public static function wc_order_import()
 {
     $wpdb = Follow_Up_Emails::instance()->wpdb;
     if (empty($_POST['cmd'])) {
         self::send_response(array('error' => 'CMD is missing'));
     }
     $cmd = $_POST['cmd'];
     $email_id = !empty($_POST['email_id']) ? $_POST['email_id'] : '';
     if ($email_id) {
         $fue_wc = Follow_Up_Emails::instance()->fue_wc;
         if ($cmd == 'start') {
             if (is_array($email_id)) {
                 $total_orders = 0;
                 foreach ($email_id as $id) {
                     $email = new FUE_Email($id);
                     $email_orders = $fue_wc->count_orders_for_email($email);
                     $total_orders += $email_orders;
                     if ($email_orders == 0) {
                         delete_post_meta($id, '_import_order_flag');
                     }
                 }
                 self::send_response(array('total_orders' => $total_orders));
             } else {
                 $email = new FUE_Email($email_id);
                 $total_orders = $fue_wc->count_orders_for_email($email);
                 if ($total_orders == 0) {
                     delete_post_meta($email_id, '_import_order_flag');
                 }
                 self::send_response(array('total_orders' => $total_orders));
             }
         } else {
             $email = new FUE_Email($email_id);
             $results = $fue_wc->import_orders_for_email($email, 10);
             if ($results['status'] == 'completed') {
                 delete_post_meta($email_id, '_import_order_flag');
             }
             self::send_response(array('status' => $results['status'] == 'running' ? 'partial' : 'completed', 'import_data' => $results['imported'], 'remaining_orders' => $results['remaining_orders']));
         }
     } else {
         if ($cmd == 'start') {
             $tables = $wpdb->get_col("SHOW TABLES LIKE '{$wpdb->prefix}followup_order_items'");
             if (empty($tables)) {
                 self::send_response(array('error' => 'Database tables are not installed. Please deactivate then reactivate Follow Up Emails'));
             }
             if (!get_option('fue_orders_imported', false) && !get_transient('fue_importing_orders')) {
                 // First run of the import script. Clear existing data for a fresh start
                 $wpdb->query("DELETE FROM {$wpdb->prefix}followup_order_items");
                 $wpdb->query("DELETE FROM {$wpdb->prefix}followup_customers");
                 $wpdb->query("DELETE FROM {$wpdb->prefix}followup_order_categories");
                 $wpdb->query("DELETE FROM {$wpdb->prefix}followup_customer_orders");
             }
             set_transient('fue_importing_orders', true, 86400);
             $sql = "SELECT COUNT( DISTINCT p.id )\n                FROM {$wpdb->posts} p, {$wpdb->postmeta} pm\n                WHERE p.ID = pm.post_id\n                AND p.post_type = 'shop_order'\n                AND (SELECT COUNT(*) FROM {$wpdb->postmeta} pm2 WHERE p.ID = pm2.post_id AND pm2.meta_key = '_fue_recorded') = 0";
             $total_orders = $wpdb->get_var($sql);
             if ($total_orders == 0) {
                 update_option('fue_orders_imported', true);
                 delete_transient('fue_importing_orders');
             }
             self::send_response(array('total_orders' => $total_orders));
         } else {
             $sql = "SELECT DISTINCT p.ID\n                FROM {$wpdb->posts} p, {$wpdb->postmeta} pm\n                WHERE p.ID = pm.post_id\n                AND p.post_type = 'shop_order'\n                AND (SELECT COUNT(*) FROM {$wpdb->postmeta} pm2 WHERE p.ID = pm2.post_id AND pm2.meta_key = '_fue_recorded') = 0\n                LIMIT 1";
             $results = $wpdb->get_results($sql);
             if (count($results) == 0) {
                 update_option('fue_orders_imported', true);
                 delete_transient('fue_importing_orders');
                 self::send_response(array('status' => 'completed'));
             } else {
                 $imported = array();
                 foreach ($results as $row) {
                     $order = WC_FUE_Compatibility::wc_get_order($row->ID);
                     FUE_Addon_Woocommerce::record_order($order);
                     $imported[] = array('id' => $row->ID, 'status' => 'success');
                 }
                 self::send_response(array('status' => 'partial', 'import_data' => $imported));
             }
         }
     }
 }
 /**
  * Display an message on the frontend. Checks if Sensei is installed first
  * and it appends the message to $woothemes_sensei->frontend_messages. Otherwise, it falls
  * back to using WooCommerce's messaging API
  *
  * @param string $message
  */
 public static function show_message($message)
 {
     if (self::is_sensei_installed()) {
         FUE_Addon_Sensei::add_message($message);
     } elseif (self::is_woocommerce_installed()) {
         FUE_Addon_Woocommerce::add_message($message);
     } else {
         // @todo Implement a notification system in case WC and Sensei are not available
     }
 }
 /**
  * Get an array of Category IDs included in the given $order
  * @param int|WC_Order  $order
  * @return array
  */
 protected function get_category_ids_from_order($order)
 {
     $wpdb = Follow_Up_Emails::instance()->wpdb;
     if (is_numeric($order)) {
         $order = WC_FUE_Compatibility::wc_get_order($order);
     }
     if (1 != get_post_meta($order->id, '_fue_recorded', true)) {
         FUE_Addon_Woocommerce::record_order($order);
     }
     $category_ids = $wpdb->get_col($wpdb->prepare("SELECT category_id\n            FROM {$wpdb->prefix}followup_order_categories\n            WHERE order_id = %d", $order->id));
     return array_unique($category_ids);
 }
 /**
  * Product/Category selector metabox
  */
 public static function email_form_product_meta_box()
 {
     global $post;
     $email = new FUE_Email($post->ID);
     // load the categories
     $categories = get_terms('product_cat', array('order_by' => 'name', 'order' => 'ASC'));
     $has_variations = !empty($email->product_id) && FUE_Addon_Woocommerce::product_has_children($email->product_id) ? true : false;
     $storewide_type = !empty($email->meta['storewide_type']) ? $email->meta['storewide_type'] : 'all';
     include FUE_TEMPLATES_DIR . '/email-form/woocommerce/email-form.php';
 }
 /**
  * Scan through the keys of $variables and apply the replacement if one is found
  * @param array     $variables
  * @param array     $email_data
  * @param object    $queue_item
  * @param FUE_Email $email
  * @return array
  */
 protected function add_variable_replacements($variables, $email_data, $queue_item, $email)
 {
     if ($queue_item->order_id && $queue_item->product_id) {
         $order = WC_FUE_Compatibility::wc_get_order($queue_item->order_id);
         $item_id = $queue_item->product_id;
         // booking data
         $meta = maybe_unserialize($queue_item->meta);
         $booking_id = !empty($meta['booking_id']) ? $meta['booking_id'] : 0;
         if ($booking_id == 0) {
             return $variables;
         }
         /**
          * @var $booking WC_Booking
          * @var $booking_product WC_Product_Booking
          */
         $booking = get_wc_booking($booking_id);
         $booking_product = $booking->get_product();
         $booking_duration = $this->duration_to_string($booking_product->get_duration(), $booking_product->get_duration_unit());
         $booking_date = $booking->get_start_date(get_option('date_format'), '');
         $booking_time = $booking->get_start_date('', get_option('time_format'));
         $booking_amount = woocommerce_price($booking->cost);
         $booking_persons = '';
         $booking_resource = $booking->resource_id > 0 ? get_the_title($booking->resource_id) : '';
         if ($booking->has_persons()) {
             $booking_persons = '<ul>';
             foreach ($booking->get_persons() as $person_id => $num) {
                 $booking_persons .= '<li>' . get_the_title($person_id) . ': ' . $num . '</li>';
             }
             $booking_persons .= '</ul>';
         }
         $used_cats = array();
         $item_cats = '<ul>';
         $categories = get_the_terms($booking->product_id, 'product_cat');
         if (is_array($categories)) {
             foreach ($categories as $category) {
                 if (!in_array($category->term_id, $used_cats)) {
                     $item_cats .= apply_filters('fue_email_cat_list', '<li>' . $category->name . '</li>', $queue_item->id, $categories);
                     $used_cats[] = $category->term_id;
                 }
             }
         }
         $variables['item_name'] = FUE_Addon_Woocommerce::get_product_name($booking_product);
         $variables['item_category'] = $item_cats;
         $variables['booking_duration'] = $booking_duration;
         $variables['booking_date'] = $booking_date;
         $variables['booking_time'] = $booking_time;
         $variables['booking_amount'] = $booking_amount;
         $variables['booking_resource'] = $booking_resource;
         $variables['booking_persons'] = $booking_persons;
         $variables['order_billing_address'] = $order->get_formatted_billing_address();
         $variables['order_shipping_address'] = $order->get_formatted_shipping_address();
     }
     return $variables;
 }
 /**
  * Get replacement data for test emails
  *
  * @param array     $email_data
  * @param FUE_Email $email
  *
  * @return array
  */
 private function get_test_replacement_data($email_data, $email)
 {
     $email_type = $email->type;
     $order_date = '';
     $order_datetime = '';
     $order_id = $email_data['order_id'];
     $product_id = $email_data['product_id'];
     $categories = '';
     $replacements = array();
     if ($email_type == 'storewide') {
         // check if user wants to simulate email from a specific order
         if (!empty($order_id)) {
             // make sure the order exist
             $order = WC_FUE_Compatibility::wc_get_order($order_id);
             if (!$order->id) {
                 die(__('The Order ID does not exist. Please try again.'));
             }
             $order_date = date(get_option('date_format'), strtotime($order->order_date));
             $order_datetime = date(get_option('date_format') . ' ' . get_option('time_format'), strtotime($order->order_date));
             $order_id = apply_filters('woocommerce_order_number', $order->id, $order);
             $order_total = $this->get_plain_order_total($order);
             $billing_address = $order->get_formatted_billing_address();
             $shipping_address = $order->get_formatted_shipping_address();
             $used_cats = array();
             $item_list = '<ul>';
             $item_cats = '<ul>';
             $items = $order->get_items();
             $items_array = array();
             foreach ($items as $item) {
                 $item_id = isset($item['product_id']) ? $item['product_id'] : $item['id'];
                 $item_name = FUE_Addon_Woocommerce::get_product_name($item_id);
                 $items_array[] = $item_name;
                 $item_list .= '<li><a href="' . get_permalink($item_id) . '">' . $item_name . '</a></li>';
                 $cats = get_the_terms($item_id, 'product_cat');
                 if (is_array($cats) && !empty($cats)) {
                     foreach ($cats as $cat) {
                         if (!in_array($cat->term_id, $used_cats)) {
                             $item_cats .= '<li>' . $cat->name . '</li>';
                         }
                     }
                 }
             }
             $item_list .= '</ul>';
             $item_cats .= '</ul>';
             $item_list_csv = implode(', ', $items_array);
             $customer_first = $order->billing_first_name;
             $customer_last = $order->billing_last_name;
             $customer_email = $order->billing_email;
         } else {
             $order_id = '798';
             $order_total = function_exists('wc_price') ? wc_price(121.4) : woocommerce_price(121.4);
             $order_total = strip_tags($order_total);
             // check if user wants to simulate email from a specific order
             if (!empty($product_id)) {
                 $item = WC_FUE_Compatibility::wc_get_product($product_id);
                 $cats = get_the_terms($item->id, 'product_cat');
                 $item_url = get_permalink($item->id);
                 $product_name = FUE_Addon_Woocommerce::get_product_name($product_id);
                 $item_name = '<a href="' . $item_url . '">' . $product_name . '</a>';
                 $item_price = strip_tags($item->get_price_html());
                 $item_qty = 1;
                 $item_list = '<ul><li>' . $item_name . '</li></ul>';
                 $item_cats = '<ul>';
                 $item_list_csv = $product_name;
                 if (is_array($cats) && !empty($cats)) {
                     foreach ($cats as $cat) {
                         $item_cats .= '<li>' . $cat->name . '</li>';
                     }
                 }
                 $item_cats .= '</ul>';
             } else {
                 $item_list = '<ul><li><a href="#">Item 1</a></li><li><a href="#">Item 2</a></li></ul>';
                 $item_cats = '<ul><li>Category 1</li><li>Category 2</li></ul>';
                 $item_list_csv = 'Item 1, Item 2';
                 $item_price = '';
                 $item_qty = '';
             }
             $billing_address = '77 North Beach Dr., Miami, FL 35122';
             $shipping_address = '77 North Beach Dr., Miami, FL 35122';
             $customer_first = $email_data['first_name'];
             $customer_last = $email_data['last_name'];
             $customer_email = $email_data['email_to'];
             $replacements['item_price'] = $item_price;
             $replacements['item_quantity'] = $item_qty;
         }
         $replacements['order_number'] = $order_id;
         $replacements['order_date'] = $order_date;
         $replacements['order_datetime'] = $order_datetime;
         $replacements['order_billing_address'] = $billing_address;
         $replacements['order_shipping_address'] = $shipping_address;
         $replacements['dollars_spent_order'] = $order_total;
         $replacements['customer_first_name'] = $customer_first;
         $replacements['customer_last_name'] = $customer_last;
         $replacements['customer_name'] = $customer_first . ' ' . $customer_last;
         $replacements['customer_email'] = $customer_email;
         $replacements['item_name'] = $item_list;
         $replacements['item_names'] = $item_list;
         $replacements['item_names_list'] = $item_list_csv;
         $replacements['item_categories'] = $item_cats;
         $replacements['item_category'] = $item_cats;
     } elseif ($email_type == 'reminder') {
         $categories = '';
         $order_id = !empty($order_id) ? $order_id : '1100';
         $order_total = function_exists('wc_price') ? wc_price(121.4) : woocommerce_price(121.4);
         $order_total = strip_tags($order_total);
         $order_date = date(get_option('date_format'));
         $order_datetime = date(get_option('date_format') . ' ' . get_option('time_format'));
         $billing_address = '77 North Beach Dr., Miami, FL 35122';
         $shipping_address = '77 North Beach Dr., Miami, FL 35122';
         $customer_first = 'John';
         $customer_last = 'Doe';
         $customer_email = '*****@*****.**';
         if ($order_id != '1100' && ($order = WC_FUE_Compatibility::wc_get_order($order_id))) {
             $order_id = apply_filters('woocommerce_order_number', $order->id, $order);
             $order_total = $this->get_plain_order_total($order);
             $used_cats = array();
             $item_list = '<ul>';
             $item_cats = '<ul>';
             $items = $order->get_items();
             $items_array = array();
             foreach ($items as $item) {
                 $item_id = isset($item['product_id']) ? $item['product_id'] : $item['id'];
                 $item_name = FUE_Addon_Woocommerce::get_product_name($item_id);
                 $item_list .= '<li><a href="' . get_permalink($item_id) . '">' . $item_name . '</a></li>';
                 $cats = get_the_terms($item_id, 'product_cat');
                 if (is_array($cats) && !empty($cats)) {
                     foreach ($cats as $cat) {
                         if (!in_array($cat->term_id, $used_cats)) {
                             $item_cats .= '<li>' . $cat->name . '</li>';
                         }
                     }
                 }
             }
             $item_list .= '</ul>';
             $item_cats .= '</ul>';
             $item_list_csv = implode(', ', $items_array);
         }
         // check if user wants to simulate email from a specific order
         if (isset($email_data['product_id']) && !empty($email_data['product_id'])) {
             $item = WC_FUE_Compatibility::wc_get_product($email_data['product_id']);
             $cats = get_the_terms($item->id, 'product_cat');
             if (is_array($cats) && !empty($cats)) {
                 foreach ($cats as $cat) {
                     $categories .= $cat->name . ', ';
                 }
                 $categories = rtrim($categories, ', ');
             }
             $item_name = $item->get_title();
             $item_url = get_permalink($item->id);
         } else {
             $item_name = '<a href="#">Name of Product</a>';
             $categories = 'Test Category';
             $item_url = get_bloginfo('url');
         }
         $replacements['order_number'] = $order_id;
         $replacements['dollars_spent_order'] = $order_total;
         $replacements['order_date'] = $order_date;
         $replacements['order_datetime'] = $order_datetime;
         $replacements['order_billing_address'] = $billing_address;
         $replacements['order_shipping_address'] = $shipping_address;
         $replacements['customer_first_name'] = $customer_first;
         $replacements['customer_last_name'] = $customer_last;
         $replacements['customer_name'] = $customer_first . ' ' . $customer_last;
         $replacements['customer_email'] = $customer_email;
         $replacements['item_name'] = '<a href="' . $item_url . '">' . $item_name . '</a>';
         $replacements['item_names'] = $item_list;
         $replacements['item_names_list'] = $item_list_csv;
         $replacements['item_categories'] = $categories;
         $replacements['item_category'] = $categories;
     } else {
         $order_number = '1100';
         $order_date = date(get_option('date_format'));
         $order_datetime = date(get_option('date_format') . ' ' . get_option('time_format'));
         $customer_first = 'John';
         $customer_last = 'John Doe';
         $customer_email = '*****@*****.**';
         $billing_address = '77 North Beach Dr., Miami, FL 35122';
         $shipping_address = '77 North Beach Dr., Miami, FL 35122';
         $item_list = '<ul><li><a href="#">Item 1</a></li><li><a href="#">Item 2</a></li></ul>';
         $item_list_csv = 'Item 1, Item 2';
         $item_cats = '<ul><li>Category 1</li><li>Category 2</li></ul>';
         $spent_order = strip_tags(woocommerce_price(19.99));
         $spent_total = strip_tags(woocommerce_price(3250));
         $total_orders = 12;
         $last_order_date = $order_date;
         $item_name = '<a href="#">Name of Product</a>';
         $item_cat = 'Test Category';
         $item_price = '$29.95';
         $item_qty = 1;
         $replacements['order_number'] = $order_number;
         $replacements['order_date'] = $order_date;
         $replacements['order_datetime'] = $order_datetime;
         $replacements['customer_first_name'] = $customer_first;
         $replacements['customer_last_name'] = $customer_last;
         $replacements['customer_name'] = $customer_first . ' ' . $customer_last;
         $replacements['customer_email'] = $customer_email;
         $replacements['item_name'] = $item_name;
         $replacements['item_names'] = $item_list;
         $replacements['item_names_list'] = $item_list_csv;
         $replacements['item_category'] = $item_cat;
         $replacements['item_categories'] = $item_cat;
         $replacements['item_price'] = $item_price;
         $replacements['item_quantity'] = $item_qty;
         $replacements['amount_spent_order'] = $spent_order;
         $replacements['amount_spent_total'] = $spent_total;
         $replacements['number_orders'] = $total_orders;
         $replacements['last_purchase_date'] = $last_order_date;
         $replacements['order_billing_address'] = $billing_address;
         $replacements['order_shipping_address'] = $shipping_address;
     }
     return $replacements;
 }