/** * Updates inventory in LightSpeed * @todo - Right now this will update the inventory in the ls_obj, but other properties will not be updated * @param $wc_prod object */ function sync_prod_inventory($wc_prod) { if (!empty($wc_prod->stock)) { $LSI_Import_Settings = new LSI_Init_Settings(); // Initialize import settings for API calls $wclsi_sync = (bool) get_post_meta($wc_prod->id, '_wclsi_sync', true); if ($wclsi_sync) { $ls_obj = get_post_meta($wc_prod->id, '_wclsi_ls_obj', true); // get the LightSpeed object if (!empty($ls_obj)) { // This will be JSON encoded and used in our PUT request $sync_item = array(); $sync_item['itemID'] = $ls_obj->itemID; $this->handle_itemshops($ls_obj, $wc_prod, $sync_item); // Update the option update_post_meta($wc_prod->id, '_wclsi_ls_obj', $ls_obj); // Sync with LightSpeed $LSI_Import_Settings->make_api_call('Account.Item', 'Update', array('load_relations' => json_encode('all')), $sync_item, $ls_obj->itemID); } } } }
/** * Processes a single product's manual sync * @param $old_ls_prod * @param $wclsi_cat_cache - optional if this is used in a loop setting to make it more efficient.s * @return array */ public static function process_single_manual_sync($old_ls_prod, $wclsi_cat_cache = null) { $wclsi_settings = new LSI_Init_Settings(); $wclsi_import_controller = new LSI_Import_Page(); $search_params = array('load_relations' => json_encode(array("ItemShops", "ItemECommerce", "Images", "Tags"))); $new_ls_prod = $wclsi_settings->make_api_call('Account/' . $wclsi_settings->ls_account_id . '/Item/' . $old_ls_prod->itemID, 'Read', $search_params); if (!is_wp_error($new_ls_prod) && !empty($new_ls_prod) && !empty($old_ls_prod)) { $new_ls_prod = $new_ls_prod->Item; // If we've imported this item, make sure to update its last sync date if (isset($old_ls_prod->wc_prod_id)) { // Check for the category cache, otherwise look it up if (is_null($wclsi_cat_cache)) { $wclsi_cat_cache = get_option('wclsi_cat_cache'); } $new_ls_prod->last_import = $old_ls_prod->last_import; $new_ls_prod->wc_prod_id = $old_ls_prod->wc_prod_id; $new_ls_prod->last_sync_date = current_time('mysql'); $wclsi_import_controller->update_product($old_ls_prod, $new_ls_prod, $wclsi_cat_cache, true); } // Grab the prod chunk info $prod_chunk_info = wclsi_get_ls_prod_chunk_info($old_ls_prod->itemID); $prod_key = $prod_chunk_info['key']; $prod_chunk_id = $prod_chunk_info['chunk_id']; // Get the chunk where the LightSpeed product is stored $chunk = get_option('wclsi_prod_chunk_' . $prod_chunk_id); // overwrite the prod in the chunk Item array $chunk->Item[$prod_key] = $new_ls_prod; // update the chunk with the updated item update_option('wclsi_prod_chunk_' . $prod_chunk_id, $chunk); return array('msg' => __('1 product successfully updated.', 'woocommerce-lightspeed-pos'), 'type' => 'updated'); } else { if ($new_ls_prod->get_error_code() == 'wclsi_bad_api_call') { return array(); } else { return array('msg' => __($new_ls_prod->get_error_message(), 'woocommerce-lightspeed-pos'), 'type' => 'error'); } } }