/**
  * Change the send date of the email for 'before_renewal' and 'before_expire' triggers
  * @param array $insert
  * @param FUE_Email $email
  * @return array
  */
 public function modify_insert_send_date($insert, $email)
 {
     if ($email->type != 'subscription') {
         return $insert;
     }
     $order_id = $insert['order_id'];
     $order = WC_FUE_Compatibility::wc_get_order($order_id);
     if (!WC_Subscriptions_Order::order_contains_subscription($order)) {
         return $insert;
     }
     $subs_key = WC_Subscriptions_Manager::get_subscription_key($order_id);
     if ($email->trigger == 'subs_before_renewal') {
         $renewal_date = WC_Subscriptions_Manager::get_next_payment_date($subs_key);
         if (!$renewal_date) {
             // return false to tell FUE to skip importing this email/order
             return false;
         }
         // convert to local time
         $local_renewal_date = get_date_from_gmt($renewal_date, 'U');
         $add = FUE_Sending_Scheduler::get_time_to_add($email->interval, $email->duration);
         $insert['send_on'] = $local_renewal_date - $add;
     } elseif ($email->trigger == 'subs_before_expire') {
         $expiry_date = WC_Subscriptions_Manager::get_subscription_expiration_date($subs_key);
         if (!$expiry_date) {
             return false;
         }
         // convert to local time
         $expiry_timestamp = get_date_from_gmt($expiry_date, 'U');
         $add = FUE_Sending_Scheduler::get_time_to_add($email->interval, $email->duration);
         $insert['send_on'] = $expiry_timestamp - $add;
     }
     // Add the subscription key if it is not present in the meta
     if (!isset($insert['meta']) || empty($insert['meta']['subs_key'])) {
         $insert['meta']['subs_key'] = $subs_key;
     }
     return $insert;
 }
예제 #2
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));
             }
         }
     }
 }
예제 #3
0
    <thead>
        <tr>
            <th class="order-number"><span class="nobr"><?php 
    _e('Order', 'woocommerce');
    ?>
</span></th>
            <th class="actions"><span class="nobr"><?php 
    _e('Actions', 'follow_up_emails');
    ?>
