File: //home/bk/__backup/exchanger/gifts_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();
$importSupplier = new importSupplier(SUPPLIER_ID);
/**
 *
 * CATEGORIES
 *
 */
$cats = validationXML(SOURCE_DIR ."gifts_tree.xml");
$level = 0;
$categories = array();
$CATS = array();
foreach ($cats->page->page as $k => $v) {
    echo $v->name . PHP_EOL;
    $cdata = array(
        'supplier_id' => SUPPLIER_ID,
        'title' => (string)$v->name,
        "parent_id" => 0,
        "sup_id" => (string)$v->page_id
        // id 	supplier_id 	title 	sup_id 	parent_id 	cat_id
    );
    $cat_id = $importSupplier->import_inner_category($cdata);
    $CATS[(int)$v->page_id] = (int)$cat_id;
    if ($v->page) {
        foreach ($v->page as $v1) {
            echo " -- " . $v1->name . PHP_EOL;
            $cdata = array(
                'supplier_id' => SUPPLIER_ID,
                'title' => (string)$v1->name,
                "parent_id" => (string)$v1['parent_page_id'],
                "sup_id" => (string)$v1->page_id
                // id 	supplier_id 	title 	sup_id 	parent_id 	cat_id
            );
            $cat_id = $importSupplier->import_inner_category($cdata);
            $CATS[(int)$v1->page_id] = (int)$cat_id;
        }
    }
}
unset($cats);
$PRODS_IN_CAT = array();
$pcats = validationXML(SOURCE_DIR ."gifts_products_tree.xml");
foreach ($pcats->page->page->page as $k => $v1) {
    if( isset($v1->product) ){
        foreach ($v1->product as $v) {
            if(!isset( $PRODS_IN_CAT[(string)$v->product]))  $PRODS_IN_CAT[(string)$v->product] =  array();
            $PRODS_IN_CAT[(string)$v->product][] =   (string)$v->page;
        }
    }
}
    /**
     * FILTERS
     */
    $xml_filters =  validationXML(SOURCE_DIR ."gifts_filters.xml");
    $filters = array();
    foreach($xml_filters->filtertypes->filtertype as $f){
    //echo "*";
    //var_dump($f->filtertypeid);
    if((string)$f->filtertypeid == '21'){
    //var_dump($f->filters->filter);
    foreach($f->filters->filter as $color){
    $filters[(string)$color->filterid] = (string)$color->filtername;
    }
    }
    }
$imagePath = "http://6901_xmlexport:220778@api2.gifts.ru/export/v2/catalogue/";
    $xml =  validationXML(SOURCE_DIR ."gifts_products.xml");
    if($xml === false) die("Error parse file");
    foreach($xml->product as $item) {
        try{
            $import = new stdClass;
              $import->category = "Неразобранное"; // по умолчанию
              $import->supplier_cat_ids = array(); // категории поставщика
                   $import->category_ids = array(); // если уже разобранные в системе
      			// -- Собираем изображения
      			$import->images   = [];
      			if($item->super_big_image['src']) {
      				$import->images[] = $imagePath.$item->super_big_image['src'];
      			}
      			foreach($item->product_attachment as $image) {
      				$import->images[] = $imagePath.$image->image;
      			}
      			// -- -- -- --
      			$import->description          =  strip_tags((string)$item->content,"<p>");
      			$import->brand          =  (string)$item->brand;
      			$import->material       =  (string)$item->matherial;
      $import->color    = null;
      			if($item->filters){
      			foreach($item->filters->filter as $color){
      			if((string)$color->filtertypeid == '21')$import->color = $filters[(string)$color->filterid] ;
      }
      }
            $import->vendor_ID = (string)$item->product_id;
            $import->name     =  (string)$item->name;
            // $PRODS_IN_CAT
 		/*	$import->category_ids;*/
            if(isset($PRODS_IN_CAT[ (string)$item->product_id])){
                $import->supplier_cat_ids = $PRODS_IN_CAT[ (string)$item->product_id];
       foreach ($PRODS_IN_CAT[ (string)$item->product_id] as $ca){
           if(isset($CATS[(int)$ca]) && intval($CATS[(int)$ca])){
               $import->category_ids[] =  intval($CATS[(int)$ca]);
             }
       }
       if(sizeof(  $import->category_ids)){
           $import->category_id = reset(  $import->category_ids);
       }
        }
      			if($item->product) {
      				//$import->idRepeatImages = $item->product_id;
      				foreach($item->product as $subItem) {
      				//if("4750.10"!=(string)$subItem->code) continue;
      				//	$import->name     =  (string)$subItem->name;
                        $import->variant = (string)$subItem->name;
      					$import->sku  =  (string)$subItem->code;
      					$import->price    =  (float)str_replace(".00","",$subItem->price->price);
      					$import->weight   = $subItem->weight / 1000;
      					$import->size     =  (string)$subItem->size_code;
      					$import->variant_vendor_ID       =  (string)$subItem->product_id;
                        // $PRODS_IN_CAT
if(isset($PRODS_IN_CAT[ (string)$subItem->product_id])){
    $import->supplier_cat_ids = $PRODS_IN_CAT[ (string)$subItem->product_id];
                   foreach ($PRODS_IN_CAT[ (string)$subItem->product_id] as $ca){
                       if(isset($CATS[(int)$ca]) && intval($CATS[(int)$ca])){
                           $import->category_ids[] =  intval($CATS[(int)$ca]);
                         }
                   }
                   if(sizeof(  $import->category_ids)){
                       $import->category_id = reset(  $import->category_ids);
                   }
}
                        $data = $importSupplier->import_item($import);
                           echo "import OK ".$import->name."[".$import->sku."]".PHP_EOL;
      				}
      			} else {
      				$import->sku  =  (string)$item->code;
      				$import->price    =  (float)str_replace(".00","",$item->price->price);
      				$import->weight   = $item->weight / 1000;
      				$import->size     =  (string)$item->product_size;
      				//$import->color    = null;
      				$import->variant_vendor_ID       =  (string)$item->product_id;
                    $data = $importSupplier->import_item($import);
                   echo "import OK ".$import->name."[".$import->sku."]".PHP_EOL;
      			}
      			 } catch (Exception $e){
      			   echo 'Выброшено исключение: ',  $e->getMessage(), "\n";
      			   sleep(2);
      			   continue;
      			 }
    }
$importSupplier->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 "-->";