/** * @see ObjectModel::delete() */ public function delete() { $res = parent::delete(); if ($res && $this->id_product_attribute == 0) { $items = ProductSupplier::getSupplierCollection($this->id_product, false); foreach ($items as $item) { if ($item->id_product_attribute > 0) { $item->delete(); } } } return $res; }
protected function setDetailProductPrice(Order $order, Cart $cart, $product) { $this->setContext((int) $product['id_shop']); $specific_price = $null = null; Product::getPriceStatic((int) $product['id_product'], true, (int) $product['id_product_attribute'], 6, null, false, true, array($product['cart_quantity'], $product['cart_quantity_fractional']), false, (int) $order->id_customer, (int) $order->id_cart, (int) $order->{Configuration::get('PS_TAX_ADDRESS_TYPE')}, $specific_price, true, true, $this->context); $this->specificPrice = $specific_price; $this->original_product_price = Product::getPriceStatic($product['id_product'], false, (int) $product['id_product_attribute'], 6, null, false, false, 1, false, null, null, null, $null, true, true, $this->context); $this->product_price = $this->original_product_price; $this->unit_price_tax_incl = (double) $product['price_wt']; $this->unit_price_tax_excl = (double) $product['price']; $this->total_price_tax_incl = (double) $product['total_wt']; $this->total_price_tax_excl = (double) $product['total']; $this->purchase_supplier_price = (double) $product['wholesale_price']; if ($product['id_supplier'] > 0 && ($supplier_price = ProductSupplier::getProductPrice((int) $product['id_supplier'], $product['id_product'], $product['id_product_attribute'], true)) > 0) { $this->purchase_supplier_price = (double) $supplier_price; } $this->setSpecificPrice($order, $product); $this->group_reduction = (double) Group::getReduction((int) $order->id_customer); $shop_id = $this->context->shop->id; $quantity_discount = SpecificPrice::getQuantityDiscount((int) $product['id_product'], $shop_id, (int) $cart->id_currency, (int) $this->vat_address->id_country, (int) $this->customer->id_default_group, (int) PP::resolveQty($product['cart_quantity'], $product['cart_quantity_fractional']), false, null, null, $null, true, true, $this->context); $unit_price = Product::getPriceStatic((int) $product['id_product'], true, $product['id_product_attribute'] ? (int) $product['id_product_attribute'] : null, 2, null, false, true, 1, false, (int) $order->id_customer, null, (int) $order->{Configuration::get('PS_TAX_ADDRESS_TYPE')}, $null, true, true, $this->context); $this->product_quantity_discount = 0.0; if ($quantity_discount) { $this->product_quantity_discount = $unit_price; if (Product::getTaxCalculationMethod((int) $order->id_customer) == PS_TAX_EXC) { $this->product_quantity_discount = Tools::ps_round($unit_price, 2); } if (isset($this->tax_calculator)) { $this->product_quantity_discount -= $this->tax_calculator->addTaxes($quantity_discount['price']); } } $this->discount_quantity_applied = $this->specificPrice && $this->specificPrice['from_quantity'] > PP::getSpecificPriceFromQty((int) $product['id_product']) ? 1 : 0; $this->id_cart_product = (int) $product['id_cart_product']; $this->product_quantity_fractional = (double) $product['cart_quantity_fractional']; $ppropertiessmartprice_hook3 = null; }
public function initFormSuppliers($obj) { $data = $this->createTemplate($this->tpl_form); if ($obj->id) { if ($this->product_exists_in_shop) { // Get all id_product_attribute $attributes = $obj->getAttributesResume($this->context->language->id); if (empty($attributes)) { $attributes[] = array('id_product' => $obj->id, 'id_product_attribute' => 0, 'attribute_designation' => ''); } $product_designation = array(); foreach ($attributes as $attribute) { $product_designation[$attribute['id_product_attribute']] = rtrim($obj->name[$this->context->language->id] . ' - ' . $attribute['attribute_designation'], ' - '); } // Get all available suppliers $suppliers = Supplier::getSuppliers(); // Get already associated suppliers $associated_suppliers = ProductSupplier::getSupplierCollection($obj->id); // Get already associated suppliers and force to retreive product declinaisons $product_supplier_collection = ProductSupplier::getSupplierCollection($obj->id, false); $default_supplier = 0; foreach ($suppliers as &$supplier) { $supplier['is_selected'] = false; $supplier['is_default'] = false; foreach ($associated_suppliers as $associated_supplier) { if ($associated_supplier->id_supplier == $supplier['id_supplier']) { $associated_supplier->name = $supplier['name']; $supplier['is_selected'] = true; if ($obj->id_supplier == $supplier['id_supplier']) { $supplier['is_default'] = true; $default_supplier = $supplier['id_supplier']; } } } } $data->assign(array('attributes' => $attributes, 'suppliers' => $suppliers, 'default_supplier' => $default_supplier, 'associated_suppliers' => $associated_suppliers, 'associated_suppliers_collection' => $product_supplier_collection, 'product_designation' => $product_designation, 'currencies' => Currency::getCurrencies(), 'product' => $obj, 'link' => $this->context->link, 'token' => $this->token, 'id_default_currency' => Configuration::get('PS_CURRENCY_DEFAULT'))); } else { $this->displayWarning($this->l('You must save the product in this shop before managing suppliers.')); } } else { $this->displayWarning($this->l('You must save this product before managing suppliers.')); } $this->tpl_form_vars['custom_form'] = $data->fetch(); }
protected function supplyOrdersDetailsImportOne($info, &$products, &$reset, $force_ids, $current_line, $validateOnly = false) { // sets default values if needed AdminImportController::setDefaultValues($info); // gets the supply order if (array_key_exists('supply_order_reference', $info) && pSQL($info['supply_order_reference']) && SupplyOrder::exists(pSQL($info['supply_order_reference']))) { $supply_order = SupplyOrder::getSupplyOrderByReference(pSQL($info['supply_order_reference'])); } else { $this->errors[] = sprintf($this->l('Supply Order (%s) could not be loaded (at line %d).'), $info['supply_order_reference'], $current_line + 1); } if (empty($this->errors)) { // sets parameters $id_product = (int) $info['id_product']; if (!$info['id_product_attribute']) { $info['id_product_attribute'] = 0; } $id_product_attribute = (int) $info['id_product_attribute']; $unit_price_te = (double) $info['unit_price_te']; $quantity_expected = (int) $info['quantity_expected']; $discount_rate = (double) $info['discount_rate']; $tax_rate = (double) $info['tax_rate']; // checks if one product/attribute is there only once if (isset($products[$id_product][$id_product_attribute])) { $this->errors[] = sprintf($this->l('Product/Attribute (%d/%d) cannot be added twice (at line %d).'), $id_product, $id_product_attribute, $current_line + 1); } else { $products[$id_product][$id_product_attribute] = $quantity_expected; } // checks parameters if (false === ($supplier_reference = ProductSupplier::getProductSupplierReference($id_product, $id_product_attribute, $supply_order->id_supplier))) { $this->errors[] = sprintf($this->l('Product (%d/%d) is not available for this order (at line %d).'), $id_product, $id_product_attribute, $current_line + 1); } if ($unit_price_te < 0) { $this->errors[] = sprintf($this->l('Unit Price (tax excl.) (%d) is not valid (at line %d).'), $unit_price_te, $current_line + 1); } if ($quantity_expected < 0) { $this->errors[] = sprintf($this->l('Quantity Expected (%d) is not valid (at line %d).'), $quantity_expected, $current_line + 1); } if ($discount_rate < 0 || $discount_rate > 100) { $this->errors[] = sprintf($this->l('Discount rate (%d) is not valid (at line %d). %s.'), $discount_rate, $current_line + 1, $this->l('Format: Between 0 and 100')); } if ($tax_rate < 0 || $tax_rate > 100) { $this->errors[] = sprintf($this->l('Quantity Expected (%d) is not valid (at line %d).'), $tax_rate, $current_line + 1, $this->l('Format: Between 0 and 100')); } // if no errors, sets supply order details if (empty($this->errors)) { // resets order if needed if (!$validateOnly && $reset) { $supply_order->resetProducts(); $reset = false; } // creates new product $supply_order_detail = new SupplyOrderDetail(); AdminImportController::arrayWalk($info, array('AdminImportController', 'fillInfo'), $supply_order_detail); // sets parameters $supply_order_detail->id_supply_order = $supply_order->id; $currency = new Currency($supply_order->id_ref_currency); $supply_order_detail->id_currency = $currency->id; $supply_order_detail->exchange_rate = $currency->conversion_rate; $supply_order_detail->supplier_reference = $supplier_reference; $supply_order_detail->name = Product::getProductName($id_product, $id_product_attribute, $supply_order->id_lang); // gets ean13 / ref / upc $query = new DbQuery(); $query->select(' IFNULL(pa.reference, IFNULL(p.reference, \'\')) as reference, IFNULL(pa.ean13, IFNULL(p.ean13, \'\')) as ean13, IFNULL(pa.upc, IFNULL(p.upc, \'\')) as upc '); $query->from('product', 'p'); $query->leftJoin('product_attribute', 'pa', 'pa.id_product = p.id_product AND id_product_attribute = ' . (int) $id_product_attribute); $query->where('p.id_product = ' . (int) $id_product); $query->where('p.is_virtual = 0 AND p.cache_is_pack = 0'); $res = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query); $product_infos = $res['0']; $supply_order_detail->reference = $product_infos['reference']; $supply_order_detail->ean13 = $product_infos['ean13']; $supply_order_detail->upc = $product_infos['upc']; $supply_order_detail->force_id = (bool) $force_ids; if (!$validateOnly) { $supply_order_detail->add(); $supply_order->update(); } unset($supply_order_detail); } } }
/** * method call when ajax request is made for search product to add to the order * @TODO - Update this method to retreive the reference, ean13, upc corresponding to a product attribute */ public function ajaxProcessSearchProduct() { // Get the search pattern $pattern = pSQL(Tools::getValue('q', false)); if (!$pattern || $pattern == '' || strlen($pattern) < 1) { die; } // get supplier id $id_supplier = (int) Tools::getValue('id_supplier', false); // gets the currency $id_currency = (int) Tools::getValue('id_currency', false); // get lang from context $id_lang = (int) Context::getContext()->language->id; $query = new DbQuery(); $query->select(' CONCAT(p.id_product, \'_\', IFNULL(pa.id_product_attribute, \'0\')) as id, ps.product_supplier_reference as supplier_reference, IFNULL(pa.reference, IFNULL(p.reference, \'\')) as reference, IFNULL(pa.ean13, IFNULL(p.ean13, \'\')) as ean13, IFNULL(pa.upc, IFNULL(p.upc, \'\')) as upc, md5(CONCAT(\'' . _COOKIE_KEY_ . '\', p.id_product, \'_\', IFNULL(pa.id_product_attribute, \'0\'))) as checksum, IFNULL(CONCAT(pl.name, \' : \', GROUP_CONCAT(DISTINCT agl.name, \' - \', al.name order by agl.name SEPARATOR \', \')), pl.name) as name '); $query->from('product', 'p'); $query->innerJoin('product_lang', 'pl', 'pl.id_product = p.id_product AND pl.id_lang = ' . $id_lang); $query->leftJoin('product_attribute', 'pa', 'pa.id_product = p.id_product'); $query->leftJoin('product_attribute_combination', 'pac', 'pac.id_product_attribute = pa.id_product_attribute'); $query->leftJoin('attribute', 'atr', 'atr.id_attribute = pac.id_attribute'); $query->leftJoin('attribute_lang', 'al', 'al.id_attribute = atr.id_attribute AND al.id_lang = ' . $id_lang); $query->leftJoin('attribute_group_lang', 'agl', 'agl.id_attribute_group = atr.id_attribute_group AND agl.id_lang = ' . $id_lang); $query->leftJoin('product_supplier', 'ps', 'ps.id_product = p.id_product AND ps.id_product_attribute = IFNULL(pa.id_product_attribute, 0)'); $query->where('(pl.name LIKE \'%' . $pattern . '%\' OR p.reference LIKE \'%' . $pattern . '%\' OR ps.product_supplier_reference LIKE \'%' . $pattern . '%\')'); $query->where('p.id_product NOT IN (SELECT pd.id_product FROM `' . _DB_PREFIX_ . 'product_download` pd WHERE (pd.id_product = p.id_product))'); $query->where('p.is_virtual = 0 AND p.cache_is_pack = 0'); if ($id_supplier) { $query->where('ps.id_supplier = ' . $id_supplier . ' OR p.id_supplier = ' . $id_supplier); } $query->groupBy('p.id_product, pa.id_product_attribute'); $items = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query); foreach ($items as &$item) { $ids = explode('_', $item['id']); $prices = ProductSupplier::getProductSupplierPrice($ids[0], $ids[1], $id_supplier, true); if (count($prices)) { $item['unit_price_te'] = Tools::convertPriceFull($prices['product_supplier_price_te'], new Currency((int) $prices['id_currency']), new Currency($id_currency)); } } if ($items) { die(Tools::jsonEncode($items)); } die(1); }
/** * Sets Supplier Reference * * @param int $id_supplier * @param int $id_product_attribute * @param string $supplier_reference * @param float $price * @param int $id_currency */ public function addSupplierReference($id_supplier, $id_product_attribute, $supplier_reference = null, $price = null, $id_currency = null) { //in some case we need to add price without supplier reference if ($supplier_reference === null) { $supplier_reference = ''; } //Try to set the default supplier reference if ($id_supplier > 0) { $id_product_supplier = (int) ProductSupplier::getIdByProductAndSupplier($this->id, $id_product_attribute, $id_supplier); if (!$id_product_supplier) { //create new record $product_supplier_entity = new ProductSupplier(); $product_supplier_entity->id_product = (int) $this->id; $product_supplier_entity->id_product_attribute = (int) $id_product_attribute; $product_supplier_entity->id_supplier = (int) $id_supplier; $product_supplier_entity->product_supplier_reference = pSQL($supplier_reference); $product_supplier_entity->product_supplier_price_te = (int) $price; $product_supplier_entity->id_currency = (int) $id_currency; $product_supplier_entity->save(); } else { $product_supplier = new ProductSupplier((int) $id_product_supplier); $product_supplier->product_supplier_reference = pSQL($supplier_reference); $product_supplier->update(); } } }
/** * Sets or updates Supplier Reference * * @param int $id_supplier * @param int $id_product_attribute * @param string $supplier_reference * @param float $price * @param int $id_currency */ public function addSupplierReference($id_supplier, $id_product_attribute, $supplier_reference = null, $price = null, $id_currency = null) { //in some case we need to add price without supplier reference if ($supplier_reference === null) { $supplier_reference = ''; } //Try to set the default supplier reference if ($id_supplier > 0 && $this->id > 0) { $id_product_supplier = (int) ProductSupplier::getIdByProductAndSupplier($this->id, $id_product_attribute, $id_supplier); $product_supplier = new ProductSupplier($id_product_supplier); if (!$id_product_supplier) { $product_supplier->id_product = (int) $this->id; $product_supplier->id_product_attribute = (int) $id_product_attribute; $product_supplier->id_supplier = (int) $id_supplier; } $product_supplier->product_supplier_reference = pSQL($supplier_reference); $product_supplier->product_supplier_price_te = !is_null($price) ? (double) $price : (double) $product_supplier->product_supplier_price_te; $product_supplier->id_currency = !is_null($id_currency) ? (int) $id_currency : (int) $product_supplier->id_currency; $product_supplier->save(); } }
public function productImport() { $this->receiveTab(); $handle = $this->openCsvFile(); $default_language_id = (int) Configuration::get('PS_LANG_DEFAULT'); $id_lang = Language::getIdByIso(Tools::getValue('iso_lang')); if (!Validate::isUnsignedId($id_lang)) { $id_lang = $default_language_id; } AdminImportController::setLocale(); $shop_ids = Shop::getCompleteListOfShopsID(); for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, $this->separator); $current_line++) { if (Tools::getValue('convert')) { $line = $this->utf8EncodeArray($line); } $info = AdminImportController::getMaskedRow($line); if (self::ignoreRow($info)) { continue; } if (Tools::getValue('forceIDs') && isset($info['id']) && (int) $info['id']) { $product = new Product((int) $info['id']); } elseif (Tools::getValue('match_ref') && array_key_exists('reference', $info)) { $datas = Db::getInstance()->getRow(' SELECT p.`id_product` FROM `' . _DB_PREFIX_ . 'product` p ' . Shop::addSqlAssociation('product', 'p') . ' WHERE p.`reference` = "' . pSQL($info['reference']) . '" '); if (isset($datas['id_product']) && $datas['id_product']) { $product = new Product((int) $datas['id_product']); } else { $product = new Product(); } } else { if (array_key_exists('id', $info) && is_string($info['id'])) { $prod = self::findProductByName($default_language_id, $info['id'], $info['name']); if ($prod['id_product']) { $info['id'] = (int) $prod['id_product']; } } if (array_key_exists('id', $info) && (int) $info['id'] && Product::existsInDatabase((int) $info['id'], 'product')) { $product = new Product((int) $info['id']); $product->loadStockData(); $category_data = Product::getProductCategories((int) $product->id); if (is_array($category_data)) { foreach ($category_data as $tmp) { if (!isset($product->category) || !$product->category || is_array($product->category)) { $product->category[] = $tmp; } } } } else { $product = new Product(); } } AdminImportController::setEntityDefaultValues($product); AdminImportController::arrayWalk($info, array('AdminImportController', 'fillInfo'), $product); if (!Shop::isFeatureActive()) { $product->shop = 1; } elseif (!isset($product->shop) || empty($product->shop)) { $product->shop = implode($this->multiple_value_separator, Shop::getContextListShopID()); } if (!Shop::isFeatureActive()) { $product->id_shop_default = 1; } else { $product->id_shop_default = (int) Context::getContext()->shop->id; } // link product to shops $product->id_shop_list = array(); foreach (explode($this->multiple_value_separator, $product->shop) as $shop) { if (!empty($shop) && !is_numeric($shop)) { $product->id_shop_list[] = Shop::getIdByName($shop); } elseif (!empty($shop)) { $product->id_shop_list[] = $shop; } } if ((int) $product->id_tax_rules_group != 0) { if (Validate::isLoadedObject(new TaxRulesGroup($product->id_tax_rules_group))) { $address = $this->context->shop->getAddress(); $tax_manager = TaxManagerFactory::getManager($address, $product->id_tax_rules_group); $product_tax_calculator = $tax_manager->getTaxCalculator(); $product->tax_rate = $product_tax_calculator->getTotalRate(); } else { $this->addProductWarning('id_tax_rules_group', $product->id_tax_rules_group, Tools::displayError('Invalid tax rule group ID. You first need to create a group with this ID.')); } } if (isset($product->manufacturer) && is_numeric($product->manufacturer) && Manufacturer::manufacturerExists((int) $product->manufacturer)) { $product->id_manufacturer = (int) $product->manufacturer; } elseif (isset($product->manufacturer) && is_string($product->manufacturer) && !empty($product->manufacturer)) { if ($manufacturer = Manufacturer::getIdByName($product->manufacturer)) { $product->id_manufacturer = (int) $manufacturer; } else { $manufacturer = new Manufacturer(); $manufacturer->name = $product->manufacturer; if (($field_error = $manufacturer->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $manufacturer->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && $manufacturer->add()) { $product->id_manufacturer = (int) $manufacturer->id; } else { $this->errors[] = sprintf(Tools::displayError('%1$s (ID: %2$s) cannot be saved'), $manufacturer->name, isset($manufacturer->id) && !empty($manufacturer->id) ? $manufacturer->id : 'null'); $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError(); } } } if (isset($product->supplier) && is_numeric($product->supplier) && Supplier::supplierExists((int) $product->supplier)) { $product->id_supplier = (int) $product->supplier; } elseif (isset($product->supplier) && is_string($product->supplier) && !empty($product->supplier)) { if ($supplier = Supplier::getIdByName($product->supplier)) { $product->id_supplier = (int) $supplier; } else { $supplier = new Supplier(); $supplier->name = $product->supplier; $supplier->active = true; if (($field_error = $supplier->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $supplier->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && $supplier->add()) { $product->id_supplier = (int) $supplier->id; $supplier->associateTo($product->id_shop_list); } else { $this->errors[] = sprintf(Tools::displayError('%1$s (ID: %2$s) cannot be saved'), $supplier->name, isset($supplier->id) && !empty($supplier->id) ? $supplier->id : 'null'); $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError(); } } } if (isset($product->price_tex) && !isset($product->price_tin)) { $product->price = $product->price_tex; } elseif (isset($product->price_tin) && !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) && isset($product->price_tex)) { $product->price = $product->price_tex; } if (!Configuration::get('PS_USE_ECOTAX')) { $product->ecotax = 0; } $properties = $product->productProperties(); if ((double) $properties['pp_unit_price_ratio'] > 0) { $product->unit_price = (double) $product->price / (double) $properties['pp_unit_price_ratio']; } if (isset($product->category) && is_array($product->category) && count($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 { $category_to_create = new Category(); $category_to_create->id = (int) $value; $category_to_create->name = AdminImportController::createMultiLangField($value); $category_to_create->active = 1; $category_to_create->id_parent = Configuration::get('PS_HOME_CATEGORY'); // Default parent is home for unknown category to create $category_link_rewrite = Tools::link_rewrite($category_to_create->name[$default_language_id]); $category_to_create->link_rewrite = AdminImportController::createMultiLangField($category_link_rewrite); if (($field_error = $category_to_create->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $category_to_create->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && $category_to_create->add()) { $product->id_category[] = (int) $category_to_create->id; } else { $this->errors[] = sprintf(Tools::displayError('%1$s (ID: %2$s) cannot be saved'), $category_to_create->name[$default_language_id], isset($category_to_create->id) && !empty($category_to_create->id) ? $category_to_create->id : 'null'); $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError(); } } } elseif (is_string($value) && !empty($value)) { $category = Category::searchByPath($default_language_id, trim($value), $this, 'productImportCreateCat'); if ($category['id_category']) { $product->id_category[] = (int) $category['id_category']; } else { $this->errors[] = sprintf(Tools::displayError('%1$s cannot be saved'), trim($value)); } } } $product->id_category = array_values(array_unique($product->id_category)); } if (!isset($product->id_category_default) || !$product->id_category_default) { $product->id_category_default = isset($product->id_category[0]) ? (int) $product->id_category[0] : (int) Configuration::get('PS_HOME_CATEGORY'); } $link_rewrite = is_array($product->link_rewrite) && isset($product->link_rewrite[$id_lang]) ? trim($product->link_rewrite[$id_lang]) : ''; $valid_link = Validate::isLinkRewrite($link_rewrite); if (isset($product->link_rewrite[$id_lang]) && empty($product->link_rewrite[$id_lang]) || !$valid_link) { $link_rewrite = Tools::link_rewrite($product->name[$id_lang]); if ($link_rewrite == '') { $link_rewrite = 'friendly-url-autogeneration-failed'; } } if (!$valid_link) { $this->warnings[] = sprintf(Tools::displayError('Rewrite link for %1$s (ID: %2$s) was re-written as %3$s.'), $product->name[$id_lang], isset($info['id']) && !empty($info['id']) ? $info['id'] : 'null', $link_rewrite); } if (!(Tools::getValue('match_ref') || Tools::getValue('forceIDs')) || !(is_array($product->link_rewrite) && count($product->link_rewrite) && !empty($product->link_rewrite[$id_lang]))) { $product->link_rewrite = AdminImportController::createMultiLangField($link_rewrite); } // replace the value of separator by coma if ($this->multiple_value_separator != ',') { if (is_array($product->meta_keywords)) { foreach ($product->meta_keywords as &$meta_keyword) { if (!empty($meta_keyword)) { $meta_keyword = str_replace($this->multiple_value_separator, ',', $meta_keyword); } } } } // Convert comma into dot for all floating values foreach (Product::$definition['fields'] as $key => $array) { if ($array['type'] == Product::TYPE_FLOAT) { $product->{$key} = str_replace(',', '.', $product->{$key}); } } // Indexation is already 0 if it's a new product, but not if it's an update $product->indexed = 0; $res = false; $field_error = $product->validateFields(UNFRIENDLY_ERROR, true); $lang_field_error = $product->validateFieldsLang(UNFRIENDLY_ERROR, true); if ($field_error === true && $lang_field_error === true) { // check quantity if ($product->quantity == null) { $product->quantity = 0; } // If match ref is specified && ref product && ref product already in base, trying to update if (Tools::getValue('match_ref') && $product->reference && $product->existsRefInDatabase($product->reference)) { $datas = Db::getInstance()->getRow(' SELECT product_shop.`date_add`, p.`id_product` FROM `' . _DB_PREFIX_ . 'product` p ' . Shop::addSqlAssociation('product', 'p') . ' WHERE p.`reference` = "' . pSQL($product->reference) . '" '); $product->id = (int) $datas['id_product']; $product->date_add = pSQL($datas['date_add']); $res = $product->update(); } elseif ($product->id && Product::existsInDatabase((int) $product->id, 'product')) { $datas = Db::getInstance()->getRow(' SELECT product_shop.`date_add` FROM `' . _DB_PREFIX_ . 'product` p ' . Shop::addSqlAssociation('product', 'p') . ' WHERE p.`id_product` = ' . (int) $product->id); $product->date_add = pSQL($datas['date_add']); $res = $product->update(); } // If no id_product or update failed $product->force_id = (bool) Tools::getValue('forceIDs'); if (!$res) { if (isset($product->date_add) && $product->date_add != '') { $res = $product->add(false); } else { $res = $product->add(); } } if ($product->getType() == Product::PTYPE_VIRTUAL) { StockAvailable::setProductOutOfStock((int) $product->id, 1); } else { StockAvailable::setProductOutOfStock((int) $product->id, (int) $product->out_of_stock); } } $shops = array(); $product_shop = explode($this->multiple_value_separator, $product->shop); foreach ($product_shop as $shop) { if (empty($shop)) { continue; } $shop = trim($shop); if (!empty($shop) && !is_numeric($shop)) { $shop = Shop::getIdByName($shop); } if (in_array($shop, $shop_ids)) { $shops[] = $shop; } else { $this->addProductWarning(Tools::safeOutput($info['name']), $product->id, $this->l('Shop is not valid')); } } if (empty($shops)) { $shops = Shop::getContextListShopID(); } // If both failed, mysql error if (!$res) { $this->errors[] = sprintf(Tools::displayError('%1$s (ID: %2$s) cannot be saved'), isset($info['name']) && !empty($info['name']) ? Tools::safeOutput($info['name']) : 'No Name', isset($info['id']) && !empty($info['id']) ? Tools::safeOutput($info['id']) : 'No ID'); $this->errors[] = ($field_error !== true ? $field_error : '') . (isset($lang_field_error) && $lang_field_error !== true ? $lang_field_error : '') . Db::getInstance()->getMsgError(); } else { // Product supplier 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(); } // SpecificPrice (only the basic reduction feature is supported by the import) if (!Shop::isFeatureActive()) { $info['shop'] = 1; } elseif (!isset($info['shop']) || empty($info['shop'])) { $info['shop'] = implode($this->multiple_value_separator, Shop::getContextListShopID()); } // Get shops for each attributes $info['shop'] = explode($this->multiple_value_separator, $info['shop']); $id_shop_list = array(); foreach ($info['shop'] as $shop) { if (!empty($shop) && !is_numeric($shop)) { $id_shop_list[] = (int) Shop::getIdByName($shop); } elseif (!empty($shop)) { $id_shop_list[] = $shop; } } if (isset($info['reduction_price']) && $info['reduction_price'] > 0 || isset($info['reduction_percent']) && $info['reduction_percent'] > 0) { foreach ($id_shop_list as $id_shop) { $specific_price = SpecificPrice::getSpecificPrice($product->id, $id_shop, 0, 0, 0, 1, 0, 0, 0, 0); if (is_array($specific_price) && isset($specific_price['id_specific_price'])) { $specific_price = new SpecificPrice((int) $specific_price['id_specific_price']); } else { $specific_price = new SpecificPrice(); } $specific_price->id_product = (int) $product->id; $specific_price->id_specific_price_rule = 0; $specific_price->id_shop = $id_shop; $specific_price->id_currency = 0; $specific_price->id_country = 0; $specific_price->id_group = 0; $specific_price->price = -1; $specific_price->id_customer = 0; $specific_price->from_quantity = 1; $specific_price->reduction = isset($info['reduction_price']) && $info['reduction_price'] ? $info['reduction_price'] : $info['reduction_percent'] / 100; $specific_price->reduction_type = isset($info['reduction_price']) && $info['reduction_price'] ? 'amount' : 'percentage'; $specific_price->from = isset($info['reduction_from']) && Validate::isDate($info['reduction_from']) ? $info['reduction_from'] : '0000-00-00 00:00:00'; $specific_price->to = isset($info['reduction_to']) && Validate::isDate($info['reduction_to']) ? $info['reduction_to'] : '0000-00-00 00:00:00'; if (!$specific_price->save()) { $this->addProductWarning(Tools::safeOutput($info['name']), $product->id, $this->l('Discount is invalid')); } } } if (isset($product->tags) && !empty($product->tags)) { if (isset($product->id) && $product->id) { $tags = Tag::getProductTags($product->id); if (is_array($tags) && count($tags)) { if (!empty($product->tags)) { $product->tags = explode($this->multiple_value_separator, $product->tags); } if (is_array($product->tags) && count($product->tags)) { foreach ($product->tags as $key => $tag) { if (!empty($tag)) { $product->tags[$key] = trim($tag); } } $tags[$id_lang] = $product->tags; $product->tags = $tags; } } } // Delete tags for this id product, for no duplicating error Tag::deleteTagsForProduct($product->id); if (!is_array($product->tags) && !empty($product->tags)) { $product->tags = AdminImportController::createMultiLangField($product->tags); foreach ($product->tags as $key => $tags) { $is_tag_added = Tag::addTags($key, $product->id, $tags, $this->multiple_value_separator); if (!$is_tag_added) { $this->addProductWarning(Tools::safeOutput($info['name']), $product->id, $this->l('Tags list is invalid')); break; } } } else { foreach ($product->tags as $key => $tags) { $str = ''; foreach ($tags as $one_tag) { $str .= $one_tag . $this->multiple_value_separator; } $str = rtrim($str, $this->multiple_value_separator); $is_tag_added = Tag::addTags($key, $product->id, $str, $this->multiple_value_separator); if (!$is_tag_added) { $this->addProductWarning(Tools::safeOutput($info['name']), (int) $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(); } } if (isset($product->image) && is_array($product->image) && count($product->image)) { $product_has_images = (bool) Image::getImages($this->context->language->id, (int) $product->id); foreach ($product->image as $key => $url) { $url = trim($url); $error = false; if (!empty($url)) { $url = str_replace(' ', '%20', $url); $image = new Image(); $image->id_product = (int) $product->id; $image->position = Image::getHighestPosition($product->id) + 1; $image->cover = !$key && !$product_has_images ? true : false; // file_exists doesn't work with HTTP protocol if (($field_error = $image->validateFields(UNFRIENDLY_ERROR, true)) === true && ($lang_field_error = $image->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && $image->add()) { // associate image to selected shops $image->associateTo($shops); if (!AdminImportController::copyImg($product->id, $image->id, $url, 'products', !Tools::getValue('regenerate'))) { $image->delete(); $this->warnings[] = sprintf(Tools::displayError('Error copying image: %s'), $url); } } else { $error = true; } } else { $error = true; } if ($error) { $this->warnings[] = sprintf(Tools::displayError('Product #%1$d: the picture (%2$s) cannot be saved.'), $image->id_product, $url); } } } if (isset($product->id_category) && is_array($product->id_category)) { $product->updateCategories(array_map('intval', $product->id_category)); } $product->checkDefaultAttributes(); if (!$product->cache_default_attribute) { Product::updateDefaultAttribute($product->id); } // Features import $features = get_object_vars($product); if (isset($features['features']) && !empty($features['features'])) { foreach (explode($this->multiple_value_separator, $features['features']) as $single_feature) { if (empty($single_feature)) { continue; } $tab_feature = explode(':', $single_feature); $feature_name = isset($tab_feature[0]) ? trim($tab_feature[0]) : ''; $feature_value = isset($tab_feature[1]) ? trim($tab_feature[1]) : ''; $position = isset($tab_feature[2]) ? (int) $tab_feature[2] - 1 : false; $custom = isset($tab_feature[3]) ? (int) $tab_feature[3] : false; if (!empty($feature_name) && !empty($feature_value)) { $id_feature = (int) Feature::addFeatureImport($feature_name, $position); $id_product = null; if (Tools::getValue('forceIDs') || Tools::getValue('match_ref')) { $id_product = (int) $product->id; } $id_feature_value = (int) FeatureValue::addFeatureValueImport($id_feature, $feature_value, $id_product, $id_lang, $custom); Product::addFeatureProductImport($product->id, $id_feature, $id_feature_value); } } } // clean feature positions to avoid conflict Feature::cleanPositions(); // set advanced stock managment if (isset($product->advanced_stock_management)) { if ($product->advanced_stock_management != 1 && $product->advanced_stock_management != 0) { $this->warnings[] = sprintf(Tools::displayError('Advanced stock management has incorrect value. Not set for product %1$s '), $product->name[$default_language_id]); } elseif (!Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT') && $product->advanced_stock_management == 1) { $this->warnings[] = sprintf(Tools::displayError('Advanced stock management is not enabled, cannot enable on product %1$s '), $product->name[$default_language_id]); } else { $product->setAdvancedStockManagement($product->advanced_stock_management); } // automaticly disable depends on stock, if a_s_m set to disabled if (StockAvailable::dependsOnStock($product->id) == 1 && $product->advanced_stock_management == 0) { StockAvailable::setProductDependsOnStock($product->id, 0); } } // Check if warehouse exists if (isset($product->warehouse) && $product->warehouse) { if (!Configuration::get('PS_ADVANCED_STOCK_MANAGEMENT')) { $this->warnings[] = sprintf(Tools::displayError('Advanced stock management is not enabled, warehouse not set on product %1$s '), $product->name[$default_language_id]); } else { if (Warehouse::exists($product->warehouse)) { // Get already associated warehouses $associated_warehouses_collection = WarehouseProductLocation::getCollection($product->id); // Delete any entry in warehouse for this product foreach ($associated_warehouses_collection as $awc) { $awc->delete(); } $warehouse_location_entity = new WarehouseProductLocation(); $warehouse_location_entity->id_product = $product->id; $warehouse_location_entity->id_product_attribute = 0; $warehouse_location_entity->id_warehouse = $product->warehouse; if (WarehouseProductLocation::getProductLocation($product->id, 0, $product->warehouse) !== false) { $warehouse_location_entity->update(); } else { $warehouse_location_entity->save(); } StockAvailable::synchronize($product->id); } else { $this->warnings[] = sprintf(Tools::displayError('Warehouse did not exist, cannot set on product %1$s.'), $product->name[$default_language_id]); } } } // stock available if (isset($product->depends_on_stock)) { if ($product->depends_on_stock != 0 && $product->depends_on_stock != 1) { $this->warnings[] = sprintf(Tools::displayError('Incorrect value for "depends on stock" for product %1$s '), $product->name[$default_language_id]); } elseif ((!$product->advanced_stock_management || $product->advanced_stock_management == 0) && $product->depends_on_stock == 1) { $this->warnings[] = sprintf(Tools::displayError('Advanced stock management not enabled, cannot set "depends on stock" for product %1$s '), $product->name[$default_language_id]); } else { StockAvailable::setProductDependsOnStock($product->id, $product->depends_on_stock); } // This code allows us to set qty and disable depends on stock if (isset($product->quantity) && $product->quantity) { // if depends on stock and quantity, add quantity to stock if ($product->depends_on_stock == 1) { $stock_manager = StockManagerFactory::getManager(); $price = str_replace(',', '.', $product->wholesale_price); if ($price == 0) { $price = 1.0E-6; } $price = round((double) $price, 6); $warehouse = new Warehouse($product->warehouse); if ($stock_manager->addProduct((int) $product->id, 0, $warehouse, $product->quantity, 1, $price, true)) { StockAvailable::synchronize((int) $product->id); } } else { if (Shop::isFeatureActive()) { foreach ($shops as $shop) { StockAvailable::setQuantity((int) $product->id, 0, $product->quantity, (int) $shop); } } else { StockAvailable::setQuantity((int) $product->id, 0, $product->quantity, (int) $this->context->shop->id); } } } } else { if (Shop::isFeatureActive()) { foreach ($shops as $shop) { StockAvailable::setQuantity((int) $product->id, 0, $product->quantity, (int) $shop); } } else { StockAvailable::setQuantity((int) $product->id, 0, $product->quantity, (int) $this->context->shop->id); } } } } $this->closeCsvFile($handle); }
public function supplyOrdersDetailsImport() { // opens CSV & sets locale $this->receiveTab(); $handle = $this->openCsvFile(); AdminImportController::setLocale(); $products = array(); $reset = true; // main loop, for each supply orders details to import for ($current_line = 0; $line = fgetcsv($handle, MAX_LINE_SIZE, $this->separator); ++$current_line) { // if convert requested if (Tools::getValue('convert')) { $line = $this->utf8EncodeArray($line); } $info = AdminImportController::getMaskedRow($line); // sets default values if needed AdminImportController::setDefaultValues($info); // gets the supply order if (array_key_exists('supply_order_reference', $info) && pSQL($info['supply_order_reference']) && SupplyOrder::exists(pSQL($info['supply_order_reference']))) { $supply_order = SupplyOrder::getSupplyOrderByReference(pSQL($info['supply_order_reference'])); } else { $this->errors[] = sprintf($this->l('Supply Order (%s) could not be loaded (at line %d).'), (int) $info['supply_order_reference'], $current_line + 1); } if (empty($this->errors)) { // sets parameters $id_product = (int) $info['id_product']; if (!$info['id_product_attribute']) { $info['id_product_attribute'] = 0; } $id_product_attribute = (int) $info['id_product_attribute']; $unit_price_te = (double) $info['unit_price_te']; $quantity_expected = (int) $info['quantity_expected']; $discount_rate = (double) $info['discount_rate']; $tax_rate = (double) $info['tax_rate']; // checks if one product is there only once if (isset($product['id_product'])) { if ($product['id_product'] == $id_product_attribute) { $this->errors[] = sprintf($this->l('Product (%d/%D) cannot be added twice (at line %d).'), $id_product, $id_product_attribute, $current_line + 1); } else { $product['id_product'] = $id_product_attribute; } } else { $product['id_product'] = 0; } // checks parameters if (false === ($supplier_reference = ProductSupplier::getProductSupplierReference($id_product, $id_product_attribute, $supply_order->id_supplier))) { $this->errors[] = sprintf($this->l('Product (%d/%d) is not available for this order (at line %d).'), $id_product, $id_product_attribute, $current_line + 1); } if ($unit_price_te < 0) { $this->errors[] = sprintf($this->l('Unit Price (tax excl.) (%d) is not valid (at line %d).'), $unit_price_te, $current_line + 1); } if ($quantity_expected < 0) { $this->errors[] = sprintf($this->l('Quantity Expected (%d) is not valid (at line %d).'), $quantity_expected, $current_line + 1); } if ($discount_rate < 0 || $discount_rate > 100) { $this->errors[] = sprintf($this->l('Discount rate (%d) is not valid (at line %d). %s.'), $discount_rate, $current_line + 1, $this->l('Format: Between 0 and 100')); } if ($tax_rate < 0 || $tax_rate > 100) { $this->errors[] = sprintf($this->l('Quantity Expected (%d) is not valid (at line %d).'), $tax_rate, $current_line + 1, $this->l('Format: Between 0 and 100')); } // if no errors, sets supply order details if (empty($this->errors)) { // resets order if needed if ($reset) { $supply_order->resetProducts(); $reset = false; } // creates new product $supply_order_detail = new SupplyOrderDetail(); AdminImportController::arrayWalk($info, array('AdminImportController', 'fillInfo'), $supply_order_detail); // sets parameters $supply_order_detail->id_supply_order = $supply_order->id; $currency = new Currency($supply_order->id_ref_currency); $supply_order_detail->id_currency = $currency->id; $supply_order_detail->exchange_rate = $currency->conversion_rate; $supply_order_detail->supplier_reference = $supplier_reference; $supply_order_detail->name = Product::getProductName($id_product, $id_product_attribute, $supply_order->id_lang); // gets ean13 / ref / upc $query = new DbQuery(); $query->select(' IFNULL(pa.reference, IFNULL(p.reference, \'\')) as reference, IFNULL(pa.ean13, IFNULL(p.ean13, \'\')) as ean13, IFNULL(pa.upc, IFNULL(p.upc, \'\')) as upc '); $query->from('product', 'p'); $query->leftJoin('product_attribute', 'pa', 'pa.id_product = p.id_product AND id_product_attribute = ' . (int) $id_product_attribute); $query->where('p.id_product = ' . (int) $id_product); $query->where('p.is_virtual = 0 AND p.cache_is_pack = 0'); $res = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query); $product_infos = $res['0']; $supply_order_detail->reference = $product_infos['reference']; $supply_order_detail->ean13 = $product_infos['ean13']; $supply_order_detail->upc = $product_infos['upc']; $supply_order_detail->add(); $supply_order->update(); unset($supply_order_detail); } } } // closes $this->closeCsvFile($handle); }
private function getItemXML($product, $lang, $id_curr, $id_shop, $combination = false) { $xml_googleshopping = ''; $id_lang = (int) $lang['id_lang']; $title_limit = 70; $description_limit = 4990; $languages = Language::getLanguages(); $tailleTabLang = sizeof($languages); $this->context->language->id = $id_lang; $this->context->shop->id = $id_shop; $p = new Product($product['id_product'], true, $id_lang, $id_shop, $this->context); // Get module configuration for this shop if (!$combination) { $product['quantity'] = StockAvailable::getQuantityAvailableByProduct($product['id_product'], 0, $id_shop); } // Exclude non-available products if ($this->module_conf['export_nap'] === 0 && $product['quantity'] < 1) { $this->nb_not_exported_products++; return; } // Check minimum product price $price = Product::getPriceStatic((int) $product['id_product'], true); if ((double) $this->module_conf['export_min_price'] > 0 && (double) $this->module_conf['export_min_price'] > (double) $price) { return; } $cat_link_rew = Category::getLinkRewrite($product['id_gcategory'], (int) $lang); $product_link = $this->context->link->getProductLink((int) $product['id_product'], $product['link_rewrite'], $cat_link_rew, $product['ean13'], (int) $product['id_lang'], $id_shop, $combination, true); // Product name $title_crop = $product['name']; // Product color attribute, if any if (!empty($product['color'])) { $title_crop .= ' ' . $product['color']; } if (!empty($product['material'])) { $title_crop .= ' ' . $product['material']; } if (!empty($product['pattern'])) { $title_crop .= ' ' . $product['pattern']; } if (!empty($product['size'])) { $title_crop .= ' ' . $product['size']; } if (Tools::strlen($product['name']) > $title_limit) { $title_crop = Tools::substr($title_crop, 0, $title_limit - 1); $title_crop = Tools::substr($title_crop, 0, strrpos($title_crop, " ")); } // Description type if ($this->module_conf['description'] == 'long') { $description_crop = $product['description']; } else { if ($this->module_conf['description'] == 'short') { $description_crop = $product['description_short']; } else { if ($this->module_conf['description'] == 'meta') { $description_crop = $product['meta_description']; } } } $description_crop = $this->rip_tags($description_crop); if (Tools::strlen($description_crop) > $description_limit) { $description_crop = Tools::substr($description_crop, 0, $description_limit - 1); $description_crop = Tools::substr($description_crop, 0, strrpos($description_crop, " ")) . ' ...'; } $xml_googleshopping .= '<item>' . "\n"; $xml_googleshopping .= '<g:id>' . $product['gid'] . '</g:id>' . "\n"; $xml_googleshopping .= '<title><![CDATA[' . $title_crop . ']]></title>' . "\n"; $xml_googleshopping .= '<description><![CDATA[' . $description_crop . ']]></description>' . "\n"; $xml_googleshopping .= '<link><![CDATA[' . htmlspecialchars($product_link, self::REPLACE_FLAGS, self::CHARSET, false) . ']]></link>' . "\n"; // Image links $images = Image::getImages($lang['id_lang'], $product['id_product'], $combination); $indexTabLang = 0; if ($tailleTabLang > 1) { while (sizeof($images) < 1 && $indexTabLang < $tailleTabLang) { if ($languages[$indexTabLang]['id_lang'] != $lang['id_lang']) { $images = Image::getImages($languages[$indexTabLang]['id_lang'], $product['id_product']); } $indexTabLang++; } } $nbimages = 0; $image_type = $this->module_conf['img_type']; if ($image_type == '') { $image_type = 'large_default'; } foreach ($images as $im) { $image = $this->context->link->getImageLink($product['link_rewrite'], $product['id_product'] . '-' . $im['id_image'], $image_type); $image = preg_replace('*http://' . Tools::getHttpHost() . '/*', $this->uri, $image); if ($im['cover'] == 1) { $xml_googleshopping .= '<g:image_link><![CDATA[' . $image . ']]></g:image_link>' . "\n"; } else { $xml_googleshopping .= '<g:additional_image_link><![CDATA[' . $image . ']]></g:additional_image_link>' . "\n"; } // max images by product if (++$nbimages == 10) { break; } } // Product condition, or category's condition attribute, or its parent one... // Product condition = new, used, refurbished if (empty($product['condition'])) { $product['condition'] = $this->categories_values[$product['id_gcategory']]['gcat_condition']; } if (!empty($product['condition'])) { $xml_googleshopping .= '<g:condition><![CDATA[' . $product['condition'] . ']]></g:condition>' . "\n"; } // Shop category $breadcrumb = GCategories::getPath($product['id_gcategory'], '', $id_lang, $id_shop, $this->id_root); $product_type = ''; if (!empty($this->module_conf['product_type[]'][$id_lang])) { $product_type = $this->module_conf['product_type[]'][$id_lang]; if (!empty($breadcrumb)) { $product_type .= " > "; } } $product_type .= $breadcrumb; $xml_googleshopping .= '<g:product_type><![CDATA[' . $product_type . ']]></g:product_type>' . "\n"; // Matching Google category, or parent categories' one $product['gcategory'] = $this->categories_values[$product['category_default']]['gcategory']; $xml_googleshopping .= '<g:google_product_category><![CDATA[' . $product['gcategory'] . ']]></g:google_product_category>' . "\n"; // Product quantity & availability if (empty($this->categories_values[$product['category_default']]['gcat_avail'])) { if ($this->module_conf['quantity'] == 1) { $xml_googleshopping .= '<g:quantity>' . $product['quantity'] . '</g:quantity>' . "\n"; } if ($product['quantity'] > 0 && $product['available_for_order']) { $xml_googleshopping .= '<g:availability>in stock</g:availability>' . "\n"; } elseif ($p->isAvailableWhenOutOfStock((int) $p->out_of_stock) && $product['available_for_order']) { $xml_googleshopping .= '<g:availability>preorder</g:availability>' . "\n"; } else { $xml_googleshopping .= '<g:availability>out of stock</g:availability>' . "\n"; } } else { if ($this->module_conf['quantity'] == 1 && $product['quantity'] > 0) { $xml_googleshopping .= '<g:quantity>' . $product['quantity'] . '</g:quantity>' . "\n"; } $xml_googleshopping .= '<g:availability>' . $this->categories_values[$product['category_default']]['gcat_avail'] . '</g:availability>' . "\n"; } // Price(s) $currency = new Currency((int) $id_curr); $use_tax = $product['tax_included'] ? true : false; $no_tax = !$use_tax ? true : false; $product['price'] = (double) $p->getPriceStatic($product['id_product'], $use_tax, $combination) * $currency->conversion_rate; $product['price_without_reduct'] = (double) $p->getPriceWithoutReduct($no_tax, $combination) * $currency->conversion_rate; $product['price'] = number_format(round($product['price'], 2, PHP_ROUND_HALF_DOWN), 2, '.', ' '); $product['price_without_reduct'] = number_format(round($product['price_without_reduct'], 2, PHP_ROUND_HALF_DOWN), 2, '.', ' '); if ((double) $product['price'] < (double) $product['price_without_reduct']) { $xml_googleshopping .= '<g:price>' . $product['price_without_reduct'] . ' ' . $currency->iso_code . '</g:price>' . "\n"; $xml_googleshopping .= '<g:sale_price>' . $product['price'] . ' ' . $currency->iso_code . '</g:sale_price>' . "\n"; } else { $xml_googleshopping .= '<g:price>' . $product['price'] . ' ' . $currency->iso_code . '</g:price>' . "\n"; } $identifier_exists = 0; // GTIN (EAN, UPC, JAN, ISBN) if (!empty($product['ean13'])) { $xml_googleshopping .= '<g:gtin>' . $product['ean13'] . '</g:gtin>' . "\n"; $identifier_exists++; } // Brand if ($this->module_conf['no_brand'] != 0 && !empty($product['id_manufacturer'])) { $xml_googleshopping .= '<g:brand><![CDATA[' . htmlspecialchars(Manufacturer::getNameById((int) $product['id_manufacturer']), self::REPLACE_FLAGS, self::CHARSET, false) . ']]></g:brand>' . "\n"; $identifier_exists++; } // MPN if (empty($product['supplier_reference'])) { $product['supplier_reference'] = ProductSupplier::getProductSupplierReference($product['id_product'], 0, $product['id_supplier']); } if ($this->module_conf['mpn_type'] == 'reference' && !empty($product['reference'])) { $xml_googleshopping .= '<g:mpn><![CDATA[' . $product['reference'] . ']]></g:mpn>' . "\n"; $identifier_exists++; } else { if ($this->module_conf['mpn_type'] == 'supplier_reference' && !empty($product['supplier_reference'])) { $xml_googleshopping .= '<g:mpn><![CDATA[' . $product['supplier_reference'] . ']]></g:mpn>' . "\n"; $identifier_exists++; } } // Tag "identifier_exists" if ($this->module_conf['id_exists_tag'] && $identifier_exists < 2) { $xml_googleshopping .= '<g:identifier_exists>FALSE</g:identifier_exists>' . "\n"; } // Product gender and age_group attributes association $product_features = $this->getProductFeatures($product['id_product'], $id_lang, $id_shop); $product['gender'] = $this->categories_values[$product['category_default']]['gcat_gender']; $product['age_group'] = $this->categories_values[$product['category_default']]['gcat_age_group']; foreach ($product_features as $feature) { switch ($feature['id_feature']) { case $this->module_conf['gender']: $product['gender'] = $feature['value']; continue 2; case $this->module_conf['age_group']: $product['age_group'] = $feature['value']; continue 2; } if (!$product['color']) { foreach ($this->module_conf['color[]'] as $id => $v) { if ($v == $feature['id_feature']) { $product['color'] = $feature['value']; } } } if (!$product['material']) { foreach ($this->module_conf['material[]'] as $id => $v) { if ($v == $feature['id_feature']) { $product['material'] = $feature['value']; } } } if (!$product['pattern']) { foreach ($this->module_conf['pattern[]'] as $id => $v) { if ($v == $feature['id_feature']) { $product['pattern'] = $feature['value']; } } } if (!$product['size']) { foreach ($this->module_conf['size[]'] as $id => $v) { if ($v == $feature['id_feature']) { $product['size'] = $feature['value']; } } } } // Product gender attribute, or category gender attribute, or parent's one if (!empty($product['gender'])) { $xml_googleshopping .= '<g:gender><![CDATA[' . $product['gender'] . ']]></g:gender>' . "\n"; } // Product age_group attribute, or category age_group attribute, or parent's one if (!empty($product['age_group'])) { $xml_googleshopping .= '<g:age_group><![CDATA[' . $product['age_group'] . ']]></g:age_group>' . "\n"; } // Product attributes combination groups if ($combination && !empty($product['item_group_id'])) { $xml_googleshopping .= '<g:item_group_id>' . $product['item_group_id'] . '</g:item_group_id>' . "\n"; } // Product color attribute, or category color attribute, or parent's one if (!empty($product['color'])) { $xml_googleshopping .= '<g:color><![CDATA[' . $product['color'] . ']]></g:color>' . "\n"; } // Product material attribute, or category material attribute, or parent's one if (!empty($product['material'])) { $xml_googleshopping .= '<g:material><![CDATA[' . $product['material'] . ']]></g:material>' . "\n"; } // Product pattern attribute, or category pattern attribute, or parent's one if (!empty($product['pattern'])) { $xml_googleshopping .= '<g:pattern><![CDATA[' . $product['pattern'] . ']]></g:pattern>' . "\n"; } // Product size attribute, or category size attribute, or parent's one if (!empty($product['size'])) { $xml_googleshopping .= '<g:size><![CDATA[' . $product['size'] . ']]></g:size>' . "\n"; } // Featured products if ($this->module_conf['featured_products'] == 1 && $product['on_sale'] != '0') { $xml_googleshopping .= '<g:featured_product>true</g:featured_product>' . "\n"; } // Shipping $xml_googleshopping .= '<g:shipping>' . "\n"; $xml_googleshopping .= "\t" . '<g:country>' . $this->module_conf['shipping_country'] . '</g:country>' . "\n"; $xml_googleshopping .= "\t" . '<g:service>Standard</g:service>' . "\n"; $xml_googleshopping .= "\t" . '<g:price>' . number_format($this->module_conf['shipping_price'], 2, '.', ' ') . ' ' . $currency->iso_code . '</g:price>' . "\n"; $xml_googleshopping .= '</g:shipping>' . "\n"; // Shipping weight if ($product['weight'] != '0') { $xml_googleshopping .= '<g:shipping_weight>' . number_format($product['weight'], 2, '.', '') . ' ' . Configuration::get('PS_WEIGHT_UNIT') . '</g:shipping_weight>' . "\n"; } $xml_googleshopping .= '</item>' . "\n\n"; if ($combination) { $this->nb_combinations++; $this->nb_prd_w_attr[$product['id_product']] = 1; } $this->nb_total_products++; return $xml_googleshopping; }
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; }