Jun 222012
 

Below code add product with image and custom field data.

<?php
	error_reporting(E_ALL | E_STRICT);
	$mageFilename = 'app/Mage.php';
	require_once $mageFilename;
	$app = Mage::app('default'); 
	
	ini_set('display_errors', 1);


	$api = new Mage_Catalog_Model_Product_Api();
	
	$attribute_api = new Mage_Catalog_Model_Product_Attribute_Set_Api();
	$attribute_sets = $attribute_api->items();
	
	$productData = array(); 
	$productData['website_ids'] = array(1); 
	$productData['categories'] = array(23);

	$productData['status'] = 1;
	
	$productData['name'] = utf8_encode('nimit');
	$productData['description'] = utf8_encode('nimit desc');
	$productData['short_description'] = utf8_encode('nimit short desc');
	
	$productData['price'] = 12.34;
	$productData['weight'] = 23.25;
	$productData['tax_class_id'] =2;
	$productData['page_layout'] ='two_columns_left';
		
	$new_product_id = $api->create('simple',$attribute_sets[0]['set_id'],'ND3',$productData);
	
	print_r($new_product_id);
	
	$stockItem = Mage::getModel('cataloginventory/stock_item');
	$stockItem->loadByProduct( $new_product_id );
	
	$stockItem->setData('use_config_manage_stock', 1);
	$stockItem->setData('qty', 100);
	$stockItem->setData('min_qty', 0);
	$stockItem->setData('use_config_min_qty', 1);
	$stockItem->setData('min_sale_qty', 0);
	$stockItem->setData('use_config_max_sale_qty', 1);
	$stockItem->setData('max_sale_qty', 0);
	$stockItem->setData('use_config_max_sale_qty', 1);
	$stockItem->setData('is_qty_decimal', 0);
	$stockItem->setData('backorders', 0);
	$stockItem->setData('notify_stock_qty', 0);
	$stockItem->setData('is_in_stock', 1);
	$stockItem->setData('tax_class_id', 0);
	
	$stockItem->save();
	
	$product = Mage::getModel('catalog/product')->load($new_product_id);
	
	$product->setMediaGallery (array('images'=>array (), 'values'=>array ()));
	$product->addImageToMediaGallery ('E:/High Res Images/High Res Images/GC00012.jpg', array ('image','small_image','thumbnail'), false, false); 
	$product->addImageToMediaGallery ('E:/High Res Images/High Res Images/GC00014_1.jpg', array ('image','small_image','thumbnail'), false, false); 
	Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 

	$product->save();
	
	
	$product = Mage::getModel('catalog/product')->load($new_product_id);
	$optionData = 	array(
						"title" => "Custom Text Field Option Title 1",
						"type" => "field",
						"is_require" => 1,
						"sort_order" => 1,
						"price" => 0,
							"price_type" => "fixed",
							"sku" => "",
							"max_characters" => 15
					);
	
	$product->setHasOptions(1);
	$option = Mage::getModel('catalog/product_option')
			  ->setProductId($new_product_id)
			  ->setStoreId(1)
			  ->addData($optionData);
	$option->save();
	$product->addOption($option);
	
	Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 

	$product->save();
	
	$product = Mage::getModel('catalog/product')->load($new_product_id);
	$optionData = 	array(
						"title" => "Custom Text Field Option Title 2",
						"type" => "field",
						"is_require" => 1,
						"sort_order" => 2,
						"price" => 0,
							"price_type" => "fixed",
							"sku" => "",
							"max_characters" => 25
					);
	
	$product->setHasOptions(1);
	$option = Mage::getModel('catalog/product_option')
			  ->setProductId($new_product_id)
			  ->setStoreId(1)
			  ->addData($optionData);
	$option->save();
	$product->addOption($option);
	
	Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 

	$product->save();
