Example #1
0
<?php
require (__DIR__.DIRECTORY_SEPARATOR.'include'.DIRECTORY_SEPARATOR.'autoload.php');
$product_from_csv = new ProductFromCsv(__DIR__.DIRECTORY_SEPARATOR.'config.ini');
$magentoProduct = $product_from_csv->import();
    /**
     * Creates te associated magento product
     * @return MagentoProduct
     */
    public function createMagentoProduct() {
        $magentoProduct = $this->magentoProduct;
        $magentoProduct->emptyData();
        if (!array_key_exists('DESCRIZIONE', $this->data)) {
             echo 'Missing mastro data'.PHP_EOL;
             return array();
        }
        $key = $this->generateKey($this->data['DESCRIZIONE']);
        $mastroCategory = $this->data['REPARTO'];
        if (
                in_array($mastroCategory, array('15.12','15.13','15.14','15.15','15.16','15.17','15.19')) 
                && preg_match('/fix/i', $this->data['DESCRIZIONE'])    
                )
             $mastroCategory = '15.c1';   


        $categories = $this->productFromCsv->getCategory($mastroCategory);
        if ($categories == false) {
            fwrite($this->productFromCsv->getMagentoExcludedCsvHandler(), $this->data['EAN13'].',"Category not found","'.$mastroCategory.'"'.PHP_EOL);
            return array();
        }
        $categoriesBranches = array_unique(preg_split('/[;,\/]/', $categories));
        $rawCategoriesWords = array_unique(preg_split('/[ ;,\/]/', strtolower($categories)));
        $categoriesWords = array();
        foreach($rawCategoriesWords as $rawCategoriesWord) {
            if (strlen($rawCategoriesWord)>3)
                $categoriesWords[] = $rawCategoriesWord;
        }
        $rawNameWords = array_unique(preg_split('/[ ;,\/]/', strtolower($this->data['DESCRIZIONE'])));
        $nameWords = array();
        foreach($rawNameWords as $rawNameWord) {
            if (strlen($rawNameWord)>3)
                $nameWords[] = $rawNameWord;
        }
        $magentoProduct->setData('categories',$categories);
        $magentoProduct->setData('sku',$this->data['EAN13']);
        $magentoProduct->setData('xus_skus',$this->getReSkus($key));
        if ($this->data['DESCRIZIONE'] == '') {
           fwrite($this->productFromCsv->getMagentoExcludedCsvHandler(), $this->data['EAN13'].',"Missing description","","'. implode(',',$this->data) .'"'.PHP_EOL);
           return array();
        }
        $this->data['DESCRIZIONE'] = ucfirst(strtolower(stripslashes($this->data['DESCRIZIONE'])));
        if ($this->data['TESTO'] == '')
            $this->data['TESTO']=$this->data['DESCRIZIONE'];
        $this->data['TESTO'] = preg_replace('/^DESCRIZIONE[ (\<br\/\>)]*/i', '', stripslashes($this->data['TESTO']));
        
        $magentoProduct->setData('name', $this->fixCategoryName($mastroCategory));
        if ($this->data['MARCA'] != '') {
            $this->data['MARCA'] = trim(strtolower(stripslashes($this->data['MARCA'])));
            switch ($this->data['MARCA']) {
               case 'jane':
                  $this->data['MARCA'] = 'jane\'';
               break;
               case 'philips':
                  $this->data['MARCA'] = 'philips avent';
               break;
            }
            $magentoProduct->setData('manufacturer', ucfirst($this->data['MARCA']).'::['.str_replace(' ','_',strtolower( iconv('UTF-8', 'ASCII//TRANSLIT',$this->data['MARCA']))).']');
            $this->data['MARCA']=ucfirst($this->data['MARCA']);
        }
        
        $magentoProduct->setData('meta_title',
            $magentoProduct->getData('name')
	);
	$magentoProduct->setData('meta_description',
            $this->data['DESCRIZIONE'].' a soli '.
            intval($this->data['VENDITA']).' euro. Vasto assortimento di '.
            strtolower(array_shift($categoriesBranches)).' della '.$this->data['MARCA'].
            ' su storebaby.it. Acquista online oggi stesso!'
	);
        $magentoProduct->setData('description', $this->data['TESTO']);
        $magentoProduct->setData('short_description', $magentoProduct->getData('name'));
        $magentoProduct->setData('meta_keyword', 'articoli infanzia,'.implode(',',  array_slice(array_unique(array_merge($categoriesWords,$nameWords)),0,5)));
        $magentoProduct->setData('url_key', 'articoli_infanzia_'.str_replace(array(' ','/','.'),'_',strtolower( $magentoProduct->getData('name') )));
        $magentoProduct->setData('url_path', 'articoli_infanzia_'.str_replace(array(' ','/','.'),'_',strtolower( $magentoProduct->getData('name') )).'.html');
        $weight = $this->productFromCsv->getWeight($mastroCategory);
        if ($weight ==false) $weight = '0.1';
        $magentoProduct->setData('weight', $weight);
        $iva =  $this->data['IVA'];
        if ($iva == '21') $iva = '22';
        //$magentoProduct->setData('price',$this->data['VENDITA']+1*($iva/100));
        if ($this->data['VENDITA'] == '') {
           fwrite($this->productFromCsv->getMagentoExcludedCsvHandler(), $this->data['EAN13'].',"Missing price",""'.PHP_EOL);
           return array();
        }
        $magentoProduct->setData('price',str_replace(',','.',str_replace('.','',$this->data['VENDITA'])));
        $magentoProduct->setData('tax_class_id', $iva);        
        if(get_class($this) == 'TestMastroProduct') {
            return $magentoProduct;
        }
        $magentoProduct->setData('qty',max(0,$this->data['ESISTENZA']-$this->data['IMPEGNATO']));
        
        preg_match('/\\\[^\\\]+$/', $this->data['FOTO_ARTICOLO'], $fileName);
        
        if($this->data['LOCAZIONE_MAG']=='99' && $magentoProduct->getData('qty')== 0) {
            if (sizeof($fileName) == 1 && $fileName[0] != '') {
               $fileName = str_replace('\\', '', $fileName[0]);
               $image = $this->mastroImageColl->deleteImage($fileName);
            }
            fwrite($this->productFromCsv->getMagentoExcludedCsvHandler(), $this->data['EAN13'].',"Code 99","","'. implode(',',$this->data) .'"'.PHP_EOL);
            return array();
        }
        
        
        if (sizeof($fileName) == 1 && $fileName[0] != '') {
            $fileName = str_replace('\\', '', $fileName[0]);
            $image = $this->mastroImageColl->resizeImage($fileName,'CONVERT_COMMAND');
            $magentoProduct->setData('image',$image);
            //$image = $this->mastroImageColl->resizeImage($fileName,'CONVERT_COMMAND_THUMBNAIL');
            $magentoProduct->setData('small_image',$image);
            $magentoProduct->setData('thumbnail',$image);
        }
        if (is_array($fileName)) {
            $fileName = current($fileName);
        }
        if ( $magentoProduct->getData('image') != '') {
            $getModifiedData = $this->mastroImageColl->getModifiedData($this->data);
            
            if ($getModifiedData != '') {
                if ($this->mastroImageColl->getModifiedDescription($this->data,$fileName)) {
                    $magentoProduct->setData('shared_on_social_networks',  '0');
                }
                $magentoProduct->setData('news_from_date',  strftime('%Y-%m-%d %H:%M:%S',$getModifiedData));
                $magentoProduct->setData('news_to_date',  strftime('%Y-%m-%d %H:%M:%S',$getModifiedData+3600 * 24 * 7 * 7 ));
                $magentoProduct->setData('modify_data',  strftime('%Y-%m-%d %H:%M:%S',$getModifiedData));
                $magentoProduct->setData('create_data',  strftime('%Y-%m-%d %H:%M:%S',$this->mastroImageColl->getCreationData($this->data)));
                
                
                foreach (self::$headers as $mastro) {
                    $magentoProduct->setData('MASTRO_'.$mastro, stripslashes($this->data[$mastro]));
                }
                if($key != '') {
                    if (!key_exists($key,$this->related))
                            $this->related[$key] = '';
                    else if ($this->related[$key] != '' )
                            $this->related[$key] .= ',';
                    $this->related[$key] .= $this->data['EAN13'];
                }
                $customPrices = $this->productFromCsv->getCustomPrices($magentoProduct->getData('sku'));
                
                $useCupon = '';
                if (sizeof($customPrices)>0) {
                    foreach($customPrices as $store =>$customPrice ) {
                        if ($useCupon =='') $useCupon = $customPrice['use_cupon'];
                    }
                }
                
                $magentoProduct->setData('use_cupon',$useCupon);
                
                $magentoProductColl = array(
                     0=>$magentoProduct
                );
                if ($this->data['ESISTENZA']-$this->data['IMPEGNATO'] == 0) {
                    $id=sizeof($magentoProductColl);
                    $magentoProductColl[$id]= clone $magentoProduct;
                    $magentoProductColl[$id]->setData('store',  'retail'); 
                    $magentoProductColl[$id]->setData('status', '2'); 					 
                }
                
                if (sizeof($customPrices)>0) {
                   foreach ($magentoProductColl as $magentoProductItem) {
                     foreach($customPrices as $store =>$customPrice ) {
                        if ($store == $magentoProductItem->getData('store')) {                     
                           $magentoProductItem->setData('price',$customPrice['special_price']);
                           $magentoProductItem->setData('show_cupon',$customPrice['use_cupon']);
                           unset ($customPrices[$store]);
                           fwrite($this->productFromCsv->getCustomPricesHandler(),implode(',', array($magentoProductItem->getData('sku'),$store,$customPrice['special_price'],$customPrice['use_cupon'])).PHP_EOL);
                        }
                     }
                  }
                  if (sizeof($customPrices)>0) {
                     foreach($customPrices as $store =>$customPrice ) {
                        $id=sizeof($magentoProductColl);
                        $magentoProductColl[$id]= clone $magentoProduct;
                        $magentoProductColl[$id]->setData('store',  $store); 
                        $magentoProductColl[$id]->setData('price', $customPrice['special_price']);
                        $magentoProductColl[$id]->setData('show_cupon',$customPrice['use_cupon']);
                        fwrite($this->productFromCsv->getCustomPricesHandler(),implode(',', array($magentoProduct->getData('sku'),$store,$customPrice['special_price'],$customPrice['use_cupon'])).PHP_EOL);
                     }
                  }
                }
                
                return $magentoProductColl;
            }
            else {
               fwrite($this->productFromCsv->getMagentoExcludedCsvHandler(), $this->data['EAN13'].',"Nothing modified","","'. implode(',',$this->data) .'"'.PHP_EOL);
               return array();
            };
            
        } else {
            fwrite($this->productFromCsv->getMagentoExcludedCsvHandler(), $this->data['EAN13'].',"Missing image","","'. implode(',',$this->data) .'"'.PHP_EOL);
            return array();
        }
    }
 public function __construct($sconfig_file) {
     parent::__construct($sconfig_file);
     $this->categories['15.1']='Test';
     $this->categories['15.2']='Test';
 }