Advertisement
Guest User

MagentoImport

a guest
Oct 10th, 2013
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.58 KB | None | 0 0
  1. while($prod = fgetcsv($productsfile)){
  2.   $prod = array_combine( $productsheader, $prod );
  3.  
  4.   $pricesfile = fopen("../productadmin/products.csv","r");
  5.   $pricesheader = fgetcsv($pricesfile);
  6.   foreach( $pricesheader as $k => $v ){
  7.     $pricesheader[$k] = str_replace( " ", "", trim( $v ) );
  8.   }
  9.     while($item = fgetcsv($pricesfile)){
  10.       $item = array_combine( $pricesheader, $item );
  11.  
  12.       $size = str_replace("'","",$item["size"]);
  13.       $size = str_replace('"','',$size);
  14.       $sku = str_replace(' ','-',$prod['sku'].'-'.$size.'-'.$item['material']);
  15.       $name = $prod['name'].' '.$item['size'].' '.$item['material'];
  16.  
  17.         $product = new Mage_Catalog_Model_Product();
  18.         $product->setTypeId('simple');
  19.         $product->setTaxClassId(0); //none
  20.         $product->setWebsiteIds(array(1));
  21.         $product->setStockData(array(
  22.             'is_in_stock' => 1,
  23.             'qty' => 99999,
  24.             'manage_stock' => 0,
  25.         ));
  26.         $product->setAttributeSetId(9);
  27.         $product->setMaterial($item['material_id']);
  28.         $product->setDifferentPrices($item['size_id']);
  29.         $product->setSku(ereg_replace("\n","",$sku));
  30.         $product->setName(ereg_replace("\n","",$name));
  31.         $product->setShortDescription($prod['short_description']);
  32.         $product->setDescription($prod['short_description']);
  33.         $product->setPrice($item['price']);
  34.         $product->setWeight(5);
  35.         $product->setStatus(1); //enabled
  36.         $product->setVisibility(1); //nowhere
  37.         $product->setMetaDescription(ereg_replace("\n","",$prod['short_description']));
  38.         $product->setMetaTitle(ereg_replace("\n","",$name));
  39.         $product->setMetaKeywords($name);
  40.  
  41.         try{
  42.             $product->save();
  43.             $productId = $product->getId();
  44.  
  45.             $configdata[$productId] = array(    
  46.                     0 => array(
  47.                        'label' => $item['material'],
  48.                        'attribute_id' => '136',
  49.                        'value_index' => $item['material_id'],
  50.                        'is_percent' => 0,
  51.                        'pricing_value' => '',
  52.                     ),
  53.                     1 => array(
  54.                        'label' => $item['size'],
  55.                        'attribute_id' => '134',
  56.                        'value_index' => $item['size_id'],
  57.                        'is_percent' => 0,
  58.                        'pricing_value' => '',
  59.                     ),
  60.                 );
  61.  
  62.             do_log('Simple Product '.$sku.', '.$productId . ' added'.PHP_EOL);
  63.         }
  64.         catch (Exception $e){      
  65.             do_log($e->getMessage().PHP_EOL);
  66.         }
  67.  
  68.     if (!in_array($item['material'], $matcheck)) {
  69.       $matcheck[] = $item['material'];
  70.       $matvalues[] = array(
  71.            'label' => $item['material'],
  72.            'attribute_id' => '136',
  73.            'value_index' => $item['material_id'],
  74.            'is_percent' => 0,
  75.            'pricing_value' => '',
  76.         );
  77.     }  
  78.     if (!in_array($item['size'], $sizecheck)) {
  79.       $sizecheck[] = $item['size'];
  80.       $sizevalues[] = array(
  81.            'label' => $item['size'],
  82.            'attribute_id' => '134',
  83.            'value_index' => $item['size_id'],
  84.            'is_percent' => 0,
  85.            'pricing_value' => '',
  86.         );
  87.     }
  88.  
  89.   }
  90.  
  91.   $attrdata = array (
  92.     0 => array(
  93.        'id' => NULL,
  94.        'label' => 'Material',
  95.        'use_default' => NULL,
  96.        'position' => NULL,
  97.        'values' => $matvalues,
  98.        'attribute_id' => '136',
  99.        'attribute_code' => 'material',
  100.        'frontend_label' => 'Material',
  101.        'store_label' => 'Material',
  102.        'html_id' => 'configurable__attribute_0',
  103.     ),
  104.     1 => array(
  105.        'id' => NULL,
  106.        'label' => 'Size',
  107.        'use_default' => NULL,
  108.        'position' => NULL,
  109.        'values' => $sizevalues,
  110.        'attribute_id' => '134',
  111.        'attribute_code' => 'different_prices',
  112.        'frontend_label' => 'Size',
  113.        'store_label' => 'Size',
  114.        'html_id' => 'configurable__attribute_1',
  115.     ),
  116.   );
  117.  
  118.   $product = new Mage_Catalog_Model_Product();
  119.   $product->setTypeId('configurable');
  120.   $product->setTaxClassId(0); //none
  121.   $product->setWebsiteIds(array(1));  // store id
  122.   $product->setAttributeSetId(9);
  123.   $product->setSku(ereg_replace("\n","",$prod['sku']));
  124.   $product->setName(ereg_replace("\n","",$prod['name']));
  125.   $product->setShortDescription($prod['short_description']);
  126.   $product->setDescription($prod['short_description']);
  127.   $product->setPrice('0.00');
  128.   $product->setWeight(5);
  129.   $product->setStatus(1); //enabled
  130.   $product->setVisibility(4);
  131.   $product->setMetaDescription(ereg_replace("\n","",$prod['short_description']));
  132.   $product->setMetaTitle(ereg_replace("\n","",$prod['name']));
  133.   $product->setMetaKeywords(ereg_replace("\n","",$prod['name']));
  134.  
  135.   if ($prod['category_ids']){
  136.     $product->setCategoryIds($prod['category_ids']);
  137.   } else {
  138.     $product->setCategoryIds(array(1));
  139.   }
  140.  
  141.   $productimage = $mediadir.$prod['image'];
  142.   if (file_exists($productimage)) {    
  143.     $mediaAttribute = array ('thumbnail','small_image','image');
  144.     $product->setMediaGallery (array('images'=>array (), 'values'=>array ()));
  145.     $product->addImageToMediaGallery ($productimage, $mediaAttribute, false, false);
  146.   }
  147.   $product->setStockData(array(
  148.     'is_in_stock' => 1,
  149.     'qty' => 0,
  150.     'manage_stock' => 0,
  151.   ));
  152.   $product->getTypeInstance()->setUsedProductAttributeIds(array(134,136));
  153.   $product->setCanSaveCustomOptions(1);
  154.   $product->setConfigurableProductsData($configdata);
  155.   $product->setConfigurableAttributesData($attrdata);
  156.   $product->setCanSaveConfigurableAttributes(1);
  157.  
  158.   try{
  159.     $product->save();
  160.     $productId = $product->getId();
  161.     do_log('Configurable Product '.$prod['sku'].', '.$productId . ' added'.PHP_EOL);
  162.   }
  163.   catch (Exception $e){        
  164.     do_log($e->getMessage().PHP_EOL);
  165.   }
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement