magento add simple product programmatically with custom options

magento add simple product programmatically with custom options

In this post we will show you how to add add simple product magento store using custom option. In following code we can add simple product programmatically using code with external php file.

error_reporting(E_ALL | E_STRICT);
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
umask(0);
Mage::app();
ini_set('display_errors', 1);
$date = date("m/d/Y");
// Generate random sku 
$randome_sku = date("YmdHis")."-".rand(10,100);
$sku = 'onlinecode-'.$randome_sku;
$name = 'onlinecode Product name';
$description = 'Test Product Description';
$short_description = 'Test Product Short Description';
$price = '99.99';
$special_price = '88.99';
$special_from_date = $date;
$special_to_date = $date;
$category_ids = array(3,4,5);
$tax_Class_Id  = 0;    
// None
$visibility = 4;    
// catalog, search
$product_Status = 1;    
// enabled
$created_Date = $date;
$updatedDate = $date;
// add your image path heat
$image_path = 'C:\Users\Public\Pictures\Sample Pictures\Desert.jpg';// absolute path of image in local file system/server path
$pro_colors = 7;    
// Dropdown Attribute i.e 7 is attribute option id
$pro_brands = '12,14,15';    
// Multiselect Attribute must be pass as string 
// example for  12,14 and 15 are attribute option's id
$add_product = Mage::getModel('catalog/product');
$add_product->setSku($sku);
$add_product->setName($name);
$add_product->setDescription($description);
$add_product->setShortDescription($short_description);
// $product->setUrlKey($data['5']);    
// Uncomment only if custom url type
$add_product->setPrice($price);
$add_product->setSpecialPrice($special_price);
$add_product->setSpecialFromDate($special_from_date);
$add_product->setSpecialToDate($special_to_date);
$add_product->setTypeId('simple');
$add_product->setAttributeSetId(4); 
// enter the catalog attribute set id here
$add_product->addImageToMediaGallery($image_path,'image',true,false); 
// absolute path of image in local file system
$add_product->setCategoryIds($category_ids); 
// id of categories
$add_product->setWeight(1.0);
$add_product->setTaxClassId($tax_Class_Id);
$add_product->setVisibility($visibility);
$add_product->setStatus($product_Status);
$add_product->setColor($pro_colors);
$add_product->setBrand($pro_brands);
$add_product->setStockData(
	array(
		'manage_stock' => 1,
		'is_in_stock' => 1,
		'qty' => 100
	)
);
// assign product to the default website
$add_product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
// $add_product->setCreatedAt($created_Date);    
// uncomment if add custom date
// $add_product->setUpdatedAt($updated_Date);    
// uncomment if add custom date	
try
{
	/*  
	* Add custom options 
	*/
	// add size of product
	$product_sizes = array(M,L,XL,XXL);
	if(count(array_filter($product_sizes)) > 0)
	{
		$product_options = array();
		$option_data = array();
		for($i = 0; $i < count($product_sizes); $i++)
		{
			$product_options[$i]['is_delete'] = '';
			$product_options[$i]['title'] = $product_sizes[$i];
			$product_options[$i]['price_type'] = 'fixed';
			$product_options[$i]['price'] = '';
			$product_options[$i]['sku'] = '';
		}
		$option_data = array(
			'is_delete'         => 0,
			'is_require'        => false,
			'previous_group'    => '',
			'title'             => 'Size',
			'type'              => 'drop_down',
			'sort_order'        => 1,
			'values'            => $product_options
		);
		$option_Instance = $add_product->getOptionInstance()->unsetOptions();
		$add_product->setHasOptions(1);
		$option_Instance->addOption($option_data);
		$option_Instance->setProduct($add_product);
	}	
	$add_product->save();
	// print message of Success
	echo "product id ".$add_product->getId().'  Save Successfully';
}
catch (Exception $exception) {
	echo $exception->getMessage();
	// Handle the error / exception
}

Leave a Comment

Your email address will not be published. Required fields are marked *

17  +    =  21

We accept paid advance and paid guest Posting on our Site : Contact US