File: //home/temp/yarusvl.ru/exchanger/gds_product.php
<?php
/**
* Created by el.
* @autor: el
* @pakage: el
* @version: 0.1 10.09.18 14:26
* Date: 10.09.18
*/
$time_start = microtime(true);
require_once('config.php');
$mgc = new Mgc();
error_reporting(E_ALL);
ini_set("display_errors", 1);
//define("SUPPLIER_ID",19);
$products = array();
$variants = array();
//echo SOURCE_DIR . "termos_products*.xml";
foreach(glob(SOURCE_DIR . "termos_products*.xml") as $file){
echo $file . PHP_EOL;
$xml = validationXML($file);
foreach ($xml->xpath('//offer') as $item) {
$import = new stdClass;
$import->category = "Неразобранное"; // по умолчанию
$import->supplier_cat_ids = array(); // категории поставщика
$import->category_ids = array(); // если уже разобранные в системе
$sup = (string)$item->{"destination-url-do-not-send-traffic"};
$sup = parse_url($sup,PHP_URL_HOST);
$sup = str_replace("www.","",$sup);
$sup = explode(".",$sup);
$sup = ucfirst($sup[0]);
if(!isset($SUPPLIERS[$sup])){
$supl = $mgc->suppliers->get_supplier($sup);
if($supl){
$supplier_id = $supl->id ;
}else{
/* var_dump($sup);
die();*/
$supplier = new stdClass;
$supplier->name=$sup;
$supplier_id = $mgc->suppliers->add_supplier($supplier) ;
}
$SUPPLIERS[$sup] = $supplier_id;
}
if(!isset( $importSupplier[$sup]))
$importSupplier[$sup] = new importSupplier($SUPPLIERS[$sup]);
$import->vendor_ID = (string)$item['article'];
$import->sku = (string)$item['article']."_".strtolower($sup);
$model = (string)$item->model;
if(!empty($model) && strlen($model)<=15 && $model!=(string)$item->name)
$import->name = (string)$item->name." - ".$model;
else $import->name = (string)$item->name;
$import->variant =(string)$item->name;
$import->variant_vendor_ID = (string)$item['article'];
if(!empty($model))
$import->annotation = "Модель: ".(string)$model;
else $import->annotation = (string)$item->name;
$import->description = (string)$item->description;
$descr = $import->description;
$import->brand = (string)$item->vendor;
$descr = explode(".",$descr);
foreach($descr as $k=>$v){
$descr[$k] = "<p>".str_ireplace(array($import->brand,$import->name),array("<b>".$import->brand."</b>","<b>".$import->name."</b>"),$v)."</p>";
}
$import->description = implode("",$descr);
$volume = '';
$lon = mb_strtolower($import->name);
if(preg_match("| ([0-9.,]+)L |i",$import->name ,$ch)){
$import->name = preg_replace("|([0-9.,]+)L|i",$ch[1]." л",$import->name);
$volume = $ch[1];
}elseif(preg_match("| ([0-9.,]+)ml |i",$import->name ,$ch)){
$import->name = preg_replace("|([0-9.,]+)ml|i",($ch[1]/1000)." л",$import->name);
$volume = $ch[1]/1000;
}elseif(preg_match("| ([0-9.,]+) л |i",$import->name ,$ch)){
$volume = $ch[1];
}elseif(preg_match("| ([0-9.,]+) мл |i",$import->name ,$ch)){
$volume = $ch[1]/1000;
}elseif(preg_match("| ([0-9.,]+)L\.|i",$lon ,$ch)){
$import->name = preg_replace("|([0-9.,]+)l\.|i",$ch[1]." л",$import->name);
$volume = $ch[1];
}elseif(preg_match("| ([0-9.,]+)ml |i",$import->name ,$ch)){
$import->name = preg_replace("|([0-9.,]+)ml\.|i",($ch[1]/1000)." л",$import->name);
$volume = $ch[1]/1000;
}elseif(preg_match("| ([0-9.,]+) л\. |i",$import->name ,$ch)){
$volume = $ch[1];
}elseif(preg_match("| ([0-9.,]+) мл\. |i",$import->name ,$ch)){
$volume = $ch[1]/1000;
}
elseif(preg_match("| ([0-9.,]+) L\.|i",$lon ,$ch)){
$import->name = preg_replace("|([0-9.,]+) l\.|i",$ch[1]." л",$import->name);
$volume = $ch[1];
}elseif(preg_match("| ([0-9.,]+) ml |i",$import->name ,$ch)){
$import->name = preg_replace("|([0-9.,]+) ml\.|i",($ch[1]/1000)." л",$import->name);
$volume = $ch[1]/1000;
}elseif(preg_match("| ([0-9.,]+)л\. |i",$import->name ,$ch)){
$volume = $ch[1];
}elseif(preg_match("| ([0-9.,]+)мл\. |i",$import->name ,$ch)){
$volume = $ch[1]/1000;
}
if(strlen($volume)){
$import->volume = str_replace(",",".",$volume);
if ("1.0" == $import->volume)$import->volume = 1;
$import->attributes['volume'] = $import->volume ;
echo "Лбъем: ".$import->volume.PHP_EOL;
}
$import->description = str_replace(array("<p> </p>","<br>","<br />"),"",$import->description);
if(sizeof($descr)>1) $import->meta_description = strip_tags($descr[0]);
else $import->meta_description = $import->annotation;
if(strlen($import->meta_description)<50)$import->meta_description .= ". ". (string)$item->name;
$import->link = (string)$item->url."?sub_id=termos"."&sub_id2=".$import->sku;
$item->price = preg_replace("|([\D,.]+)|","",$item->price);
$import->price = (string)str_replace(",", ".", $item->price);
$import->price = explode(".",$import->price);
$import->price = $import->price[0];
// $import->oldprice = (string)str_replace(",", ".", $item->oldprice);
/* if((int) $import->price >10000){*/
// var_dump( $import->price );
// var_dump( $item->price );
//$item->visible = 0;
/* continue;
}*/
// $import->weight = floatval(str_replace(",",".",$item->tov_vesed));
$import->material = implode(",", (array)$item->materials);
//$import->size = $item->tov_razm;
//$import->color = $item->tov_color;
$import->id = null;
$import->images = [];
$import->weight = 0;
$import->size = '';
$import->color = '';
$code = "404";
if((string)$item->original_picture){
$code = check_http_status((string)$item->original_picture);
$import->images[] = (string)$item->original_picture;
}
/* var_dump($code);
exit;*/
if((int)$code!==200){
$import->images = array();
var_dump($code);
if(!empty((string)$item->picture))
$import->images[] = "http:".str_replace("http:","",(string)$item->picture);
else $import->images[] = "http:".str_replace("http:","",(string)$item->thumbnail);
}
$data = $importSupplier[$sup]->import_item($import);
echo "import OK ".$import->name."[".$import->sku."]".PHP_EOL;
/*var_dump($supplier_id);
exit;*/
}
/*exit;*/
}
foreach ($importSupplier as $sup) $sup->setInactive();
//var_dump($categories);
// Отладочная информация
print "<!--\r\n";
$time_end = microtime(true);
$exec_time = $time_end-$time_start;
if(function_exists('memory_get_peak_usage')) {
print "memory peak usage: ".memory_get_peak_usage()." bytes\r\n";
}
print "page generation time: ".$exec_time." seconds\r\n";
print "-->";