woocommerce_model = new A2WL_Woocommerce(); $this->loader = new A2WL_Aliexpress(); } public function init_reccurences($schedules) { $schedules['a2wl_1_mins'] = array('interval' => 1 * 60, 'display' => __('Every Minute', 'ali2woo-lite')); $schedules['a2wl_5_mins'] = array('interval' => 5 * 60, 'display' => __('Every 5 Minutes', 'ali2woo-lite')); $schedules['a2wl_15_mins'] = array('interval' => 15 * 60, 'display' => __('Every 15 Minutes', 'ali2woo-lite')); return $schedules; } public function a2wl_synch_event_check_proc() { // hourly check: is a2wl_auto_update_event and a2wl_auto_synch_event exist. if no, create it. if (!wp_next_scheduled('a2wl_auto_update_event') && a2wl_get_setting('auto_update')) { $this->schedule_event(); } } public function install() { if (!wp_next_scheduled('a2wl_synch_event_check')) { wp_schedule_event(time(), 'hourly', 'a2wl_synch_event_check'); } $this->unschedule_event(); if (a2wl_get_setting('auto_update')) { $this->schedule_event(); } } public function uninstall() { $this->unschedule_event(); } public function togle_auto_update($old_value, $value, $option) { if($old_value !== $value){ $this->unschedule_event(); if ($value) { $this->schedule_event(); } } } // Cron auto update event public function auto_update_event() { if (!a2wl_get_setting('auto_update')) { return; } a2wl_init_error_handler(); try { $update_per_schedule = 100; $update_per_request = 5; $update_period_delay = 60 * 60 * 24; $product_ids = $this->woocommerce_model->get_sorted_products_ids("_a2wl_last_update", $update_per_schedule, array('value' => time() - $update_period_delay, 'compare' => '<')); $a2wl_sync_type = a2wl_get_setting('sync_type'); $product_map = array(); foreach ($product_ids as $product_id) { $product = $this->woocommerce_model->get_product_by_post_id($product_id, false); if ($product && !$product['disable_sync']) { if($a2wl_sync_type === 'price'){ $product['disable_var_quantity_change'] = true; }else if($a2wl_sync_type === 'stock'){ $product['disable_var_price_change'] = true; }else if($a2wl_sync_type === 'no'){ $product['disable_var_price_change'] = true; $product['disable_var_quantity_change'] = true; } $product_map[$product['id']] = $product; } unset($product); } while ($product_map) { $tmp_product_map = array_slice($product_map, 0, $update_per_request, true); $product_map = array_diff_key($product_map, $tmp_product_map); $result = $this->loader->sync_products(array_keys($tmp_product_map)); if ($result['state'] !== 'error') { foreach ($result['products'] as $product) { if (!empty($tmp_product_map[$product['id']])) { $product = array_replace_recursive($tmp_product_map[$product['id']], $product); $product = A2WL_PriceFormula::apply_formula($product); $this->woocommerce_model->upd_product($product['post_id'], $product); if ($result['state'] !== 'ok') { error_log($result['message']); } unset($tmp_product_map[$product['id']]); } } // update meta for skiped products foreach($tmp_product_map as $product){ update_post_meta($product['post_id'], '_a2wl_last_update', time()); } } else { error_log($result['message']); } unset($result); } } catch (Exception $e) { error_log($e->getMessage()); } if (a2wl_get_setting('auto_update')) { $this->schedule_event(); } else { $this->unschedule_event(); } } private function schedule_event() { if (!($timestamp = wp_next_scheduled('a2wl_auto_update_event'))) { wp_schedule_single_event(time() + MINUTE_IN_SECONDS * 5, 'a2wl_auto_update_event'); } } private function unschedule_event() { wp_clear_scheduled_hook('a2wl_auto_update_event'); } } }