예제 #1
0
 public function addFeaturesToDB($id_feature, $id_value, $cust = 0)
 {
     if ($cust) {
         $row = array('id_feature' => (int) $id_feature, 'custom' => 1);
         Db::getInstance()->insert('feature_value', $row);
         $id_value = Db::getInstance()->Insert_ID();
     }
     $row = array('id_feature' => (int) $id_feature, 'id_product' => (int) $this->id, 'id_feature_value' => (int) $id_value);
     Db::getInstance()->insert('feature_product', $row);
     SpecificPriceRule::applyAllRules(array((int) $this->id));
     if ($id_value) {
         return $id_value;
     }
 }
예제 #2
0
 public function add($autodate = true, $null_values = false)
 {
     if (!parent::add($autodate, $null_values)) {
         return false;
     }
     $product = new Product((int) $this->id_product);
     if ($product->getType() == Product::PTYPE_VIRTUAL) {
         StockAvailable::setProductOutOfStock((int) $this->id_product, 1, null, (int) $this->id);
     } else {
         StockAvailable::setProductOutOfStock((int) $this->id_product, StockAvailable::outOfStock((int) $this->id_product), null, $this->id);
     }
     SpecificPriceRule::applyAllRules(array((int) $this->id_product));
     return true;
 }
 public function processGenerate()
 {
     if (!is_array(Tools::getValue('options'))) {
         $this->errors[] = Tools::displayError('Please select at least one attribute.');
     } else {
         $tab = array_values(Tools::getValue('options'));
         if (count($tab) && Validate::isLoadedObject($this->product)) {
             AdminAttributeGeneratorController::setAttributesImpacts($this->product->id, $tab);
             $this->combinations = array_values(AdminAttributeGeneratorController::createCombinations($tab));
             $values = array_values(array_map(array($this, 'addAttribute'), $this->combinations));
             // @since 1.5.0
             if ($this->product->depends_on_stock == 0) {
                 $attributes = Product::getProductAttributesIds($this->product->id, true);
                 foreach ($attributes as $attribute) {
                     StockAvailable::removeProductFromStockAvailable($this->product->id, $attribute['id_product_attribute'], Context::getContext()->shop);
                 }
             }
             SpecificPriceRule::disableAnyApplication();
             $this->product->deleteProductAttributes();
             $this->product->generateMultipleCombinations($values, $this->combinations);
             // @since 1.5.0
             if ($this->product->depends_on_stock == 0) {
                 $attributes = Product::getProductAttributesIds($this->product->id, true);
                 $quantity = str_replace(',', '.', Tools::getValue('quantity'));
                 foreach ($attributes as $attribute) {
                     StockAvailable::setQuantity($this->product->id, $attribute['id_product_attribute'], $quantity);
                 }
             } else {
                 StockAvailable::synchronize($this->product->id);
             }
             SpecificPriceRule::enableAnyApplication();
             SpecificPriceRule::applyAllRules(array((int) $this->product->id));
             Tools::redirectAdmin($this->context->link->getAdminLink('AdminProducts') . '&id_product=' . (int) Tools::getValue('id_product') . '&updateproduct&key_tab=Combinations&conf=4');
         } else {
             $this->errors[] = Tools::displayError('Unable to initialize these parameters. A combination is missing or an object cannot be loaded.');
         }
     }
 }
 public function processGenerate()
 {
     if (!is_array(Tools::getValue('options'))) {
         $this->errors[] = $this->trans('Please select at least one attribute.', array(), 'Admin.Catalog.Notification');
     } else {
         $tab = array_values(Tools::getValue('options'));
         if (count($tab) && Validate::isLoadedObject($this->product)) {
             AdminAttributeGeneratorController::setAttributesImpacts($this->product->id, $tab);
             $this->combinations = array_values(AdminAttributeGeneratorController::createCombinations($tab));
             $values = array_values(array_map(array($this, 'addAttribute'), $this->combinations));
             // @since 1.5.0
             if ($this->product->depends_on_stock == 0) {
                 $attributes = Product::getProductAttributesIds($this->product->id, true);
                 foreach ($attributes as $attribute) {
                     StockAvailable::removeProductFromStockAvailable($this->product->id, $attribute['id_product_attribute'], Context::getContext()->shop);
                 }
             }
             SpecificPriceRule::disableAnyApplication();
             $this->product->deleteProductAttributes();
             $this->product->generateMultipleCombinations($values, $this->combinations);
             // Reset cached default attribute for the product and get a new one
             Product::getDefaultAttribute($this->product->id, 0, true);
             Product::updateDefaultAttribute($this->product->id);
             // @since 1.5.0
             if ($this->product->depends_on_stock == 0) {
                 $attributes = Product::getProductAttributesIds($this->product->id, true);
                 $quantity = (int) Tools::getValue('quantity');
                 foreach ($attributes as $attribute) {
                     if (Shop::getContext() == Shop::CONTEXT_ALL) {
                         $shops_list = Shop::getShops();
                         if (is_array($shops_list)) {
                             foreach ($shops_list as $current_shop) {
                                 if (isset($current_shop['id_shop']) && (int) $current_shop['id_shop'] > 0) {
                                     StockAvailable::setQuantity($this->product->id, (int) $attribute['id_product_attribute'], $quantity, (int) $current_shop['id_shop']);
                                 }
                             }
                         }
                     } else {
                         StockAvailable::setQuantity($this->product->id, (int) $attribute['id_product_attribute'], $quantity);
                     }
                 }
             } else {
                 StockAvailable::synchronize($this->product->id);
             }
             SpecificPriceRule::enableAnyApplication();
             SpecificPriceRule::applyAllRules(array((int) $this->product->id));
             Tools::redirectAdmin($this->context->link->getAdminLink('AdminProducts') . '&id_product=' . (int) Tools::getValue('id_product') . '&updateproduct&key_tab=Combinations&conf=4');
         } else {
             $this->errors[] = $this->trans('Unable to initialize these parameters. A combination is missing or an object cannot be loaded.');
         }
     }
 }