</span></th>
        </tr>
    </thead>
    <tbody>
        <?php 
    foreach ($emails as $email) {
        $order = WC_FUE_Compatibility::wc_get_order($email->order_id);
        if (function_exists('wc_get_endpoint_url')) {
            $order_url = wc_get_endpoint_url('view-order', $email->order_id, wc_get_page_permalink('myaccount'));
        } else {
            $order_url = add_query_arg('order', $email->order_id, get_permalink(woocommerce_get_page_id('view_order')));
        }
        ?>
        <tr>
            <td class="order-number">
                <a href="<?php 
        echo esc_url($order_url);
        ?>
">
                    <?php 
        echo $order->get_order_number();
        ?>
 /**
  * Return a link to the order screen
  * @param array $item
  * @return string
  */
 public function get_order_value($item)
 {
     if (empty($item['order_id'])) {
         return '-';
     }
     $order = WC_FUE_Compatibility::wc_get_order($item['order_id']);
     if ($order instanceof WC_Order) {
         return sprintf(__('<a href="%s">%s</a>', 'follow_up_emails'), 'post.php?post=' . $item['order_id'] . '&action=edit', $order->get_order_number());
     }
     return '-';
 }
예제 #5
0
 /**
  * Get the URL of the My Account page. If Sensei is installed, it's the Dashboard page.
  * @return string
  */
 public static function get_account_url()
 {
     if (self::is_sensei_installed()) {
         return get_permalink(get_option('woothemes-sensei_user_dashboard_page_id', -1));
     } elseif (self::is_woocommerce_installed()) {
         return get_permalink(WC_FUE_Compatibility::wc_get_page_id('myaccount'));
     }
     return apply_filters('fue_get_account_url', get_bloginfo('url'));
 }
 /**
  * Get replacement data for product and reminder emails
  *
  * @param array     $replacements
  * @param array     $email_data
  * @param object    $queue_item
  * @param FUE_Email $email
  *
  * @return array
  */
 private function get_replacement_data_for_product($replacements, $email_data, $queue_item, $email)
 {
     $categories = '';
     $item_name = '';
     $item_price = '';
     if (!empty($queue_item->product_id)) {
         $item = WC_FUE_Compatibility::wc_get_product($queue_item->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_url = FUE_Sending_Mailer::create_email_url($queue_item->id, $queue_item->id, $email_data['user_id'], $email_data['email_to'], get_permalink($queue_item->product_id));
         $item_name = '<a href="' . $item_url . '">' . get_the_title($queue_item->product_id) . '</a>';
         $order = WC_FUE_Compatibility::wc_get_order($queue_item->order_id);
         if ($order instanceof WC_Order) {
             foreach ($order->get_items() as $order_item) {
                 if ($order_item['product_id'] == $queue_item->product_id) {
                     $replacements['item_quantity'] = $order_item['qty'];
                     break;
                 }
             }
         }
         $item_price = strip_tags($item->get_price_html());
     }
     $replacements['item_name'] = $item_name;
     $replacements['item_price'] = $item_price;
     return $replacements;
 }
 /**
  * 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;
 }
예제 #8
0
            <td><?php 
        echo $row->user_email;
        ?>
</td>
            <td>
                <?php 
        if ($row->product_id > 0) {
            echo '<a href="post.php?post=' . $row->product_id . '&action=edit">' . get_the_title($row->product_id) . '</a>';
        } else {
            echo '-';
        }
        ?>
            </td>
            <td>
                <?php 
        if ($row->order_id > 0 && ($order = WC_FUE_Compatibility::wc_get_order($row->order_id))) {
            echo '<a href="post.php?post=' . $row->order_id . '&action=edit">' . $order->get_order_number() . '</a>';
        } else {
            echo '-';
        }
        ?>
            </td>
            <td>
                <?php 
        $email_trigger = '-';
        if ($email) {
            $email_trigger = $email->get_trigger_string();
        }
        echo apply_filters('fue_interval_str', $email_trigger, $email);
        ?>
            </td>
 /**
  * Register styles and scripts used in rendering the Admin UI
  */
 public function admin_scripts()
 {
     $page = isset($_GET['page']) ? $_GET['page'] : '';
     if ($page == 'followup-emails' || $page == 'followup-emails-settings' || $page == 'followup-emails-queue') {
         if (WC_FUE_Compatibility::is_wc_version_gt('2.1')) {
             $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';
             wp_register_script('ajax-chosen', WC()->plugin_url() . '/assets/js/chosen/ajax-chosen.jquery' . $suffix . '.js', array('jquery', 'chosen'), WC()->version);
             wp_register_script('chosen', WC()->plugin_url() . '/assets/js/chosen/chosen.jquery' . $suffix . '.js', array('jquery'), WC()->version);
         } else {
             // For WC < 2.1
             woocommerce_admin_scripts();
         }
         wp_enqueue_script('fue-queue', FUE_TEMPLATES_URL . '/js/queue.js', array('jquery', 'chosen'), FUE_VERSION);
         wp_enqueue_script('woocommerce_admin');
         wp_enqueue_script('farbtastic');
         wp_enqueue_script('ajax-chosen');
         wp_enqueue_script('chosen');
         wp_enqueue_script('jquery-ui-sortable');
         wp_enqueue_script('jquery-ui-autocomplete', null, array('jquery-ui-core'));
         ?>
         <style type="text/css">
             .chzn-choices li.search-field .default {
                 width: auto !important;
             }
             select option[disabled] {display:none;}
         </style>
         <?php 
         wp_enqueue_style('woocommerce_admin_styles', WC()->plugin_url() . '/assets/css/admin.css');
         if (!empty($_GET['tab']) && $_GET['tab'] == 'order_import') {
             wp_enqueue_script('jquery-ui-progressbar', false, array('jquery', 'jquery-ui'));
             wp_enqueue_script('fue_wc_order_import', FUE_TEMPLATES_URL . '/js/wc_order_import.js', array('jquery', 'jquery-ui-progressbar'), FUE_VERSION);
         }
     } elseif ($page == 'followup-emails-form' || $page == 'followup-emails-reports') {
         if ($page == 'followup-emails-form') {
             wp_enqueue_script('fue-form-woocommerce', plugins_url('templates/js/email-form-woocommerce.js', FUE_FILE), array('jquery'), FUE_VERSION);
         }
         wp_enqueue_script('select2');
         wp_enqueue_style('select2');
         wp_enqueue_script('woocommerce_admin');
         wp_enqueue_script('farbtastic');
         wp_enqueue_script('jquery-ui-sortable');
         wp_enqueue_script('jquery-ui-core', null, array('jquery'));
         wp_enqueue_script('jquery-ui-datepicker', null, array('jquery-ui-core'));
         wp_enqueue_script('jquery-ui-autocomplete', null, array('jquery-ui-core'));
         wp_enqueue_style('woocommerce_admin_styles', WC()->plugin_url() . '/assets/css/admin.css');
         wp_enqueue_style('jquery-ui-css', '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/themes/base/jquery-ui.css');
     }
     $screen = get_current_screen();
     if ($screen->id == 'follow_up_email') {
         wp_enqueue_script('fue-form-woocommerce', plugins_url('templates/js/email-form-woocommerce.js', FUE_FILE), array('jquery'), FUE_VERSION);
         wp_enqueue_style('woocommerce_admin_styles', WC()->plugin_url() . '/assets/css/admin.css');
     }
     wp_enqueue_script('wc-product-search', plugins_url('templates/js/fue-select.js', FUE_FILE), array('jquery', 'select2'), FUE_VERSION);
 }
 /**
  * Checks if there is a match in the order's status and the email's trigger
  *
  * @param int|WC_Order  $order
  * @param int|FUE_Email $email
  * @return bool
  */
 private function order_status_matches_email_trigger($order, $email)
 {
     if (is_numeric($order)) {
         $order = WC_FUE_Compatibility::wc_get_order($order);
     }
     if (is_numeric($email)) {
         $email = new FUE_Email($email);
     }
     return WC_FUE_Compatibility::get_order_status($order) == $email->trigger;
 }
 /**
  * Queue emails after an order is marked as completed
  * @param int $order_id
  */
 public function set_reminders($order_id)
 {
     global $woocommerce, $wpdb;
     // load reminder emails
     $emails = fue_get_emails('wootickets', FUE_Email::STATUS_ACTIVE, array('meta_query' => array(array('key' => '_interval_type', 'value' => array('before_tribe_event_starts', 'after_tribe_event_ends'), 'compare' => 'IN'))));
     $tickets = array();
     if (empty($emails)) {
         return;
     }
     $has_tickets = get_post_meta($order_id, '_tribe_has_tickets', true);
     $order = WC_FUE_Compatibility::wc_get_order($order_id);
     $items = $order->get_items();
     foreach ($items as $item) {
         $ticket_id = isset($item['id']) ? $item['id'] : $item['product_id'];
         // if $item is a ticket, load the event where the ticket is attached to
         $event_id = get_post_meta($ticket_id, '_tribe_wooticket_for_event', true);
         if (!$event_id) {
             continue;
         }
         if (!in_array($ticket_id, $tickets)) {
             $tickets[] = $ticket_id;
         }
     }
     $now = current_time('timestamp');
     foreach ($emails as $email) {
         $interval = (int) $email->interval_num;
         $add = FUE_Sending_Scheduler::get_time_to_add($interval, $email->interval_duration);
         foreach ($tickets as $ticket_id) {
             // if this email is for a specific ticket, make sure the IDs match
             if (!empty($email->product_id) && $email->product_id != $ticket_id) {
                 continue;
             }
             // check for category matching
             if (!empty($email->category_id)) {
                 $ticket_terms = get_the_terms($ticket_id, 'product_cat');
                 $product_categories = array();
                 if ($ticket_terms && !is_wp_error($ticket_terms)) {
                     foreach ($ticket_terms as $ticket_term) {
                         $product_categories[$ticket_term->term_id] = $ticket_term->name;
                     }
                 }
                 if (!array_key_exists($email->category_id, $product_categories)) {
                     continue;
                 }
             }
             $event_id = get_post_meta($ticket_id, '_tribe_wooticket_for_event', true);
             if ($email->interval_type == 'before_tribe_event_starts') {
                 $start = get_post_meta($event_id, '_EventStartDate', true);
                 if (empty($start)) {
                     continue;
                 }
                 $start = strtotime($start);
                 // check if a limit is in place
                 $email_meta = maybe_unserialize($email->meta);
                 if (isset($email_meta['tribe_limit'], $email_meta['tribe_limit_days']) && !empty($email_meta['tribe_limit_days'])) {
                     $days = ($start - $now) / 86400;
                     if ($days <= $email_meta['tribe_limit_days']) {
                         // $days is within limit - skip
                         continue;
                     }
                 }
                 $send_on = $start - $add;
                 // if send_on is in the past, do not queue it
                 if ($now > $send_on) {
                     continue;
                 }
             } else {
                 $end = get_post_meta($event_id, '_EventEndDate', true);
                 if (empty($end)) {
                     continue;
                 }
                 $end = strtotime($end);
                 $send_on = $end + $add;
                 // if send_on is in the past, do not queue it
                 if ($now > $send_on) {
                     continue;
                 }
             }
             $insert = array('user_id' => $order->user_id, 'order_id' => $order_id, 'product_id' => $ticket_id, 'email_id' => $email->id, 'send_on' => $send_on);
             FUE_Sending_Scheduler::queue_email($insert, $email);
         }
     }
 }
예제 #12
0
 /**
  * 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);
             }
         }
     }
 }
예제 #13
0
 function sfn_get_product($product_id)
 {
     _deprecated_function('sfn_get_product', '3.7', 'WC_FUE_Compatibility::wc_get_product');
     return WC_FUE_Compatibility::wc_get_product($product_id);
 }
예제 #14
0
 /**
  * Inject the pixel tracker image to the email body before it is sent
  *
  * @param string    $message
  * @param FUE_Email $email
  * @param object    $email_order
  *
  * @return string The modified email body
  */
 public function inject_pixel_tracker($message, $email, $email_order)
 {
     global $wpdb;
     $user_id = 0;
     if ($email_order->order_id != 0) {
         // order
         $order = WC_FUE_Compatibility::wc_get_order($email_order->order_id);
         if (isset($order->user_id) && $order->user_id > 0) {
             $user_id = $order->user_id;
             $wp_user = new WP_User($order->user_id);
             $email_to = $wp_user->user_email;
         } else {
             $email_to = $order->billing_email;
         }
     } else {
         $order = false;
         $wp_user = new WP_User($email_order->user_id);
         $user_id = $email_order->user_id;
         $email_to = $wp_user->user_email;
     }
     $qstring = base64_encode('oid=' . $email_order->id . '&eid=' . $email->id . '&user_email=' . $email_to . '&user_id=' . $user_id);
     $px_url = add_query_arg('fuepx', 1, add_query_arg('data', $qstring, site_url()));
     $message .= '<img src="' . $px_url . '" height="1" width="1" />';
     return $message;
 }
예제 #15
0
 /**
  * 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)
 {
     global $wpdb;
     $order = $queue_item->order_id != 0 ? WC_FUE_Compatibility::wc_get_order($queue_item->order_id) : false;
     $email = new FUE_Email($queue_item->email_id);
     $send_coupon = $email->send_coupon;
     $email_coupon_id = $email->coupon_id;
     if (!$email->exists() || !$send_coupon) {
         return $variables;
     }
     if ($queue_item->order_id != 0) {
         // order
         $order = WC_FUE_Compatibility::wc_get_order($queue_item->order_id);
         if (isset($order->user_id) && $order->user_id > 0) {
             $wp_user = new WP_User($order->user_id);
             $email_to = $wp_user->user_email;
         } else {
             $email_to = $order->billing_email;
         }
     } else {
         $wp_user = new WP_User($queue_item->user_id);
         $email_to = $wp_user->user_email;
     }
     $coupon_code = '';
     $coupon = false;
     if ($send_coupon == 1 && $email_coupon_id != 0) {
         $coupon = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}followup_coupons WHERE `id` = %d", $email_coupon_id));
         if (!$coupon) {
             $variables['coupon_code'] = '';
             $queue_item->add_note(sprintf('Warning: The email coupon (ID #%s) could not be found', $email_coupon_id));
             return $variables;
         }
         $coupon_code = FUE_Coupons::add_prefix(self::generate_coupon_code(), $coupon, $order);
         $coupon_array = array('post_title' => $coupon_code, 'post_author' => 1, 'post_date' => date("Y-m-d H:i:s"), 'post_status' => 'publish', 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_name' => $coupon_code, 'post_parent' => 0, 'menu_order' => 0, 'post_type' => 'shop_coupon');
         $coupon_id = wp_insert_post($coupon_array);
         $wpdb->query("UPDATE {$wpdb->prefix}posts SET post_status = 'publish' WHERE ID = {$coupon_id}");
         $expiry = '';
         if ($coupon->expiry_value > 0 && !empty($coupon->expiry_type)) {
             $exp = $coupon->expiry_value . ' ' . $coupon->expiry_type;
             $now = current_time('mysql');
             $ts = strtotime("{$now} +{$exp}");
             if ($ts !== false) {
                 $expiry = date('Y-m-d', $ts);
             }
         }
         update_post_meta($coupon_id, 'discount_type', $coupon->coupon_type);
         update_post_meta($coupon_id, 'coupon_amount', $coupon->amount);
         update_post_meta($coupon_id, 'individual_use', $coupon->individual == 0 ? 'no' : 'yes');
         update_post_meta($coupon_id, 'product_ids', $coupon->product_ids);
         update_post_meta($coupon_id, 'exclude_product_ids', $coupon->exclude_product_ids);
         update_post_meta($coupon_id, 'usage_limit', $coupon->usage_limit);
         update_post_meta($coupon_id, 'usage_limit_per_user', $coupon->usage_limit_per_user);
         update_post_meta($coupon_id, 'expiry_date', $expiry);
         update_post_meta($coupon_id, 'apply_before_tax', $coupon->before_tax == 0 ? 'no' : 'yes');
         update_post_meta($coupon_id, 'free_shipping', $coupon->free_shipping == 0 ? 'no' : 'yes');
         update_post_meta($coupon_id, 'exclude_sale_items', $coupon->exclude_sale_items == 0 ? 'no' : 'yes');
         update_post_meta($coupon_id, 'product_categories', maybe_unserialize($coupon->product_categories));
         update_post_meta($coupon_id, 'exclude_product_categories', maybe_unserialize($coupon->exclude_product_categories));
         update_post_meta($coupon_id, 'minimum_amount', $coupon->minimum_amount);
         update_post_meta($coupon_id, 'maximum_amount', $coupon->maximum_amount);
         $product_categories = '';
         $exclude_product_categories = '';
         if (!empty($coupon->product_categories)) {
             $product_categories = unserialize($coupon->product_categories);
         }
         update_post_meta($coupon_id, 'product_categories', $product_categories);
         if (!empty($coupon->exclude_product_categories)) {
             $exclude_product_categories = unserialize($coupon->exclude_product_categories);
         }
         update_post_meta($coupon_id, 'exclude_product_categories', $exclude_product_categories);
         $wpdb->query($wpdb->prepare("UPDATE {$wpdb->prefix}followup_coupons SET `usage_count` = `usage_count` + 1 WHERE `id` = %d", $coupon->id));
         FUE_Coupons::coupon_log($coupon_id, $coupon->coupon_name, $email->name, $email_to, $coupon_code);
         // record into the email_orders table
         $wpdb->query($wpdb->prepare("UPDATE `{$wpdb->prefix}followup_email_order_coupons` SET `coupon_name` = %s, `coupon_code` = %s WHERE `email_order_id` = %d", $coupon->coupon_name, $coupon_code, $queue_item->id));
         $variables['coupon_code'] = $coupon_code;
     }
     return $variables;
 }
 /**
  * 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);
 }
예제 #17
0
    <p>
        <strong><?php 
_e('Select the product that, when bought or added to the cart, will trigger this follow-up email.', 'follow_up_emails');
?>
</strong>
    </p>

    <label for="product_ids"><?php 
_e('Product', 'follow_up_emails');
?>
</label>
    <?php 
$product_id = !empty($email->product_id) ? $email->product_id : '';
$product_name = '';
if (!empty($product_id)) {
    $product = WC_FUE_Compatibility::wc_get_product($product_id);
    if ($product) {
        $product_name = wp_kses_post($product->get_formatted_name());
    }
}
?>
    <input
        type="hidden"
        id="product_id"
        name="product_id"
        class="ajax_select2_products_and_variations"
        data-multiple="false"
        data-placeholder="<?php 
_e('Search for a product&hellip;', 'woocommerce');
?>
"
 public static function report_order_str($str, $report)
 {
     if ($report->order_id != 0) {
         $order = WC_FUE_Compatibility::wc_get_order($report->order_id);
         $str = '<a href="' . get_admin_url() . 'post.php?post=' . $report->order_id . '&action=edit">View Order</a>';
     }
     return $str;
 }