public function apply($products = false)
 {
     $this->resetApplication($products);
     $products = $this->getAffectedProducts($products);
     foreach ($products as $product) {
         SpecificPriceRule::applyRuleToProduct((int) $this->id, (int) $product['id_product'], (int) $product['id_product_attribute']);
     }
 }
 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;
     }
 }
 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.');
         }
     }
 }
 protected function generateSpecificPriceRuleData()
 {
     $delimiter = ';';
     $line = array();
     $titles = array();
     $new_path = new Sampledatainstall();
     $f = fopen($new_path->sendPath() . 'output/specific_price_rules.vsc', 'w');
     foreach ($this->specific_price_rule_fields as $field => $array) {
         $titles[] = $array['label'];
     }
     fputcsv($f, $titles, $delimiter, '"');
     $specific_price_rules = $this->getAllSpecificPriceRules();
     if ($specific_price_rules) {
         foreach ($specific_price_rules as $specific_price_rule) {
             $spr = new SpecificPriceRule($specific_price_rule['id_specific_price_rule']);
             foreach ($this->specific_price_rule_fields as $field => $array) {
                 $line[$field] = property_exists('SpecificPriceRule', $field) && !is_array($spr->{$field}) && !Tools::isEmpty($spr->{$field}) ? $spr->{$field} : '';
             }
             $values = array();
             $conditions = $spr->getConditions();
             if ($conditions) {
                 foreach ($conditions as $condition) {
                     foreach ($condition as $c) {
                         $values[] = $c['type'] . ':' . $c['value'];
                     }
                 }
             }
             $line['conditions'] = $conditions ? implode(',', $values) : '';
             if (!$line[$field]) {
                 $line[$field] = '';
             }
             fputcsv($f, $line, $delimiter, '"');
         }
     }
     fclose($f);
 }
示例#7
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 importSpecificPriceRules()
 {
     $this->truncateTables(array('specific_price_rule', 'specific_price_rule_condition', 'specific_price_rule_condition_group'));
     $handle = $this->openCsvFile('specific_price_rules.csv');
     for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, ';'); $current_line++) {
         $res = false;
         $fields = $this->filterFields('SpecificPriceRule', $this->specific_price_rule_fields, $line);
         if (!isset($fields['id'])) {
             $specific_price_rule = new SpecificPriceRule($line[0]);
             $specific_price_rule->id = $line[0];
         } else {
             $specific_price_rule = new SpecificPriceRule($fields['id']);
         }
         foreach ($fields as $key => $field) {
             $specific_price_rule->{$key} = $field;
         }
         // add price rule conditions ps_specific_price_rule_condition
         if (trim($line[13])) {
             $conditions = explode(',', $line[13]);
             $values = array();
             foreach ($conditions as $condition) {
                 $cond = explode(':', $condition);
                 $values[] = array('type' => $cond[0], 'value' => $cond[1]);
             }
             $specific_price_rule->addConditions($values);
         }
         $specific_price_rule->force_id = true;
         if (!$res) {
             $res = $specific_price_rule->add();
         }
     }
     $this->closeCsvFile($handle);
     return true;
 }