예제 #5
0
 public function hookDisplayProductDeliveryTime($params)
 {
     $product = $params['product'];
     $id_lang = Context::getContext()->language->id;
     $file = dirname(__FILE__) . '/content/' . $product->id . '_c.html';
     $fileu = dirname(__FILE__) . '/content/' . $product->id . '_u.html';
     $content = '';
     if (Tools::getValue("u", false) !== false) {
         $url = Tools::getValue('url');
         if (Tools::getValue('url', false) !== false) {
             if ($url != '') {
                 file_put_contents($fileu, $url);
             }
             $url = file_get_contents($fileu);
             if ($url != '') {
                 $content = $this->getProductContent($url);
                 file_put_contents($file, $content);
                 $messages[] = "load from url";
             } else {
                 $messages[] = "Error: url not found";
             }
         }
         if ($content == '') {
             $messages[] = "update content";
             $content = file_get_contents($file);
         }
         $query = "//select[@id='sku-variants']/option";
         $dom = new DomDocument();
         libxml_use_internal_errors(true);
         $dom->loadHTML($content);
         $xpath = new DomXPath($dom);
         $nodes = $xpath->query($query);
         if ($nodes->length == 0) {
             // update only base price
             $query = "string(//div[@id='price-container']/span/text())";
             $price = $xpath->evaluate($query);
             $query = "string(//div[@id='price-container']/del/text())";
             $oldprice = $xpath->evaluate($query);
             $price = preg_replace("/[^0-9]/", '', $price);
             $oldprice = preg_replace("/[^0-9]/", '', $oldprice);
             if ($price == "" && $oldprice == "") {
                 $tab[0] = array();
                 $combinations = 0;
                 $query = "string(//div[@id='complectData']/text())";
                 $node = $xpath->evaluate($query);
                 $product_data = json_decode($node);
                 $offers = $product_data->items[0]->OFFERS;
                 //array_multisort($offers['BASE_PRICE'], SORT_ASC);
                 foreach ($offers as $offer) {
                     $size = $offer->SHIRINA . '×' . $offer->DLINA;
                     $id_attr = egormprod::getAttributeId($size);
                     if ($id_attr) {
                         $tab[0][] = $id_attr;
                         $prices[] = $offer->BASE_PRICE;
                     } else {
                         $messages[] = "Error: size " . $size . " not found ";
                     }
                 }
                 array_multisort($prices, SORT_ASC, SORT_NUMERIC, $tab[0]);
                 $this->basePrice = $prices[0];
                 foreach ($prices as $key => $price_tmp) {
                     $prices[$key] = $price_tmp - $this->basePrice;
                 }
                 $this->updateProductBasePrice($product->id);
                 egormprod::setAttributesImpacts($product->id, $tab);
                 //AdminAttributeGeneratorController::setAttributesImpacts($product->id, $tab);
                 $combinations = $this->getCombination($tab);
                 $values = $this->getMap($product->id, $combinations, $prices);
                 //array_values(array_map(array($this, 'addAttribute'), $this->combinations));
                 SpecificPriceRule::disableAnyApplication();
                 $product->deleteProductAttributes();
                 $product->generateMultipleCombinations($values, $combinations);
                 SpecificPriceRule::enableAnyApplication();
                 SpecificPriceRule::applyAllRules(array((int) $product->id));
                 $messageTextErr = "";
                 foreach ($messages as $message) {
                     $messageTextErr .= $message . "\\r\\n";
                 }
             } else {
                 // pillows
                 $this->basePrice = $oldprice > 0 ? $oldprice : $price;
                 $this->updateProductBasePrice($product->id);
             }
         } else {
             $tab[0] = array();
             $combinations = 0;
             foreach ($nodes as $key => $node) {
                 $product_data = json_decode($node->getAttribute('data-price'));
                 if ($key == 0) {
                     $this->basePrice = $product_data->VALUE;
                 }
                 $f1 = json_decode($node->getAttribute('data-cases'));
                 $size = str_replace(' ', '', $node->getAttribute('value'));
                 $id_attr = egormprod::getAttributeId($size);
                 if ($id_attr) {
                     $tab[0][] = $id_attr;
                     $prices[] = $product_data->VALUE - $this->basePrice;
                 } else {
                     $messages[] = "Error: size " . $size . " not found ";
                 }
             }
             $this->updateProductBasePrice($product->id);
             egormprod::setAttributesImpacts($product->id, $tab);
             //AdminAttributeGeneratorController::setAttributesImpacts($product->id, $tab);
             $combinations = $this->getCombination($tab);
             $values = $this->getMap($product->id, $combinations, $prices);
             //array_values(array_map(array($this, 'addAttribute'), $this->combinations));
             SpecificPriceRule::disableAnyApplication();
             $product->deleteProductAttributes();
             $product->generateMultipleCombinations($values, $combinations);
             SpecificPriceRule::enableAnyApplication();
             SpecificPriceRule::applyAllRules(array((int) $product->id));
             $messageTextErr = "";
             foreach ($messages as $message) {
                 $messageTextErr .= $message . "\\r\\n";
             }
         }
     }
     if (Tools::getValue("admin", false) !== false) {
         $f = 1;
         $this->context->cookie->__set('a', 1);
     }
     if (Tools::getValue("admin") == "-1") {
         $f = 1;
         $this->context->cookie->__unset('a');
     }
     if ($this->context->cookie->__isset('a')) {
         $url = file_get_contents($fileu);
         $this->smarty->assign(array('messageTextErr' => $messageTextErr, 'url' => $url));
         return $this->display(__FILE__, 'up.tpl');
     }
 }
 protected function importProducts()
 {
     $this->truncateTables(array('product', 'product_lang', 'product_shop', 'product_sale', 'product_supplier', 'product_tag', 'feature_product', 'category_product', 'product_carrier', 'compare_product', 'product_attachment', 'product_country_tax', 'product_download', 'product_group_reduction_cache', 'scene_products', 'warehouse_product_location', 'customization', 'customization_field', 'customization_field_lang', 'supply_order_detail', 'attribute_impact', 'pack'));
     $handle = $this->openCsvFile('products.csv');
     $languages = Language::getLanguages(false);
     for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, ';'); $current_line++) {
         $res = false;
         $product = new Product((int) $line[0]);
         $product->id = (int) $line[0];
         $product->active = $line[1];
         foreach ($languages as $lang) {
             $product->name[$lang['id_lang']] = $line['2'];
         }
         $categories = explode(',', $line[3]);
         $product->id_category_default = $categories[0] ? $categories[0] : Configuration::get('PS_HOME_CATEGORY');
         if (isset($categories) && $categories && count($categories)) {
             $product->addToCategories($categories);
         }
         if (isset($line[4])) {
             $product->price_tex = $line[4];
         }
         if (isset($line[5])) {
             $product->price_tin = $line[5];
         }
         $product->id_tax_rules_group = trim($line[6]) ? $line[6] : 0;
         $product->wholesale_price = trim($line[7]) ? $line[7] : 0;
         $product->on_sale = trim($line[8]) ? $line[8] : 0;
         if (trim($line[13])) {
             $product->reference = $line[13];
         }
         if (trim($line[14])) {
             $product->supplier_reference = trim($line[14]);
         }
         if (trim($line[15])) {
             $product->id_supplier = (int) $line[15];
         }
         if (isset($product->id) && $product->id && isset($product->id_supplier) && property_exists($product, 'supplier_reference')) {
             $id_product_supplier = (int) ProductSupplier::getIdByProductAndSupplier((int) $product->id, 0, (int) $product->id_supplier);
             if ($id_product_supplier) {
                 $product_supplier = new ProductSupplier($id_product_supplier);
             } else {
                 $product_supplier = new ProductSupplier();
             }
             $product_supplier->id_product = (int) $product->id;
             $product_supplier->id_product_attribute = 0;
             $product_supplier->id_supplier = (int) $product->id_supplier;
             $product_supplier->product_supplier_price_te = $product->wholesale_price;
             $product_supplier->product_supplier_reference = $product->supplier_reference;
             $product_supplier->save();
         }
         if (trim($line[16])) {
             $product->id_manufacturer = $line[16];
         }
         if (!Tools::isEmpty(trim($line[17]))) {
             $product->ean13 = $line[17];
         }
         if (trim($line[18])) {
             $product->upc = $line[18];
         }
         if (trim($line[19])) {
             $product->ecotax = $line[19];
         }
         $product->width = $line[20];
         $product->height = $line[21];
         $product->depth = $line[22];
         $product->weight = $line[23];
         if ($line[24]) {
             StockAvailable::setQuantity((int) $product->id, 0, (int) $line[24], (int) $this->context->shop->id);
         }
         $product->minimal_quantity = $line[25];
         $product->visibility = $line[26];
         $product->additional_shipping_cost = $line[27];
         if (trim($line[28])) {
             $product->unity = $line[28];
         }
         if (trim($line[29])) {
             $product->unit_price = $line[29];
         }
         foreach ($languages as $lang) {
             $product->description_short[$lang['id_lang']] = $line[30];
             $product->description[$lang['id_lang']] = $line[31];
         }
         if ($line[32]) {
             foreach ($languages as $lang) {
                 Tag::addTags($lang['id_lang'], $product->id, $line[32]);
             }
         }
         foreach ($languages as $lang) {
             $product->meta_title[$lang['id_lang']] = $line[33];
             $product->meta_keywords[$lang['id_lang']] = $line[34];
             $product->meta_description[$lang['id_lang']] = $line[35];
             $product->link_rewrite[$lang['id_lang']] = $line[36];
             $product->available_now[$lang['id_lang']] = $line[37];
             $product->available_later[$lang['id_lang']] = $line[38];
         }
         $product->available_for_order = $line[39];
         $product->available_date = $line[40];
         $product->date_add = $line[41];
         $product->show_price = $line[42];
         // Features import
         $features = explode(',', $line[45]);
         if ($features) {
             foreach ($features as $feature) {
                 $value = explode(':', $feature);
                 if ($value[0] && $value[1]) {
                     Product::addFeatureProductImport((int) $product->id, (int) $value[0], (int) $value[1]);
                     SpecificPriceRule::applyAllRules(array((int) $product->id));
                 }
             }
         }
         $product->online_only = trim($line[46]) ? $line[46] : 0;
         $product->condition = $line[47];
         $product->customizable = trim($line[48]) ? $line[48] : 0;
         $product->uploadable_files = trim($line[49]) ? $line[49] : 0;
         $product->text_fields = trim($line[50]) ? $line[50] : 0;
         if ($product->getType() == Product::PTYPE_VIRTUAL) {
             StockAvailable::setProductOutOfStock((int) $product->id, 1);
         } else {
             StockAvailable::setProductOutOfStock((int) $product->id, (int) $line[51]);
         }
         $product->id_shop_default = $line[52];
         // add product accessories
         if ($line[56]) {
             $accessories = explode(',', $line[56]);
             foreach ($accessories as $accessory) {
                 $a[]['id'] = $accessory;
             }
             $product->setWsAccessories($a);
         }
         // add product carriers
         if ($line[57]) {
             $carriers = explode(',', $line[57]);
             $product->setCarriers($carriers);
         }
         // add costomisation fields
         if (!Tools::isEmpty($line[58]) && class_exists('CustomizationField')) {
             $customisation_fields_ids = explode(',', $line[58]);
             foreach ($customisation_fields_ids as $customisation_field) {
                 $result = false;
                 $customisation_data = explode(':', $customisation_field);
                 $cf = new CustomizationField();
                 $cf->id_product = $product->id;
                 $cf->type = $customisation_data[1];
                 $cf->required = $customisation_data[2];
                 foreach ($languages as $lang) {
                     $cf->name[$lang['id_lang']] = $customisation_data[3] ? $customisation_data[3] : ' ';
                 }
                 $cf->force_id = 1;
                 if (!$result) {
                     $result = $cf->add();
                 }
             }
         }
         // add attachments
         if ($line[59]) {
             $attachments = explode(',', $line[59]);
             if (isset($attachments) && count($attachments)) {
                 Attachment::attachToProduct($product->id, $attachments);
             }
         }
         if ($line[60]) {
             $product->date_upd = $line[60];
         }
         $product->price = $line[61];
         $product->force_id = 1;
         if (!$res) {
             $res = $product->add();
         }
     }
     $this->closeCsvFile($handle);
     Search::indexation(true);
     return true;
 }