?>

  32 Responses to “How to add product programmatically in magento 1.7.0 with image and custom field”

  1. Do you know how to add news_fron_date value on th fly?

    Thanks in advance

  2. can you please be more specific?

  3. Hi. I use your script it works fine but doesnt add images. Can you help me with this problems?

  4. You need to change E:/High Res Images/High Res Images/GC00012.jpg to your absolute image path. if you are running this script on server then give server’s absolute image path.

    Hope this may help

  5. Hello nDudani,

    I am new to Magento. Could you please guide me on how to update an existing product your above code programmatically in magento 1.7.0.

    Please advise.

  6. oh, i’m magento noob =(
    can you describe a full process of programmatically add product, please?
    we need create a addition module for that? or what?
    thank you

  7. Hi, i am not able to add images via passing a url(http://media.asicdn.com/images/jpgo/6080000/6089330.jpg). why? Please advise.

  8. $url = ‘http://media.asicdn.com/images/jpgo/6080000/6089330.jpg)';
    $img = ‘/my/folder/flower.gif';
    file_put_contents($img, file_get_contents($url));

    // now use this variable : $img

    $product->addImageToMediaGallery ($img , array (‘image’,’small_image’,’thumbnail’), false, false);

  9. @ Dee

    This is the individual file to add single product at a time.

    You can change this as per your need.

    Please email author for details answer with detail query.

    nimitdudani [at] gmail [dot] com

  10. hi, it’s helpful for me to add import product from my old store. is there any way to update or ignore my existing product in magento, because of duplication of products in magento.

    please help if any one have!!

    Thanks :-)

  11. Hi,
    I’m looking for a way to create configurable products on one page, with attributes fields, price, and so on.
    I’m a newbie and don’t know where to start. How can I make a form with all attributes on one page to send datas to this script and then save a configurable product ? I would also be able to set some default value if I’ve control on this form (I’ve then to create single products based on configurable one).
    it’s there any module doing this ?

    please help, I’m stuck and really need this, I’ve so many products to insert in the catalog that I loose a lot of time doing it, it also really slow down the website when I do insert a product.

    Bob

  12. $productData[‘news_from_date’] = date(‘Y-m-d’);
    $productData[‘news_to_date’] = strtotime(“+1 week”);

    this is for new from date

  13. can u please specify the directory structure for this code and file name..

  14. this is the error I get : Fatal error: Uncaught exception 'Mage_Eav_Model_Entity_Attribute_Exception' with message 'The value of attribute "SKU" must be unique' in /home1/shubadje/public_html/app/code/core/Mage/Core/Model/Config.php:1348 Stack trace: #0 /home1/shubadje/public_html/app/Mage.php(462): Mage_Core_Model_Config->getModelInstance('eav/entity_attr...', 'The value of at...') #1 /home1/shubadje/public_html/app/code/core/Mage/Eav/Model/Entity/Abstract.php(661): Mage::getModel('eav/entity_attr...', 'The value of at...') #2 /home1/shubadje/public_html/app/code/core/Mage/Eav/Model/Entity/Abstract.php(830): Mage_Eav_Model_Entity_Abstract->walkAttributes('backend/validat...', Array) #3 /home1/shubadje/public_html/app/code/core/Mage/Catalog/Model/Product.php(188): Mage_Eav_Model_Entity_Abstract->validate(Object(Mage_Catalog_Model_Product)) #4 /home1/shubadje/public_html/app/code/core/Mage/Catalog/Model/Product/Api.php(196): Mage_Catalog_Model_Product->validate() #5 /home1/shubadje/public_html/errorin.php(30): Mage_Catalog_Model_Product_Api->create in /home1/shubadje/public_html/app/code/core/Mage/Core/Model/Config.php on line 1348

  15. […] am using the code I found on this blog : http://www.errorin.com/open-source/ho... but it returns the following errors: Please note I did change both SKU values to unique […]

  16. Hi, I am doing a project in magento which has stitching options on every product. When i add custom options in admin, it takes too much time. I saw your blog but couldn’t understand where i should add above code of block. Plz help me.

  17. nDudani…
    i am getting this error called.
    image details and type to be specified foe each store view

  18. $productData[‘website_ids’] = array(1);

    correct this ID

  19. Hi,

    Very nice code, but I have question, where the folder do I need put it this file code?

    Thanks.

  20. @Rodrigo, you have to create file and save in root directory where magneto installed

  21. How to add special price??

  22. Just commenting to say you rock! Thanks man.

  23. You are my hero!!
    I was going crazy to add new products.
    Many thanks

    W.

  24. Thank you very much. My thumbnail image was not appearing. Adding Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); made all the difference. Thanks!

  25. Thank you so much Nimit for nice code…

  26. Thanks a lot Nitim, this code is very helpful, really nice code. I

  27. please let me know if I use upload multi image and set main image, small image .etc..
    I use flash uploader of magento

    thank you so much

  28. I want to assign multiple level of category to the product.

    How can I add it?

    $productData[‘categories’] = array(23);

  29. Add category ids saparated with “,” like $productData[‘categories’] = array(0,2,15,21,23);

  30. Hi nDudani,

    Thank you for your valuable notes.

    The following script uploaded the image. But, it is not displaying under MAIN image. It shows under more view image. How can I make it as main image?

    $product = Mage::getModel(‘catalog/product’)->load($new_product_id);

    $product->setMediaGallery (array(‘images’=>array (), ‘values’=>array ()));

    $product->addImageToMediaGallery (‘/store/productimages/’.$product_image, array (‘image’,’small_image’,’thumbnail’), false, false);

    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

    $product->save();

  31. Nimit Dudani, I have found the solutions.

    Thank you.

  32. Thanks U are awesome dear

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>