cancel
Showing results for 
Search instead for 
Did you mean: 

create configurable product

create configurable product

This is running code which i am using for configurable product.
Here configurable product is using three attribute for color shape and color.
attributes are confcolor,confshape and size, which should be dropdown and with global scope, assigned to your product's attribute set.
The script is using the csv file with three columns like this.
<!-------------------------------->
ConfSku, ConfName, SimpleSku
confsku1, confname1, simple-sku1
confsku1, confname1, simple-sku2
confsku1, confname1, simple-sku3
confsku1, confname1, simple-sku4
confsku2, confname2, simple-sku5
confsku2, confname2, simple-sku6
confsku2, confname2, simple-sku6
confsku2, confname2, simple-sku8
<!-------------------------------->
<!------------Script Created By Shailesh Thapa ----------st.homespice@gmail.com---------->


<?php 

    error_reporting(E_ALL); 
    ini_set('memory_limit', -1);
    ini_set('max_execution_time', -1); 
    define('MAGENTO', realpath(dirname(__FILE__)));
	require_once MAGENTO . '/app/Mage.php';
	Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 


 
   $row = 0;
   $quantity =1; 
 

   if(($handle = fopen("braided-configurable-Homespice.csv", "r")) !== FALSE)
   { 
            
     $customeArray = array();
	 $finalCustomArray = array();
	 $i=0; 
     while(($value = fgetcsv($handle, 2000, ",")) !== FALSE) 
	  {
       
	   
       if($i>0 && $i< 18000) //4000000
       {   
                
			
			   $customeArray[] =  $value;
			      
			   if(sizeof($customeArray)>1)
			   {
							  $currentRowIndex =  $i-1;
							  $previoudRowIndex = $currentRowIndex - 1; 
							
							  if($customeArray[$currentRowIndex][0] == $customeArray[$previoudRowIndex][0])
							  { 
							  
								   $finalCustomArray[] = $customeArray[$previoudRowIndex];  // sku of product created earlier
							  }
							  else
							  {   
								   $finalCustomArray[] = $customeArray[$previoudRowIndex];  //  sku of product created earlier
								
									 createConfigurableProduct($finalCustomArray);
									 
								  
								   unset($finalCustomArray);
								 	
							 }
			    }
			 	
				
          }
		  
		  $i++;
		  
       } 
       echo "Imported All Products";
	   fclose($handle);
    } 
		
		
		
		
		
		
		function createConfigurableProduct($associatedRecords)
		{
			$sku   =  $associatedRecords[0][0];
            $isExistConfigurableProduct = checkAvailability($sku);
			
			$storeId=1;
			$websiteIds=1;
			$attributesetId = 10;
			$sku =$associatedRecords[0][0]; 
			$name = $associatedRecords[0][2]; ;
			$price = 100;
			$categoryIds ="239";
			$status = 1;
			$taxClassId = 0;
			$visibility = 4;
			
			$description="This is description product";
			$shortDescription="This is short description";
			
			$configCodeString="confcolor,confshape,size";    // YOur configurable dropdown attributes
			$configAttributeArray = explode(",",$configCodeString);
			
			$associatedSkuArray = array();
			foreach($associatedRecords as $tmpArray)
			{
				$associatedSkuArray[] = $tmpArray[1]; //simple products sku
					 
			}
			
			 if( $isExistConfigurableProduct==0)
			 {
					
						$configProduct = Mage::getModel('catalog/product');
						$configProduct
							 ->setStoreId($storeId) 
							 ->setWebsiteIds(array($websiteIds))
							 ->setAttributeSetId($attributesetId)
							 ->setTypeId('configurable') 
							 ->setCreatedAt(strtotime('now')) 
							 ->setSku($sku)
							 ->setName($name)
							 ->setStatus($status) 
							 ->setTaxClassId($taxClassId) 
							 ->setVisibility($visibility)
							 ->setPrice($price)
							 ->setDescription($description)
							 ->setShortDescription($shortDescription)
							 ->setCategoryIds(array($categoryIds))
							 ->setStockData(array(
												 'use_config_manage_stock' => 0, 
												 'manage_stock' => 1, 
												 'is_in_stock' => 1, 
										)     );
						
						
						$configurableProductsData = array();
						$configAttributesIdsArray = array();
						foreach($configAttributeArray as $configAttributeCode)
						{
						   $attributeid = Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product', $configAttributeCode);	
						   $configAttributesIdsArray[] = $attributeid;
						}
						
						$configProduct->getTypeInstance()->setUsedProductAttributeIds($configAttributesIdsArray); 
						
						foreach($associatedSkuArray as $simpleSku)
						{  
						
								$simpleProduct  =  Mage::getModel("catalog/product")->loadByAttribute("sku",$simpleSku);
								if(is_object($simpleProduct))
								 {
										 $confCount =0;
										 $simpleProductsData  = array();
										 foreach($configAttributeArray as $configAttributeCode)
										 { 
											 $configAttributeid = Mage::getResourceModel('eav/entity_attribute')
																  ->getIdByCode('catalog_product', $configAttributeCode);
																  
												 //Start --Hard coded for the configurable attributes//				  
												  $value_index='';
												  if($configAttributeCode=='confcolor')
												  { $value_index = $simpleProduct->getConfcolor(); }
												  else  if($configAttributeCode=='confshape')
												  { $value_index = $simpleProduct->getConfshape(); }
												  else  if($configAttributeCode=='size')
												  { $value_index = $simpleProduct->getSize(); }
												  //End --Hard coded for the configurable attributes//		
											 
											 $simpleProductsData[$confCount] =    array(
																						  'label'         => $simpleProduct->getAttributeText($configAttributeCode),
																						  'attribute_id'  => $configAttributeid,
																						  'value_index'   => $value_index,
																						  'is_percent'    => 0,
																						  'pricing_value' => $simpleProduct->getPrice(),
																						  
																									
																					   );
										   
											 $confCount++;
										 }
										 $configurableProductsData[$simpleProduct->getId()] = $simpleProductsData;
								 }
						 }
						
						 
						 
						
						 $configurableAttributesData = $configProduct->getTypeInstance()->getConfigurableAttributesAsArray();
					   
						
						 $configProduct->setConfigurableProductsData($configurableProductsData);
						 $configProduct->setConfigurableAttributesData($configurableAttributesData);
						 $configProduct->setCanSaveConfigurableAttributes(true);
						 
						 $configProduct->save(); 
						 echo "Configurable Product with SKU = $sku created<hr><br>"; //die;
						
			 }
			 else
			 {
			        echo "Configurable Product with SKU = $sku already exist<hr><br>"; 
				   
				  
				   
			 }
			 
		}
		
		
		
	function checkAvailability($productSku)
	{
		 
	   $_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $productSku);  
	
	   if(is_object($_product))
	   {
		  
	   
	
			   $sku = $_product->getSku();
			if($sku!='')  
			{
			   return 1;	
			}
			else
			{
			 return 0;
			} 
	 
	  }
	  else
	  {
			return 0;
	  }
	}

?> 
1 REPLY 1

Re: create configurable product

Do you face any issues into above code?

Was my answer helpful? You can Click Kudos/Accept As Solution.
200+ professional extensions for M1 & M2 with free lifetime updates!