Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- while($prod = fgetcsv($productsfile)){
- $prod = array_combine( $productsheader, $prod );
- $pricesfile = fopen("../productadmin/products.csv","r");
- $pricesheader = fgetcsv($pricesfile);
- foreach( $pricesheader as $k => $v ){
- $pricesheader[$k] = str_replace( " ", "", trim( $v ) );
- }
- while($item = fgetcsv($pricesfile)){
- $item = array_combine( $pricesheader, $item );
- $size = str_replace("'","",$item["size"]);
- $size = str_replace('"','',$size);
- $sku = str_replace(' ','-',$prod['sku'].'-'.$size.'-'.$item['material']);
- $name = $prod['name'].' '.$item['size'].' '.$item['material'];
- $product = new Mage_Catalog_Model_Product();
- $product->setTypeId('simple');
- $product->setTaxClassId(0); //none
- $product->setWebsiteIds(array(1));
- $product->setStockData(array(
- 'is_in_stock' => 1,
- 'qty' => 99999,
- 'manage_stock' => 0,
- ));
- $product->setAttributeSetId(9);
- $product->setMaterial($item['material_id']);
- $product->setDifferentPrices($item['size_id']);
- $product->setSku(ereg_replace("\n","",$sku));
- $product->setName(ereg_replace("\n","",$name));
- $product->setShortDescription($prod['short_description']);
- $product->setDescription($prod['short_description']);
- $product->setPrice($item['price']);
- $product->setWeight(5);
- $product->setStatus(1); //enabled
- $product->setVisibility(1); //nowhere
- $product->setMetaDescription(ereg_replace("\n","",$prod['short_description']));
- $product->setMetaTitle(ereg_replace("\n","",$name));
- $product->setMetaKeywords($name);
- try{
- $product->save();
- $productId = $product->getId();
- $configdata[$productId] = array(
- 0 => array(
- 'label' => $item['material'],
- 'attribute_id' => '136',
- 'value_index' => $item['material_id'],
- 'is_percent' => 0,
- 'pricing_value' => '',
- ),
- 1 => array(
- 'label' => $item['size'],
- 'attribute_id' => '134',
- 'value_index' => $item['size_id'],
- 'is_percent' => 0,
- 'pricing_value' => '',
- ),
- );
- do_log('Simple Product '.$sku.', '.$productId . ' added'.PHP_EOL);
- }
- catch (Exception $e){
- do_log($e->getMessage().PHP_EOL);
- }
- if (!in_array($item['material'], $matcheck)) {
- $matcheck[] = $item['material'];
- $matvalues[] = array(
- 'label' => $item['material'],
- 'attribute_id' => '136',
- 'value_index' => $item['material_id'],
- 'is_percent' => 0,
- 'pricing_value' => '',
- );
- }
- if (!in_array($item['size'], $sizecheck)) {
- $sizecheck[] = $item['size'];
- $sizevalues[] = array(
- 'label' => $item['size'],
- 'attribute_id' => '134',
- 'value_index' => $item['size_id'],
- 'is_percent' => 0,
- 'pricing_value' => '',
- );
- }
- }
- $attrdata = array (
- 0 => array(
- 'id' => NULL,
- 'label' => 'Material',
- 'use_default' => NULL,
- 'position' => NULL,
- 'values' => $matvalues,
- 'attribute_id' => '136',
- 'attribute_code' => 'material',
- 'frontend_label' => 'Material',
- 'store_label' => 'Material',
- 'html_id' => 'configurable__attribute_0',
- ),
- 1 => array(
- 'id' => NULL,
- 'label' => 'Size',
- 'use_default' => NULL,
- 'position' => NULL,
- 'values' => $sizevalues,
- 'attribute_id' => '134',
- 'attribute_code' => 'different_prices',
- 'frontend_label' => 'Size',
- 'store_label' => 'Size',
- 'html_id' => 'configurable__attribute_1',
- ),
- );
- $product = new Mage_Catalog_Model_Product();
- $product->setTypeId('configurable');
- $product->setTaxClassId(0); //none
- $product->setWebsiteIds(array(1)); // store id
- $product->setAttributeSetId(9);
- $product->setSku(ereg_replace("\n","",$prod['sku']));
- $product->setName(ereg_replace("\n","",$prod['name']));
- $product->setShortDescription($prod['short_description']);
- $product->setDescription($prod['short_description']);
- $product->setPrice('0.00');
- $product->setWeight(5);
- $product->setStatus(1); //enabled
- $product->setVisibility(4);
- $product->setMetaDescription(ereg_replace("\n","",$prod['short_description']));
- $product->setMetaTitle(ereg_replace("\n","",$prod['name']));
- $product->setMetaKeywords(ereg_replace("\n","",$prod['name']));
- if ($prod['category_ids']){
- $product->setCategoryIds($prod['category_ids']);
- } else {
- $product->setCategoryIds(array(1));
- }
- $productimage = $mediadir.$prod['image'];
- if (file_exists($productimage)) {
- $mediaAttribute = array ('thumbnail','small_image','image');
- $product->setMediaGallery (array('images'=>array (), 'values'=>array ()));
- $product->addImageToMediaGallery ($productimage, $mediaAttribute, false, false);
- }
- $product->setStockData(array(
- 'is_in_stock' => 1,
- 'qty' => 0,
- 'manage_stock' => 0,
- ));
- $product->getTypeInstance()->setUsedProductAttributeIds(array(134,136));
- $product->setCanSaveCustomOptions(1);
- $product->setConfigurableProductsData($configdata);
- $product->setConfigurableAttributesData($attrdata);
- $product->setCanSaveConfigurableAttributes(1);
- try{
- $product->save();
- $productId = $product->getId();
- do_log('Configurable Product '.$prod['sku'].', '.$productId . ' added'.PHP_EOL);
- }
- catch (Exception $e){
- do_log($e->getMessage().PHP_EOL);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement