Пример #1
0
 /**
  * Imports and adds a sync flag to all the loaded products from LightSpeed
  * Idea - leave image import to a later time? i.e. set a flag for each imported item, and
  * if that flag exists, try and import those images.
  */
 public function import_and_sync_all_load_prods()
 {
     $nonce = $_POST['wclsi_admin_nonce'];
     if (!wp_verify_nonce($nonce, 'wclsi_load_ls_prods')) {
         header("HTTP/1.0 409 Security Check.");
         exit;
     }
     // First pass, return JSON of prod IDs
     if (true == $_POST['init_import']) {
         echo json_encode(array('ls_prod_ids' => array_keys(wclsi_glue_chunks(true))));
         exit;
     }
     // After first pass, start importing products
     if (isset($_POST['prod_id'])) {
         $prod_id = (int) $_POST['prod_id'];
         $wclsi_cat_cache = get_option('wclsi_cat_cache');
         // Get the products chunk ID and key
         $ls_prod_info = wclsi_get_ls_prod_chunk_info($prod_id);
         if (is_array($ls_prod_info)) {
             $prod_key = $ls_prod_info['key'];
             $chunk_id = $ls_prod_info['chunk_id'];
             // Get the product's chunk that it belongs to
             $wclsi_prod_chunk = get_option('wclsi_prod_chunk_' . $chunk_id);
             // If it's there, convert it
             if (isset($wclsi_prod_chunk->Item[$prod_key])) {
                 $ls_prod = $wclsi_prod_chunk->Item[$prod_key];
                 $wc_prod_id = $this->convert_to_wc_prod($ls_prod, $wclsi_cat_cache);
                 // Check if everything went ok
                 if (!is_wp_error($wc_prod_id)) {
                     // Update the chunk after we've imported the product
                     $wclsi_prod_chunk->Item[$prod_key]->wc_prod_id = $wc_prod_id;
                     $wclsi_prod_chunk->Item[$prod_key]->last_import = current_time('mysql');
                     update_option('wclsi_prod_chunk_' . $chunk_id, $wclsi_prod_chunk);
                     // Save our progress in case something gets f'ed
                     update_option('wclsi_import_progress', array('prod_id' => $prod_id, 'progress' => $_POST['import_progress']));
                     echo json_encode(array('success' => $wc_prod_id, 'prod_id' => $prod_id));
                 } else {
                     // Otherwise return an error
                     header("HTTP/1.0 409 " . sprintf(__('Could not convert product with ID %d. Error: %s', 'woocommerce-lightspeed-pos'), $prod_id, $wc_prod_id->get_error_message()));
                     exit;
                 }
             } else {
                 header("HTTP/1.0 409 " . sprintf(__('Product with ID %d does not exist!', 'woocommerce-lightspeed-pos'), $prod_id));
                 exit;
             }
         }
     } else {
         header("HTTP/1.0 409 " . __('Could not find a product ID to import!', 'woocommerce-lightspeed-pos'));
         exit;
     }
     exit;
 }
Пример #2
0
 /**
  * Sets the sync flag for a single product
  * @param $ls_prod_id
  * @param $is_synced
  * @param $ls_prods
  * @return array
  */
 function process_single_sync($ls_prod_id, $is_synced, $ls_prods = array())
 {
     // For bulk process, we'll pass $ls_prods to make it more efficient
     if (empty($ls_prods)) {
         $ls_prods = wclsi_glue_chunks(true);
     }
     if (isset($ls_prods[$ls_prod_id])) {
         $ls_prod = $ls_prods[$ls_prod_id];
         if (isset($ls_prod->wc_prod_id)) {
             update_post_meta($ls_prod->wc_prod_id, '_wclsi_sync', $is_synced);
             $update_msg = __('Successfully added "' . $ls_prod->description . '" to the sync schedule!');
             $update_type = 'updated';
         } else {
             $update_msg = __('Please import "' . $ls_prod->description . '" before attempting to sync it!', 'woocommerce-lightspeed-pos');
             $update_type = 'error';
         }
     } else {
         $update_msg = __('Error: Could not find product ID to sync with!', 'woocommerce-lightspeed-pos');
         $update_type = 'error';
     }
     return array('msg' => $update_msg, 'type' => $update_type);
 }