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.');
         }
     }
 }
예제 #3
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');
     }
 }