238 lines
9.4 KiB
PHP
238 lines
9.4 KiB
PHP
|
<?php
|
||
|
require_once(dirname(__FILE__).'/../../config/config.inc.php');
|
||
|
@ini_set('display_errors', 'on');
|
||
|
@ini_set('max_execution_time', 0);
|
||
|
|
||
|
if (!defined('_PS_BASE_URL_'))
|
||
|
define('_PS_BASE_URL_', Tools::getShopDomain(true));
|
||
|
|
||
|
$path = _PS_ROOT_DIR_.'/modules/importproduit/';
|
||
|
$fichier = "productimport.csv";
|
||
|
|
||
|
$handle = fopen($path.$fichier, "r");
|
||
|
if($handle){
|
||
|
$row = 0;
|
||
|
while (($data01 = fgetcsv($handle, 100000, ";")) !== FALSE) {
|
||
|
$num = count($data01);
|
||
|
$data = utf8EncodeArray($data01);
|
||
|
if($row > 27327){
|
||
|
$reference = $data[0]; // A
|
||
|
$name = str_replace("=","",str_replace("°","",str_replace("#","",$data[1]))); // B
|
||
|
$shortdesc = $data[847]; // AFP
|
||
|
$desc = $data[460]; // QS
|
||
|
$pricewt = $data[12]; // M
|
||
|
$wholesale_price = $data[10]; // K
|
||
|
$meta_description = str_replace("=","",str_replace("°","",str_replace("#","",$data[160]))); // FE
|
||
|
$meta_keywords = str_replace("=","",str_replace("°","",str_replace("#","",$data[162]))); // FG
|
||
|
$active = $data[136]; // EG
|
||
|
$carac_fabricant = $data[177]; // FV
|
||
|
$carac_reference = $data[178]; // FW
|
||
|
$out_of_stock = $data[138]; // EI
|
||
|
$carac_echelle = $data[183]; // GB
|
||
|
$namecategory = $data[313]; // LB
|
||
|
$quantity = (int)$data[275]; // JP
|
||
|
|
||
|
if($active == "Oui"){
|
||
|
$active = true;
|
||
|
}else{
|
||
|
$active = false;
|
||
|
}
|
||
|
|
||
|
if($out_of_stock == "Oui"){
|
||
|
$out_of_stock = 1;
|
||
|
}else if($out_of_stock == "Non"){
|
||
|
$out_of_stock = 0;
|
||
|
}else{
|
||
|
$out_of_stock = 2;
|
||
|
}
|
||
|
|
||
|
if($name && $reference != "CODE"){
|
||
|
$id_product = existsRefInDatabase($reference);
|
||
|
|
||
|
if($id_product){
|
||
|
$product = new Product($id_product);
|
||
|
$update = true;
|
||
|
}else{
|
||
|
$product = new Product();
|
||
|
$update = false;
|
||
|
}
|
||
|
|
||
|
|
||
|
$language = Language::getLanguages(false);
|
||
|
$numlang = count($language) + 1;
|
||
|
$tabname = array();
|
||
|
for($i = 1 ; $i < $numlang; $i++){
|
||
|
$tabname[$i] = $name;
|
||
|
}
|
||
|
$tabrewrite = array();
|
||
|
for($i = 1 ; $i < $numlang; $i++){
|
||
|
$tabrewrite[$i] = Tools::str2url($name);
|
||
|
}
|
||
|
$tabshortdesc = array();
|
||
|
for($i = 1 ; $i < $numlang; $i++){
|
||
|
$tabshortdesc[$i] = $shortdesc;
|
||
|
}
|
||
|
$tabdesc = array();
|
||
|
for($i = 1; $i < $numlang; $i++){
|
||
|
$tabdesc[$i] = $desc;
|
||
|
}
|
||
|
$tabmeta_description = array();
|
||
|
for($i = 1 ; $i < $numlang; $i++){
|
||
|
$tabmeta_description[$i] = $meta_description;
|
||
|
}
|
||
|
$tabmeta_keywords = array();
|
||
|
for($i = 1; $i < $numlang; $i++){
|
||
|
$tabmeta_keywords[$i] = $meta_keywords;
|
||
|
}
|
||
|
$tabavailable_now = array();
|
||
|
for($i = 1; $i < $numlang; $i++){
|
||
|
$tabavailable_now[$i] = "En stock";
|
||
|
}
|
||
|
|
||
|
$product->name = $tabname;
|
||
|
$product->link_rewrite = $tabrewrite;
|
||
|
$product->meta_title = $tabmeta_description;
|
||
|
$product->meta_description = $tabmeta_description;
|
||
|
$product->meta_keywords = $tabmeta_keywords;
|
||
|
$product->reference = $reference;
|
||
|
$product->supplier_reference = $reference;
|
||
|
$product->available_now = $tabavailable_now;
|
||
|
$product->price = (float)$pricewt;
|
||
|
$product->wholesale_price = (float)$wholesale_price;
|
||
|
$product->quantity = (int)$quantity;
|
||
|
$product->active = $active;
|
||
|
$product->description = $tabdesc;
|
||
|
$product->description_short = $tabshortdesc;
|
||
|
|
||
|
if($category = Category::searchByName(1,$namecategory)){
|
||
|
$product->id_category_default = $category[0]['id_category'];
|
||
|
$product->updateCategories(array($category[0]['id_category']));
|
||
|
}else{
|
||
|
$category = new Category();
|
||
|
$tabnamecategory = array();
|
||
|
for($i = 1; $i < $numlang; $i++){
|
||
|
$tabnamecategory[$i] = $namecategory;
|
||
|
}
|
||
|
|
||
|
$tabrewritecategory = array();
|
||
|
for($i = 1 ; $i < $numlang; $i++){
|
||
|
$tabrewritecategory[$i] = substr(Tools::str2url($name), 0, 60);
|
||
|
}
|
||
|
$category->name = $tabnamecategory;
|
||
|
$category->link_rewrite = $tabrewritecategory;
|
||
|
$category->id_parent = 209;
|
||
|
$category->add();
|
||
|
$product->id_category_default = $category->id;
|
||
|
$product->updateCategories(array($category->id));
|
||
|
}
|
||
|
|
||
|
if(!$update){
|
||
|
if(!$product->add()){
|
||
|
echo $row;
|
||
|
}
|
||
|
}else{
|
||
|
$product->update();
|
||
|
}
|
||
|
|
||
|
StockAvailable::setProductOutOfStock($product->id, (int)$out_of_stock);
|
||
|
|
||
|
if($carac_fabricant){
|
||
|
$idfeature = FeatureValue::addFeatureValueImport(6, $carac_fabricant);
|
||
|
$feature_value = new FeatureValue($idfeature);
|
||
|
$feature_value->custom = 0;
|
||
|
$feature_value->update();
|
||
|
Product::addFeatureProductImport($product->id, 6, $idfeature);
|
||
|
}
|
||
|
if($carac_reference){
|
||
|
$idfeature = FeatureValue::addFeatureValueImport(7, $carac_reference);
|
||
|
$feature_value = new FeatureValue($idfeature);
|
||
|
Product::addFeatureProductImport($product->id, 7, $idfeature);
|
||
|
}
|
||
|
if($carac_echelle){
|
||
|
$idfeature = FeatureValue::addFeatureValueImport(1, $carac_echelle);
|
||
|
$feature_value = new FeatureValue($idfeature);
|
||
|
$feature_value->custom = 0;
|
||
|
$feature_value->update();
|
||
|
Product::addFeatureProductImport($product->id, 1, $idfeature);
|
||
|
}
|
||
|
|
||
|
setQuantity($product->id, 0, (int)$quantity);
|
||
|
echo $product->id."<br/>";
|
||
|
}
|
||
|
}
|
||
|
$row++;
|
||
|
|
||
|
echo $row."<br/>";
|
||
|
ob_end_flush();
|
||
|
flush();
|
||
|
ob_start();
|
||
|
}
|
||
|
fclose($handle);
|
||
|
echo "Import terminé";
|
||
|
}else{
|
||
|
echo "prob ouverture";
|
||
|
}
|
||
|
|
||
|
function existsRefInDatabase($reference){
|
||
|
$row = Db::getInstance()->getRow('
|
||
|
SELECT `id_product`
|
||
|
FROM `'._DB_PREFIX_.'product` p
|
||
|
WHERE p.reference = "'.$reference.'"');
|
||
|
|
||
|
return $row['id_product'];
|
||
|
}
|
||
|
|
||
|
function utf8EncodeArray($array){
|
||
|
return (is_array($array) ? array_map('utf8_encode', $array) : utf8_encode($array));
|
||
|
}
|
||
|
function setQuantity($id_product, $id_product_attribute, $quantity, $id_shop = null){
|
||
|
if (!Validate::isUnsignedId($id_product))
|
||
|
return false;
|
||
|
|
||
|
$context = Context::getContext();
|
||
|
|
||
|
// if there is no $id_shop, gets the context one
|
||
|
if ($id_shop === null && Shop::getContext() != Shop::CONTEXT_GROUP)
|
||
|
$id_shop = (int)$context->shop->id;
|
||
|
|
||
|
$depends_on_stock = StockAvailable::dependsOnStock($id_product);
|
||
|
|
||
|
//Try to set available quantity if product does not depend on physical stock
|
||
|
if (!$depends_on_stock)
|
||
|
{
|
||
|
$id_stock_available = (int)StockAvailable::getStockAvailableIdByProductId($id_product, $id_product_attribute, $id_shop);
|
||
|
if ($id_stock_available)
|
||
|
{
|
||
|
$stock_available = new StockAvailable($id_stock_available);
|
||
|
$stock_available->quantity = (int)$quantity;
|
||
|
$stock_available->update();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$out_of_stock = StockAvailable::outOfStock($id_product, $id_shop);
|
||
|
$stock_available = new StockAvailable();
|
||
|
$stock_available->out_of_stock = (int)$out_of_stock;
|
||
|
$stock_available->id_product = (int)$id_product;
|
||
|
$stock_available->id_product_attribute = (int)$id_product_attribute;
|
||
|
$stock_available->quantity = (int)$quantity;
|
||
|
|
||
|
if ($id_shop === null)
|
||
|
$shop_group = Shop::getContextShopGroup();
|
||
|
else
|
||
|
$shop_group = new ShopGroup((int)Shop::getGroupFromShop((int)$id_shop));
|
||
|
|
||
|
// if quantities are shared between shops of the group
|
||
|
if ($shop_group->share_stock)
|
||
|
{
|
||
|
$stock_available->id_shop = 0;
|
||
|
$stock_available->id_shop_group = (int)$shop_group->id;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$stock_available->id_shop = (int)$id_shop;
|
||
|
$stock_available->id_shop_group = 0;
|
||
|
}
|
||
|
$stock_available->add();
|
||
|
}
|
||
|
}
|
||
|
}
|