Ejemplo n.º 1
0
 public static function syncProducts($products, $context, $id_lang, $request_context = null, $log_type = false)
 {
     if (!$products) {
         return;
     }
     $date = date('Y-m-d H:i:s');
     // Get errors back
     if (file_exists(dirname(__FILE__) . '/../log/syncError.php')) {
         include dirname(__FILE__) . '/../log/syncError.php';
     }
     $tab_error = array();
     // Up the time limit
     @set_time_limit(3600);
     $product_ids = array_map(array('EbaySynchronizer', 'getIdProduct'), $products);
     if (method_exists('Cache', 'clean')) {
         Cache::clean('StockAvailable::getQuantityAvailableByProduct_*');
     }
     foreach ($products as $p) {
         $product = new Product((int) $p['id_product'], true, $id_lang);
         $product_configuration = EbayProductConfiguration::getByProductIdAndProfile($p['id_product'], $p['id_ebay_profile']);
         // make sure that product exists in the db and has a default category
         if (!Validate::isLoadedObject($product) || !$product->id_category_default) {
             continue;
         }
         $quantity_product = EbaySynchronizer::_getProductQuantity($product, (int) $p['id_product']);
         $ebay_profile = new EbayProfile((int) $p['id_ebay_profile']);
         if (!$ebay_profile->getConfiguration('EBAY_HAS_SYNCED_PRODUCTS')) {
         }
         $ebay_profile->setConfiguration('EBAY_HAS_SYNCED_PRODUCTS', 1);
         $ebay_category = EbaySynchronizer::_getEbayCategory($product->id_category_default, $ebay_profile);
         $variations = EbaySynchronizer::_loadVariations($product, $ebay_profile, $context, $ebay_category);
         $ebay = new EbayRequest((int) $p['id_ebay_profile'], $request_context);
         if (!$product->active || $product_configuration && $product_configuration['blacklisted']) {
             // try to stop sale on eBay
             $ebay = EbaySynchronizer::endProductOnEbay($ebay, $ebay_profile, $context, $id_lang, null, $product->id);
             if (!empty($ebay->error)) {
                 $tab_error = EbaySynchronizer::_updateTabError($ebay->error, str_replace('&', '&', $product->name));
                 if ($log_type) {
                     EbayLog::write('Error: ' . $ebay->error, $log_type);
                 }
             }
             continue;
         }
         $pictures = EbaySynchronizer::_getPictures($product, $ebay_profile, $id_lang, $context, $variations);
         // Load basic price
         list($price, $price_original) = EbaySynchronizer::_getPrices($product->id, $ebay_category->getPercent(), $ebay_profile);
         $conditions = $ebay_category->getConditionsValues($p['id_ebay_profile']);
         $ebay_store_category_id = pSQL(EbayStoreCategoryConfiguration::getEbayStoreCategoryIdByIdProfileAndIdCategory($ebay_profile->id, $product->id_category_default));
         // Generate array and try insert in database
         $data = array('price' => $price, 'quantity' => $quantity_product, 'categoryId' => $ebay_category->getIdCategoryRef(), 'variations' => $variations, 'pictures' => $pictures['general'], 'picturesMedium' => $pictures['medium'], 'picturesLarge' => $pictures['large'], 'condition' => $conditions[$product->condition], 'shipping' => EbaySynchronizer::_getShippingDetailsForProduct($product, $ebay_profile), 'id_lang' => $id_lang, 'real_id_product' => (int) $p['id_product'], 'ebay_store_category_id' => $ebay_store_category_id);
         $data = array_merge($data, EbaySynchronizer::_getProductData($product, $ebay_profile));
         // Fix hook update product
         if (Tools::getValue('id_product_attribute')) {
             $id_product_attribute_fix = (int) Tools::getValue('id_product_attribute');
             $key = $product_id . '-' . $id_product_attribute_fix . '_' . $ebay_profile->id;
             if (isset($data['variations'][$key]['quantity'])) {
                 $data['variations'][$key]['quantity'] = EbaySynchronizer::_fixHookUpdateProduct($context, $product_id, $data['variations'][$key]['quantity']);
             }
         }
         // Price Update
         if (isset($p['noPriceUpdate'])) {
             $data['noPriceUpdate'] = $p['noPriceUpdate'];
         }
         $clean_percent = $ebay_category->getCleanPercent();
         // Save percent and price discount
         if ($clean_percent < 0) {
             $data['price_original'] = round($price_original, 2);
             //				$data['price_percent'] = round($clean_percent);
         } elseif ($price_original > $price) {
             $data['price_original'] = round($price_original, 2);
         }
         if (isset($data['price_original'])) {
             $data['price_percent'] = round(($price_original - $price) / $price_original * 100.0);
         }
         $data['description'] = EbaySynchronizer::_getEbayDescription($product, $ebay_profile, $id_lang);
         // Export to eBay
         $ebay = EbaySynchronizer::_exportProductToEbay($product, $data, $p['id_ebay_profile'], $ebay_category, $ebay, $date, $context, $id_lang);
         if (!empty($ebay->error)) {
             $tab_error = EbaySynchronizer::_updateTabError($ebay->error, $data['name']);
             if ($log_type) {
                 EbayLog::write('Error: ' . $ebay->error, $log_type);
             }
         } elseif ($log_type) {
             EbayLog::write('Success', $log_type);
         }
         if ($request_context == 'CRON') {
             Configuration::updateValue('NB_PRODUCTS_LAST', (int) Configuration::get('NB_PRODUCTS_LAST') + 1);
         }
     }
     if (count($tab_error)) {
         if (isset($all_error)) {
             foreach ($all_error as $key => $value) {
                 if (isset($tab_error[$key])) {
                     $tab_error[$key]['products'] = array_merge($all_error[$key]['products'], $tab_error[$key]['products']);
                 } else {
                     $tab_error[$key] = $all_error[$key];
                 }
             }
         }
         file_put_contents(dirname(__FILE__) . '/../log/syncError.php', '<?php $all_error = ' . var_export($tab_error, true) . '; ' . ($ebay->itemConditionError ? '$itemConditionError = true; ' : '$itemConditionError = false;') . ' ?>');
     }
 }