$id_product = (int) $line[0]; $product = new Product($id_product, true, 1); $newPrice = (double) $line[1]; $mrp = $product->getPriceWithoutReduct(); $product = new Order($id_order); SpecificPrice::deleteByProductId($id_product); $specificPriceIDs = SpecificPrice::getIdsByProductId($id_product); if (count($specificPriceIDs) > 0) { $specificPrice = new SpecificPrice((int) $specificPriceIDs[0]['id_specific_price']); //delete discount if the prices are equal update otherwise if (round($mrp) > round($newPrice)) { $specificPrice->reduction_type = "amount"; $specificPrice->reduction = $mrp - $newPrice; $specificPrice->update(); } else { SpecificPrice::deleteByProductId($id_product); } } else { if (round($mrp) > round($newPrice)) { $specificPrice = new SpecificPrice(); $specificPrice->id_product = $id_product; $specificPrice->reduction_type = "amount"; $specificPrice->reduction = $mrp - $newPrice; $specificPrice->from_quantity = 1; $specificPrice->from = date('Y-m-d H:i:s'); $specificPrice->update(); } } SolrSearch::updateProduct($id_product); $count++; }
public function delete() { if (!GroupReduction::deleteProductReduction($this->id)) { return false; } Hook::deleteProduct($this); if (!parent::delete() or !$this->deleteCategories(true) or !$this->deleteImages() or !$this->deleteProductAttributes() or !$this->deleteProductFeatures() or !$this->deleteTags() or !$this->deleteCartProducts() or !$this->deleteAttributesImpacts() or !$this->deleteAttachments() or !$this->deleteCustomization() or !SpecificPrice::deleteByProductId((int) $this->id) or !$this->deletePack() or !$this->deleteProductSale() or !$this->deleteSceneProducts() or !$this->deleteSearchIndexes() or !$this->deleteAccessories() or !$this->deleteFromAccessories()) { return false; } if ($id = ProductDownload::getIdFromIdProduct($this->id)) { if ($productDownload = new ProductDownload($id) and !$productDownload->delete(true)) { return false; } } return true; }
public function delete() { /* * @since 1.5.0 * It is NOT possible to delete a product if there are currently: * - physical stock for this product * - supply order(s) for this product */ if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && $this->advanced_stock_management) { $stock_manager = StockManagerFactory::getManager(); $physical_quantity = $stock_manager->getProductPhysicalQuantities($this->id, 0); $real_quantity = $stock_manager->getProductRealQuantities($this->id, 0); if ($physical_quantity > 0) { return false; } if ($real_quantity > $physical_quantity) { return false; } } // Removes the product from StockAvailable, for the current shop StockAvailable::removeProductFromStockAvailable($this->id); $result = parent::delete(); $result &= $this->deleteProductAttributes() && $this->deleteImages() && $this->deleteSceneProducts(); // If there are still entries in product_shop, don't remove completly the product if ($this->hasMultishopEntries()) { return true; } Hook::exec('actionProductDelete', array('product' => $this)); if (!$result || !GroupReduction::deleteProductReduction($this->id) || !$this->deleteCategories(true) || !$this->deleteProductFeatures() || !$this->deleteTags() || !$this->deleteCartProducts() || !$this->deleteAttributesImpacts() || !$this->deleteAttachments() || !$this->deleteCustomization() || !SpecificPrice::deleteByProductId((int) $this->id) || !$this->deletePack() || !$this->deleteProductSale() || !$this->deleteSearchIndexes() || !$this->deleteAccessories() || !$this->deleteFromAccessories() || !$this->deleteFromSupplier() || !$this->deleteDownload() || !$this->deleteFromCartRules()) { return false; } return true; }
public function handleConfirm($update = false) { global $currentIndex, $cookie, $smarty; $products_to_import = array(); $defaultLanguageId = (int) Configuration::get('PS_LANG_DEFAULT'); $file_path = Tools::getValue('current_file'); $overwrite_imgs = Tools::getValue("overwrite_imgs"); //$file_path = '/Users/rohit/webroot/indusdiva/admin12/product-uploads/upload_sheet_1.csv'; $f = fopen($file_path, 'r'); $file_error = false; if ($f) { //discard header $line = fgetcsv($f); while ($line = fgetcsv($f)) { //ignore empty lines if (empty($line)) { continue; } //trim data foreach ($line as $key => $value) { $line[$key] = trim($value); } $id_product = $line[0]; $images = $line[1]; $product_name = $line[2]; $fabric = $line[3]; $color = $line[4]; $mrp = $line[5]; $supplier_code = $line[6]; $reference = $line[7]; $location = $line[8]; $length = $line[9]; $width = $line[10]; $blouse_length = $line[11]; $garment_type = $line[12]; $work_type = $line[13]; $weight = $line[14]; $description = $line[15]; $other_info = $line[16]; $wash_care = $line[17]; $shipping_estimate = $line[18]; $supplier_price = $line[19]; $manufacturer = $line[20]; $categories = explode(",", $line[21]); $tax_rule = $line[22]; $quantity = $line[23]; $active = $line[24]; $discount = $line[25]; $tags = $line[26]; $kameez_style = $line[27]; $salwar_style = $line[28]; $sleeves = $line[29]; $customizable = $line[30]; $generic_color = $line[31]; $skirt_length = $line[32]; $dupatta_length = $line[33]; $stone = $line[34]; $plating = $line[35]; $material = $line[36]; $dimensions = $line[37]; $look = $line[38]; $as_shown = isset($line[39]) && !empty($line[39]) ? intval($line[39]) : 0; $id_sizechart = isset($line[40]) && !empty($line[40]) ? intval($line[40]) : 0; $is_exclusive = isset($line[41]) && !empty($line[41]) ? intval($line[41]) : 0; $handbag_occasion = isset($line[42]) && !empty($line[42]) ? $line[42] : null; $handbag_style = isset($line[43]) && !empty($line[43]) ? $line[43] : null; $handbag_material = isset($line[44]) && !empty($line[44]) ? $line[44] : null; $images = explode(",", $images); $error = false; //validate fields if (!Validate::isFloat($mrp)) { $error = 'MRP should be a number: ' . trim($reference); } elseif (!Validate::isFloat($supplier_price)) { $error = 'Supplier Price should be a number: ' . trim($reference); } $importCategories = array(); if (is_array($categories)) { $categories = array_unique($categories); foreach ($categories as $category) { $category = intval(trim($category)); if (empty($category)) { continue; } if (!is_numeric($category) || !Category::categoryExists($category)) { $error = 'Category does not exist: ' . $category; } $importCategories[] = $category; } } else { $error = 'Atleast one category required: ' . trim($reference); } if (!Validate::isFloat($weight)) { $error = 'Weight has to be a number: ' . trim($reference); } if (!empty($manufacturer) && (!is_numeric($manufacturer) || !Manufacturer::manufacturerExists((int) $manufacturer))) { $error = 'Manufacturer does not exist'; } if ($quantity && !is_numeric($quantity) || $discount && !is_numeric($discount)) { $error = 'Quantity and discount should be numbers: ' . trim($reference); } if (!Validate::isLoadedObject(new TaxRulesGroup($tax_rule))) { $error = 'Tax rate invalid: ' . trim($reference); } if (!$update) { $sql = "SELECT `reference`\n\t\t\t\t\t\t\tFROM " . _DB_PREFIX_ . "product p\n\t\t\t\t\t\t\tWHERE p.`reference` = '" . $reference . "'"; $row = Db::getInstance()->getRow($sql); if (isset($row['reference'])) { $error = "Duplicate indusdiva code : " . trim($reference); } } //check for souring price if ($supplier_price > $mrp / 1.2) { $error = "MRP too low : " . trim($reference); } //check for images if (!$update || $overwrite_imgs == "on") { foreach ($images as $image_name) { $image_name = trim($image_name); $image_path = IMAGE_UPLOAD_PATH . $image_name; if (!empty($image_name) && !file_exists($image_path)) { $error = "Image not found for: " . trim($reference) . ", Image Name: " . $image_name; break; } } } $vendor_code = substr($reference, 0, 6); $sql = "select id_supplier from ps_supplier where code = '{$vendor_code}'"; $row = Db::getInstance()->getRow($sql); if (!isset($row['id_supplier'])) { $error = "Vendor Details not found for : " . trim($reference); } else { $id_supplier = $row['id_supplier']; } //For sudarshan, supplier_code (vendor product code) is mandatory if (false) { //(int) $id_supplier === 2 ) { if (empty($supplier_code)) { $error = "Reference: {$reference} -- Supplier Code is Mandatory for Vendor {$vendor_code}"; } else { if (strpos("::", ${$supplier_code}) === false) { $error = "Reference: {$reference} -- Supplier Code:{$supplier_code} is not in DESIGN_NO::ITEM_CODE format for Vendor {$vendor_code}"; } } } if (!$error) { if ($update && !empty($id_product)) { $product = new Product((int) $id_product); if (!Validate::isLoadedObject($product)) { $error = "Error loading the product: " . $id_product; return; } } elseif (!$update) { $product = new Product(); } $product->id_tax_rules_group = $tax_rule; $product->reference = $reference; $product->id_supplier = $id_supplier; $product->location = $location; $product->tax_rate = TaxRulesGroup::getTaxesRate((int) $product->id_tax_rules_group, Configuration::get('PS_COUNTRY_DEFAULT'), 0, 0); if (isset($manufacturer) and is_numeric($manufacturer) and Manufacturer::manufacturerExists((int) $manufacturer)) { $product->id_manufacturer = $manufacturer; } $product->price = (double) $mrp; $product->price = (double) number_format($product->price / (1 + $product->tax_rate / 100), 6, '.', ''); $product->id_category = $importCategories; $product->id_category_default = 1; $product->name = array(); $product->name[$defaultLanguageId] = $product_name; $product->description_short = array(); $product->description_short[$defaultLanguageId] = $style_tips; $product->description = array(); $product->description[$defaultLanguageId] = $description; $link_rewrite = Tools::link_rewrite($product->name[$defaultLanguageId]); $product->link_rewrite = array(); $product->link_rewrite[$defaultLanguageId] = $link_rewrite; $product->quantity = $quantity ? intval($quantity) : 0; if ($discount && is_numeric($discount)) { $product->discount = $discount; } if (!empty($tags)) { $product->tags = $tags; } $product->weight = is_numeric($weight) ? $weight : 0; $product->width = is_numeric($width) ? $width : 0; $product->height = is_numeric($length) ? $length : 0; $product->supplier_reference = $supplier_code; $product->wholesale_price = $supplier_price ? (double) $supplier_price : 0; $product->active = $active == 1 ? 1 : 0; $product->images = $images; $product->fabric = $fabric; $product->color = $color; $product->generic_color = $generic_color; $product->garment_type = $garment_type; $product->work_type = $work_type; $product->blouse_length = $blouse_length ? $blouse_length : ' '; $product->wash_care = $wash_care ? $wash_care : ' '; $product->other_info = $other_info ? $other_info : ' '; $product->shipping_estimate = $shipping_estimate ? $shipping_estimate : ' '; $product->is_customizable = $customizable == 1 ? 1 : 0; $product->kameez_style = $kameez_style; $product->salwar_style = $salwar_style; $product->sleeves = $sleeves; $product->skirt_length = $skirt_length; $product->dupatta_length = $dupatta_length; $product->stone = $stone; $product->plating = $plating; $product->material = $material; $product->dimensions = $dimensions; $product->look = $look; $product->as_shown = $as_shown; $product->id_sizechart = $id_sizechart; $product->is_exclusive = $is_exclusive; $product->handbag_occasion = $handbag_occasion; $product->handbag_style = $handbag_style; $product->handbag_material = $handbag_material; $product->indexed = 0; $products_to_import[] = $product; } else { $smarty->assign('error', $error); return; $file_error = true; } } if (!$file_error) { $added_product_ids = array(); foreach ($products_to_import as $product) { $fieldError = $product->validateFields(UNFRIENDLY_ERROR, true); $langFieldError = $product->validateFieldsLang(UNFRIENDLY_ERROR, true); if ($fieldError === true and $langFieldError === true) { // check quantity if ($product->quantity == NULL) { $product->quantity = 0; } // If no id_product or update failed if ($update && $product->id) { $res = $product->update(); } else { $res = $product->add(); } $added_product_ids[] = $product->id; } if (isset($product->discount) && $product->discount > 0) { SpecificPrice::deleteByProductId((int) $product->id); $specificPrice = new SpecificPrice(); $specificPrice->id_product = (int) $product->id; $specificPrice->id_shop = (int) Shop::getCurrentShop(); $specificPrice->id_currency = 0; $specificPrice->id_country = 0; $specificPrice->id_group = 0; $specificPrice->from_quantity = 1; $specificPrice->reduction = $product->discount / 100; $specificPrice->reduction_type = 'percentage'; $specificPrice->from = '2012-01-01 00:00:00'; $specificPrice->to = '2016-01-01 00:00:00'; $specificPrice->price = $product->price; $specificPrice->add(); } if (isset($product->tags) and !empty($product->tags)) { // Delete tags for this id product, for no duplicating error Tag::deleteTagsForProduct($product->id); $tag = new Tag(); $tag->addTags($defaultLanguageId, $product->id, $tags); } if (isset($product->images) and is_array($product->images) and sizeof($product->images) and !$update || $overwrite_imgs == "on") { $product->deleteImages(); $first_image = true; foreach ($product->images as $image_name) { $image_name = trim($image_name); $image_path = IMAGE_UPLOAD_PATH . $image_name; if (!empty($image_name)) { $image = new Image(); $image->id_product = (int) $product->id; $image->position = Image::getHighestPosition($product->id) + 1; $image->cover = $first_image; $image->legend[$defaultLanguageId] = $product->name[$defaultLanguageId]; if (($fieldError = $image->validateFields(false, true)) === true and ($langFieldError = $image->validateFieldsLang(false, true)) === true and $image->add()) { if (!self::copyImg($product->id, $image->id, $image_path)) { $_warnings[] = Tools::displayError('Error copying image: ') . $image_path; } else { //delete the original image @unlink($image_path); } } else { $_warnings[] = $image->legend[$defaultLanguageId] . (isset($image->id_product) ? ' (' . $image->id_product . ')' : '') . ' ' . Tools::displayError('Cannot be saved'); $_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error(); } } $first_image = false; } } if (isset($product->id_category)) { $product->updateCategories(array_map('intval', $product->id_category)); } $this->addFeature($product->id, 'fabric', $product->fabric); $this->addFeature($product->id, 'color', $product->color); $this->addFeature($product->id, 'garment_type', $product->garment_type); $this->addFeature($product->id, 'work_type', $product->work_type); $this->addFeature($product->id, 'blouse_length', $product->blouse_length); $this->addFeature($product->id, 'wash_care', $product->wash_care); $this->addFeature($product->id, 'other_info', $product->other_info); // to avoid type errors in the catalog sheet - construct the string here again $shipping_sla = (int) preg_replace('/\\D/', '', $product->shipping_estimate); $shipping_estimate_str = ""; if ($shipping_sla > 0) { $shipping_estimate_str = $shipping_sla === 1 ? "Ready to be shipped in 1 day" : "Ready to be shipped in {$shipping_sla} days"; } $this->addFeature($product->id, 'shipping_estimate', $shipping_estimate_str); $this->addFeature($product->id, 'kameez_style', $product->kameez_style); $this->addFeature($product->id, 'salwar_style', $product->salwar_style); $this->addFeature($product->id, 'sleeves', $product->sleeves); $this->addFeature($product->id, 'generic_color', $product->generic_color); $this->addFeature($product->id, 'skirt_length', $product->skirt_length); $this->addFeature($product->id, 'dupatta_length', $product->dupatta_length); $this->addFeature($product->id, 'stone', $product->stone); $this->addFeature($product->id, 'plating', $product->plating); $this->addFeature($product->id, 'material', $product->material); $this->addFeature($product->id, 'dimensions', $product->dimensions); $this->addFeature($product->id, 'look', $product->look); $this->addFeature($product->id, 'handbag_occasion', $product->handbag_occasion); $this->addFeature($product->id, 'handbag_style', $product->handbag_style); $this->addFeature($product->id, 'handbag_material', $product->handbag_material); } $smarty->assign("products_affected", $products_to_import); //reindex the products SolrSearch::updateProducts($added_product_ids); $smarty->assign("is_update", $update); } else { $smarty->assign('file_error', 1); } } else { $smarty->assign('error_reading', 1); } }
public function delete() { /* * @since 1.5.0 * It is NOT possible to delete a product if there are currently: * - physical stock for this product * - supply order(s) for this product */ if (Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && $this->advanced_stock_management) { $stock_manager = StockManagerFactory::getManager(); $physical_quantity = $stock_manager->getProductPhysicalQuantities($this->id, 0); $real_quantity = $stock_manager->getProductRealQuantities($this->id, 0); if ($physical_quantity > 0) { return false; } if ($real_quantity > $physical_quantity) { return false; } $warehouse_product_locations = \PrestaShop\PrestaShop\Adapter\ServiceLocator::get('\\PrestaShop\\PrestaShop\\Core\\Foundation\\Database\\EntityManager')->getRepository('WarehouseProductLocation')->findByIdProduct($this->id); foreach ($warehouse_product_locations as $warehouse_product_location) { $warehouse_product_location->delete(); } $stocks = \PrestaShop\PrestaShop\Adapter\ServiceLocator::get('\\PrestaShop\\PrestaShop\\Core\\Foundation\\Database\\EntityManager')->getRepository('Stock')->findByIdProduct($this->id); foreach ($stocks as $stock) { $stock->delete(); } } $result = parent::delete(); // Removes the product from StockAvailable, for the current shop StockAvailable::removeProductFromStockAvailable($this->id); $result &= $this->deleteProductAttributes() && $this->deleteImages(); // If there are still entries in product_shop, don't remove completely the product if ($this->hasMultishopEntries()) { return true; } Hook::exec('actionProductDelete', array('id_product' => (int) $this->id, 'product' => $this)); if (!$result || !GroupReduction::deleteProductReduction($this->id) || !$this->deleteCategories(true) || !$this->deleteProductFeatures() || !$this->deleteTags() || !$this->deleteCartProducts() || !$this->deleteAttributesImpacts() || !$this->deleteAttachments(false) || !$this->deleteCustomization() || !SpecificPrice::deleteByProductId((int) $this->id) || !$this->deletePack() || !$this->deleteProductSale() || !$this->deleteSearchIndexes() || !$this->deleteAccessories() || !$this->deleteFromAccessories() || !$this->deleteFromSupplier() || !$this->deleteDownload() || !$this->deleteFromCartRules()) { return false; } return true; }
public function delete($force_delete = false) { // check if the product is not used in a non validated order if (!$force_delete && !$this->isDeletable()) { return false; } if (!$force_delete && !GroupReduction::deleteProductReduction($this->id)) { return false; } Hook::deleteProduct($this); if (!parent::delete() || !$this->deleteCategories(true) || !$this->deleteProductAttributes() || !$this->deleteProductFeatures() || !$this->deleteTags() || !$this->deleteCartProducts() || !$this->deleteAttachments() || !$this->deleteCustomization() || !SpecificPrice::deleteByProductId((int) $this->id) || !$this->deletePack() || !$this->deleteProductSale() || !$this->deleteSceneProducts() || !$this->deleteSearchIndexes() || !$this->deleteAccessories() || !$this->deleteFromAccessories()) { return false; } if (!_PS_MODE_DEMO_ && !$this->deleteImages()) { return false; } if ($id = ProductDownload::getIdFromIdProduct($this->id)) { if ($productDownload = new ProductDownload($id)) { if (!$productDownload->delete(true)) { return false; } } } return true; }
public function productImport() { global $cookie; $this->receiveTab(); $handle = $this->openCsvFile(); $defaultLanguageId = (int) Configuration::get('PS_LANG_DEFAULT'); self::setLocale(); $productIds = array(); for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, Tools::getValue('separator')); $current_line++) { if (Tools::getValue('convert')) { $line = $this->utf8_encode_array($line); } $info = self::getMaskedRow($line); if (array_key_exists('id', $info) and (int) $info['id'] and Product::existsInDatabase((int) $info['id'])) { $product = new Product((int) $info['id']); $categoryData = Product::getProductCategories((int) $product->id); foreach ($categoryData as $tmp) { $product->category[] = $tmp; } } else { $product = new Product(); } self::setEntityDefaultValues($product); self::array_walk($info, array('AdminImport', 'fillInfo'), $product); $trg_id = (int) $product->id_tax_rules_group; if ($product->id_tax_rules_group == 0 || !Validate::isLoadedObject(new TaxRulesGroup($trg_id))) { $this->_addProductWarning('id_tax_rules_group', $product->id_tax_rules_group, Tools::displayError('Invalid tax rule group ID, you first need a group with this ID.')); } else { $product->tax_rate = TaxRulesGroup::getTaxesRate((int) $product->id_tax_rules_group, Configuration::get('PS_COUNTRY_DEFAULT'), 0, 0); } if (isset($product->manufacturer) and is_numeric($product->manufacturer) and Manufacturer::manufacturerExists((int) $product->manufacturer)) { $product->id_manufacturer = (int) $product->manufacturer; } elseif (isset($product->manufacturer) and is_string($product->manufacturer) and !empty($product->manufacturer)) { if ($manufacturer = Manufacturer::getIdByName($product->manufacturer)) { $product->id_manufacturer = (int) $manufacturer; } else { $manufacturer = new Manufacturer(); $manufacturer->name = $product->manufacturer; if (($fieldError = $manufacturer->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $manufacturer->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true and $manufacturer->add()) { $product->id_manufacturer = (int) $manufacturer->id; } else { $this->_errors[] = $manufacturer->name . (isset($manufacturer->id) ? ' (' . $manufacturer->id . ')' : '') . ' ' . Tools::displayError('Cannot be saved'); $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error(); } } } if (isset($product->supplier) and is_numeric($product->supplier) and Supplier::supplierExists((int) $product->supplier)) { $product->id_supplier = (int) $product->supplier; } elseif (isset($product->supplier) and is_string($product->supplier) and !empty($product->supplier)) { if ($supplier = Supplier::getIdByName($product->supplier)) { $product->id_supplier = (int) $supplier; } else { $supplier = new Supplier(); $supplier->name = $product->supplier; if (($fieldError = $supplier->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $supplier->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true and $supplier->add()) { $product->id_supplier = (int) $supplier->id; } else { $this->_errors[] = $supplier->name . (isset($supplier->id) ? ' (' . $supplier->id . ')' : '') . ' ' . Tools::displayError('Cannot be saved'); $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error(); } } } if (isset($product->price_tex) and !isset($product->price_tin)) { $product->price = $product->price_tex; } elseif (isset($product->price_tin) and !isset($product->price_tex)) { $product->price = $product->price_tin; // If a tax is already included in price, withdraw it from price if ($product->tax_rate) { $product->price = (double) number_format($product->price / (1 + $product->tax_rate / 100), 6, '.', ''); } } elseif (isset($product->price_tin) and isset($product->price_tex)) { $product->price = $product->price_tex; } if (isset($product->category) and is_array($product->category) and sizeof($product->category)) { $product->id_category = array(); // Reset default values array foreach ($product->category as $value) { if (is_numeric($value)) { if (Category::categoryExists((int) $value)) { $product->id_category[] = (int) $value; } else { $categoryToCreate = new Category(); $categoryToCreate->id = (int) $value; $categoryToCreate->name = self::createMultiLangField($value); $categoryToCreate->active = 1; $categoryToCreate->id_parent = 1; // Default parent is home for unknown category to create if (($fieldError = $categoryToCreate->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $categoryToCreate->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true and $categoryToCreate->add()) { $product->id_category[] = (int) $categoryToCreate->id; } else { $this->_errors[] = $categoryToCreate->name[$defaultLanguageId] . (isset($categoryToCreate->id) ? ' (' . $categoryToCreate->id . ')' : '') . ' ' . Tools::displayError('Cannot be saved'); $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error(); } } } elseif (is_string($value) and !empty($value)) { $category = Category::searchByName($defaultLanguageId, $value, true); if ($category['id_category']) { $product->id_category[] = (int) $category['id_category']; } else { $categoryToCreate = new Category(); $categoryToCreate->name = self::createMultiLangField($value); $categoryToCreate->active = 1; $categoryToCreate->id_parent = 1; // Default parent is home for unknown category to create if (($fieldError = $categoryToCreate->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $categoryToCreate->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true and $categoryToCreate->add()) { $product->id_category[] = (int) $categoryToCreate->id; } else { $this->_errors[] = $categoryToCreate->name[$defaultLanguageId] . (isset($categoryToCreate->id) ? ' (' . $categoryToCreate->id . ')' : '') . ' ' . Tools::displayError('Cannot be saved'); $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error(); } } } } } $product->id_category_default = isset($product->id_category[0]) ? (int) $product->id_category[0] : ''; $link_rewrite = is_array($product->link_rewrite) && count($product->link_rewrite) ? $product->link_rewrite[$defaultLanguageId] : ''; $valid_link = Validate::isLinkRewrite($link_rewrite); if (isset($product->link_rewrite[$defaultLanguageId]) and empty($product->link_rewrite[$defaultLanguageId]) or !$valid_link) { $link_rewrite = Tools::link_rewrite($product->name[$defaultLanguageId]); if ($link_rewrite == '') { $link_rewrite = 'friendly-url-autogeneration-failed'; } } if (!$valid_link) { $this->_warnings[] = Tools::displayError('Rewrite link for') . ' ' . $link_rewrite . (isset($info['id']) ? ' (ID ' . $info['id'] . ') ' : '') . ' ' . Tools::displayError('was re-written as') . ' ' . $link_rewrite; } $product->link_rewrite = self::createMultiLangField($link_rewrite); $res = false; $fieldError = $product->validateFields(UNFRIENDLY_ERROR, true); $langFieldError = $product->validateFieldsLang(UNFRIENDLY_ERROR, true); if ($fieldError === true and $langFieldError === true) { // check quantity if ($product->quantity == NULL) { $product->quantity = 0; } // If id product AND id product already in base, trying to update if ($product->id and Product::existsInDatabase((int) $product->id)) { $datas = Db::getInstance()->getRow('SELECT `date_add` FROM `' . _DB_PREFIX_ . 'product` WHERE `id_product` = ' . (int) $product->id); $product->date_add = pSQL($datas['date_add']); $res = $product->update(); } // If no id_product or update failed if (!$res) { $res = $product->add(); } $productIds[] = $product->id; } // If both failed, mysql error if (!$res) { $this->_errors[] = $info['name'] . (isset($info['id']) ? ' (ID ' . $info['id'] . ')' : '') . ' ' . Tools::displayError('Cannot be saved'); $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error(); } else { // SpecificPrice (only the basic reduction feature is supported by the import) if (isset($info['reduction_price']) and $info['reduction_price'] > 0 or isset($info['reduction_percent']) and $info['reduction_percent'] > 0) { SpecificPrice::deleteByProductId((int) $product->id); $specificPrice = new SpecificPrice(); $specificPrice->id_product = (int) $product->id; $specificPrice->id_shop = (int) Shop::getCurrentShop(); $specificPrice->id_currency = 0; $specificPrice->id_country = 0; $specificPrice->id_group = 0; $specificPrice->price = 0.0; $specificPrice->from_quantity = 1; $specificPrice->reduction = (isset($info['reduction_price']) and $info['reduction_price']) ? $info['reduction_price'] : $info['reduction_percent'] / 100; $specificPrice->reduction_type = (isset($info['reduction_price']) and $info['reduction_price']) ? 'amount' : 'percentage'; $specificPrice->from = (isset($info['reduction_from']) and Validate::isDate($info['reduction_from'])) ? $info['reduction_from'] : '0000-00-00 00:00:00'; $specificPrice->to = (isset($info['reduction_to']) and Validate::isDate($info['reduction_to'])) ? $info['reduction_to'] : '0000-00-00 00:00:00'; if (!$specificPrice->add()) { $this->_addProductWarning($info['name'], $product->id, $this->l('Discount is invalid')); } } if (isset($product->tags) and !empty($product->tags)) { // Delete tags for this id product, for no duplicating error Tag::deleteTagsForProduct($product->id); $tag = new Tag(); if (!is_array($product->tags)) { $product->tags = self::createMultiLangField($product->tags); foreach ($product->tags as $key => $tags) { $isTagAdded = $tag->addTags($key, $product->id, $tags); if (!$isTagAdded) { $this->_addProductWarning($info['name'], $product->id, $this->l('Tags list') . ' ' . $this->l('is invalid')); break; } } } else { foreach ($product->tags as $key => $tags) { $str = ''; foreach ($tags as $one_tag) { $str .= $one_tag . ','; } $str = rtrim($str, ','); $isTagAdded = $tag->addTags($key, $product->id, $str); if (!$isTagAdded) { $this->_addProductWarning($info['name'], $product->id, 'Invalid tag(s) (' . $str . ')'); break; } } } } //delete existing images if "delete_existing_images" is set to 1 if (isset($product->delete_existing_images)) { if ((bool) $product->delete_existing_images) { $product->deleteImages(); } elseif (isset($product->image) and is_array($product->image) and sizeof($product->image)) { $product->deleteImages(); } } if (isset($product->image) and is_array($product->image) and sizeof($product->image)) { $productHasImages = (bool) Image::getImages((int) $cookie->id_lang, (int) $product->id); foreach ($product->image as $key => $url) { if (!empty($url)) { $image = new Image(); $image->id_product = (int) $product->id; $image->position = Image::getHighestPosition($product->id) + 1; $image->cover = (!$key and !$productHasImages) ? true : false; $image->legend = self::createMultiLangField($product->name[$defaultLanguageId]); if (($fieldError = $image->validateFields(UNFRIENDLY_ERROR, true)) === true and ($langFieldError = $image->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true and $image->add()) { if (!self::copyImg($product->id, $image->id, $url)) { $this->_warnings[] = Tools::displayError('Error copying image: ') . $url; } } else { $this->_warnings[] = $image->legend[$defaultLanguageId] . (isset($image->id_product) ? ' (' . $image->id_product . ')' : '') . ' ' . Tools::displayError('Cannot be saved'); $this->_errors[] = ($fieldError !== true ? $fieldError : '') . ($langFieldError !== true ? $langFieldError : '') . mysql_error(); } } } } if (isset($product->id_category)) { $product->updateCategories(array_map('intval', $product->id_category)); } $features = get_object_vars($product); foreach ($features as $feature => $value) { if (!strncmp($feature, '#F_', 3) and Tools::strlen($product->{$feature})) { $feature_name = str_replace('#F_', '', $feature); $id_feature = Feature::addFeatureImport($feature_name); $id_feature_value = FeatureValue::addFeatureValueImport($id_feature, $product->{$feature}); Product::addFeatureProductImport($product->id, $id_feature, $id_feature_value); } } } } $this->closeCsvFile($handle); if (sizeof($productIds)) { SolrSearch::updateProducts($productIds); } }