Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); $productArr = []; $tmpFilename = $_FILES["docname"]["tmp_name"]; $csv = new Varien_File_Csv(); $data = $csv->getData($tmpFilename); $skuArr = array(); foreach ($data as $value) { $skuArr[] = $value[0]; $attributeColorCode = 'color'; $attributeSizeCode = 'size'; // return function to getting the attribute color id $attribute_details = Mage::getSingleton("eav/config")->getAttribute('catalog_product', $attributeColorCode); $attribute = $attribute_details->getData(); $attribute_colorid = $attribute['attribute_id']; // return function to getting the attribute size id $attribute_details = Mage::getSingleton("eav/config")->getAttribute('catalog_product', $attributeSizeCode); $attribute = $attribute_details->getData(); $attribute_sizeid = $attribute['attribute_id']; $optionColorId = Mage::helper('bulkupload')->getOptionId($attributeColorCode,$value[15]); $optionSizeId = Mage::helper('bulkupload')->getOptionId($attributeSizeCode,$value[16]); $cat = Mage::helper('bulkupload')->getCategoryNameById($value[3]); if($value[1]=='simple'){ $simpleProduct = Mage::getModel('catalog/product'); try { $simpleProduct ->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array ->setAttributeSetId($value[17]) //ID of a attribute set named 'default' ->setTypeId($value[1]) //product type ->setCreatedAt(strtotime('now')) //product creation time ->setSku($value[0]) //SKU ->setName($value[2]) //product name ->setWeight($value[6]) ->setStatus(1) //product status (1 - enabled, 2 - disabled) ->setTaxClassId(0) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping) ->setVisibility(1) //catalog and search visibility ->setColor($optionColorId) ->setSize($optionSizeId) ->setNewsFromDate('') //product set as new from ->setNewsToDate('') //product set as new to ->setPrice($value[7]) //price in form 11.22 ->setSpecialPrice('') //special price in form 11.22 ->setSpecialFromDate('') //special price from (MM-DD-YYYY) ->setSpecialToDate('') //special price to (MM-DD-YYYY) ->setMetaTitle($value[8]) ->setMetaKeyword($value[10]) ->setMetaDescription($value[9]) ->setDescription($value[4]) ->setShortDescription($value[5]); if(!empty($value[14])){ $galleryData = explode(',',$value[14]); $simpleProduct->setMediaGallery (array('images'=>array (), 'values'=>array ())); foreach($galleryData as $gallery_img) { if ($gallery_img){ $simpleProduct->addImageToMediaGallery($gallery_img, array ('image','small_image','thumbnail'), false, false); } else{ $err = 1; Mage::getSingleton('core/session')->addError($this->__('Image Path of '.$value[0].' Not Exist !!!')); } } } $simpleProduct->setStockData(array( 'use_config_manage_stock' => 0, //'Use config settings' checkbox 'manage_stock' => 1, //manage stock 'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart 'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart 'is_in_stock' => 1, //Stock Availability 'qty' => $value[11] //qty ) ) // $cat = $this->getCategoryNameById($value[3]); ->setCategoryIds($cat); //assign product to categories $simpleProduct->save(); $productId = $simpleProduct->getId(); $simpleProductArr[] = $productId; } catch (Exception $e) { Mage::log($e->getMessage()); echo $e->getMessage(); } } /* Configurable Product Insert Section */ if(count($simpleProductArr)>0 && $value[1]=='configurable'){ $configProduct = Mage::getModel('catalog/product'); try { $configProduct // ->setStoreId(1) //you can set data in store scope ->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array ->setAttributeSetId($value[17]) //ID of a attribute set named 'default' ->setTypeId($value[1]) //product type ->setCreatedAt(strtotime('now')) //product creation time // ->setUpdatedAt(strtotime('now')) //product update time ->setSku($value[0]) //SKU ->setName($value[2]) //product name ->setWeight($value[6]) ->setStatus(1) //product status (1 - enabled, 2 - disabled) ->setTaxClassId(0) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping) ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) //catalog and search visibility //->setManufacturer(28) //manufacturer id //->setColor($value[15]) //->setSize($value[16]) ->setNewsFromDate('') //product set as new from ->setNewsToDate('') //product set as new to ->setPrice($value[7]) //price in form 11.22 ->setSpecialPrice('') //special price in form 11.22 ->setSpecialFromDate('') //special price from (MM-DD-YYYY) ->setSpecialToDate('') //special price to (MM-DD-YYYY) ->setMetaTitle($value[8]) ->setMetaKeyword($value[10]) ->setMetaDescription($value[9]) ->setDescription($value[4]) ->setShortDescription($value[5]); if(!empty($value[14])){ $galleryData = explode(',',$value[14]); $configProduct->setMediaGallery (array('images'=>array (), 'values'=>array ())); foreach($galleryData as $gallery_img) { if ($gallery_img){ $configProduct->addImageToMediaGallery($gallery_img, array ('image','small_image','thumbnail'), false, false); } else{ $err = 1; Mage::getSingleton('core/session')->addError($this->__('Image Path of '.$value[0].' Not Exist !!!')); //$this->_redirect('musbury/index/index/'); } } } $configProduct->setStockData(array( 'use_config_manage_stock' => 0, //'Use config settings' checkbox 'manage_stock' => 1, //manage stock 'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart 'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart 'is_in_stock' => 1, //Stock Availability 'qty' => $value[11] //qty ) ) // $cat = $this->getCategoryNameById($value[3]); ->setCategoryIds($cat); //assign product to categories $simpleProducts = Mage::getResourceModel('catalog/product_collection') ->addIdFilter($simpleProductArr) ->addAttributeToSelect('color') ->addAttributeToSelect('size') ->addAttributeToSelect('price'); $configProduct->setCanSaveConfigurableAttributes(true); $configProduct->setCanSaveCustomOptions(true); $configProduct->getTypeInstance()->setUsedProductAttributeIds(array($attribute_colorid,$attribute_sizeid)); //attribute ID of attribute 'color' in my store $configurableAttributesData = $configProduct->getTypeInstance()->getConfigurableAttributesAsArray(); $configProduct->setCanSaveConfigurableAttributes(true); $configProduct->setConfigurableAttributesData($configurableAttributesData); $configurableProductsData = array(); foreach ($simpleProducts as $simple) { $productData = array( 'label' => $simple->getAttributeText('color'), 'attribute_id' => $attribute_colorid, 'value_index' => (int) $simple->getColor(), 'is_percent' => 0, 'pricing_value' => $simple->getPrice() ); $configurableProductsData[$simple->getId()] = $productData; $configurableAttributesData[0]['values'][] = $productData; $productData = array( 'label' => $simple->getAttributeText('size'), 'attribute_id' => $attribute_sizeid, 'value_index' => (int) $simple->getSize(), 'is_percent' => 0, 'pricing_value' => $simple->getPrice() ); $configurableProductsData[$simple->getId()] = $productData; $configurableAttributesData[1]['values'][] = $productData; } $configProduct->setConfigurableProductsData($configurableProductsData); $configProduct->setConfigurableAttributesData($configurableAttributesData); $configProduct->setCanSaveConfigurableAttributes(true); Mage::log($configurableProductsData, null, 'configurableProductsData.log', true); Mage::log($configurableAttributesData, null, 'configurableAttributesData.log', true); $configProduct->save(); $confId = $configProduct->getId(); // saving the configurable option attribute price value if($configProduct->getId()!='') { $configurable = Mage::getModel('catalog/product')->load($confId); $simpleProducts = Mage::getResourceModel('catalog/product_collection') ->addIdFilter($simpleProductArr) ->addAttributeToSelect('color') ->addAttributeToSelect('size') ->addAttributeToSelect('price'); $configurableProductsData = array(); $configurableAttributesData = $configurable->getTypeInstance()->getConfigurableAttributesAsArray(); foreach ($simpleProducts as $simple) { $productData = array( 'label' => $simple->getAttributeText('color'), 'attribute_id' => $attribute_colorid, 'value_index' => (int) $simple->getColor(), 'is_percent' => 0, 'pricing_value' => $simple->getPrice() ); $configurableProductsData[$simple->getId()] = $productData; $configurableAttributesData[0]['values'][] = $productData; $productData = array( 'label' => $simple->getAttributeText('size'), 'attribute_id' => $attribute_sizeid, 'value_index' => (int) $simple->getSize(), 'is_percent' => 0, 'pricing_value' => $simple->getPrice() ); $configurableProductsData[$simple->getId()] = $productData; $configurableAttributesData[1]['values'][] = $productData; } $configurable->setConfigurableProductsData($configurableProductsData); $configurable->setConfigurableAttributesData($configurableAttributesData); $configurable->setCanSaveConfigurableAttributes(true); Mage::log($configurableProductsData, null, 'configurableProductsData.log', true); Mage::log($configurableAttributesData, null, 'configurableAttributesData.log', true); $configurable->save(); $simpleProductArr = array(); echo "SKU:".$value[0].' added sucessfully'."<br />"; } } catch (Exception $e) { $simpleProductArr = array(); Mage::log($e->getMessage()); echo "SKU:".$value[0].' added unsucessfully'."<br />"; } } } }
Solved! Go to Solution.
Hi,
Thanks for the code, but is anybody able to explain how it's implemented?
Thanks.