Magento can't create product - magento

In magento I created simple product with this code:
try {
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product');
$product
->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array
->setAttributeSetId(9) //ID of a attribute set named 'default'
->setTypeId('simple') //product type
->setCreatedAt(strtotime('now')) //product creation time
->setSku('testsku17') //SKU
->setName('test product17') //product name
->setWeight(8.00)
->setStatus(1) //product status (1 - enabled, 2 - disabled)
->setTaxClassId(4) //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(24)
->setNewsFromDate(strtotime('now')) //product set as new from
->setNewsToDate('06/30/2015') //product set as new to
->setCountryOfManufacture('AF') //country of manufacture (2-letter country code)
->setPrice(11.22) //price in form 11.22
->setCost(22.33) //price in form 11.22
->setSpecialPrice(3.44) //special price in form 11.22
->setSpecialFromDate(strtotime('now')) //special price from (MM-DD-YYYY)
->setSpecialToDate('06/30/2015') //special price to (MM-DD-YYYY)
->setMsrpEnabled(1) //enable MAP
->setMsrpDisplayActualPriceType(1) //display actual price (1 - on gesture, 2 - in cart, 3 - before order confirmation, 4 - use config)
->setMsrp(99.99) //Manufacturer's Suggested Retail Price
->setMetaTitle('test meta title')
->setMetaKeyword('testproduct')
->setMetaDescription('test meta description')
->setDescription('This is a long description')
->setShortDescription('This is a short description')
->setMediaGallery (array('images'=>array (), 'values'=>array ())) //media gallery initialization
->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' => 999 //qty
)
)
->setCategoryIds(array(3, 10)); //assign product to categories
$product->save();
}catch(Exception $e){
Mage::log($e->getMessage());
}
First time it was succesful created product, but it was displayed only in sql.
If I tried create another product, its nothing happend.
Now I delete products and try to create again, but nothing happens.
Hope your help!

my code is:
$product = Mage::getModel('catalog/product');
$product->setWebsiteIds(array(1));
$product->setTypeId('simple');
$product->setName('TESTTEST');
$product->setDescription('TESTPRODUKT');
$product->setShortDescription(' ');
$product->setStatus(1);
$product->setTaxClassId(1);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH); //catalog and search visibility
$product->setWeight(0);
$product->setCreatedAt(strtotime('now'));
$product->setManufacturer(1);
$product->setAttributeSetId(2);
$product->setSku('SKUTEST222');
$product->setPrice(5);
$product->setVisibility(1);
$product->save();

Related

Magento 2.2.5, how to create a configurable product with simple products associated

I want to create programmatically a configurable product with a simple product associated to it.
The script is executed with no errors but I can only see the configurable product created with no assocciation, while the simple product is created but not linked with anything.
Any suggest? Here it is my code:
<?php
use \Magento\Framework\App\Bootstrap;
include('../app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$url = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $url->get('\Magento\Store\Model\StoreManagerInterface');
$mediaurl= $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');
$simple_product = $objectManager->create('\Magento\Catalog\Model\Product');
$simple_product->setSku('test-simple-32');
$simple_product->setName('test name simple 32');
$simple_product->setAttributeSetId(4);
$simple_product->setColor(10);
//$simple_product->setSize_general(10); // value id of S size
$simple_product->setStatus(1);
$simple_product->setTypeId('simple');
$simple_product->setPrice(10);
$simple_product->setWebsiteIds(array(1));
$simple_product->setCategoryIds(array(31));
$simple_product->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' => 100 //qty
)
);
$simple_product->save();
$simple_product_id = $simple_product->getId();
echo "simple product id: ".$simple_product_id."\n";
//configurable product
$configurable_product = $objectManager->create('\Magento\Catalog\Model\Product');
$configurable_product->setSku('test-configurable-26');
$configurable_product->setName('test name configurable 26');
$configurable_product->setAttributeSetId(93);
$configurable_product->setStatus(1);
$configurable_product->setTypeId('configurable');
$configurable_product->setPrice(11);
$configurable_product->setWebsiteIds(array(1));
$configurable_product->setCategoryIds(array(31));
$configurable_product->setStockData(array(
'use_config_manage_stock' => 0, //'Use config settings' checkbox
'manage_stock' => 1, //manage stock
'is_in_stock' => 1, //Stock Availability
)
);
$configurable_product->save();
$configProductId = $configurable_product->getId();
$attributeSetId = 93;
$associatedProductIds=array($simple_product_id);
$optionsFactory = $objectManager->create('\Magento\ConfigurableProduct\Helper\Product\Options\Factory');
$configurableOptions = $optionsFactory->create($configurableProductsData);
$extensionConfigurableAttributes = $configurable_product->getExtensionAttributes();
$extensionConfigurableAttributes->setConfigurableProductOptions($configurableOptions);
$extensionConfigurableAttributes->setConfigurableProductLinks($associatedProductIds);
$configurable_product->setExtensionAttributes($extensionConfigurableAttributes);
$associatedProductIds=array($simple_product_id);
$configurable_product->setAssociatedProductIds($associatedProductIds);
$configurable_product->setConfigurableProductsData($configurableProductsData);
$configurable_product->save();
*/
//$associatedProductIds = array($simplProductId1,$simplProductId2,$simplProductId3,$simplProductId4);//Simple Product ids array
$associatedProductIds = array($simple_product_id);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($configProductId); // Load Configurable Product
$attributeModel = $objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute');
$position = 0;
$attributes = array(10);
//$attributes = array($attributeColorId, $attributeSizeId); // Super Attribute Ids Used To Create Configurable Product(list of supper attribute ids what ever belong to that the attribute set under which the configurable product is)
foreach ($attributes as $attributeId) {
$data = array('attribute_id' => $attributeId, 'product_id' => $configProductId, 'position' => $position);
$position++;
$attributeModel->setData($data);//->save();
}
$product->setTypeId("configurable");
$product->setAffectConfigurableProductAttributes($attributeSetId);
$objectManager->create('Magento\ConfigurableProduct\Model\Product\Type\Configurable')->setUsedProductAttributeIds($attributes, $product);
$product->setNewVariationsAttributeSetId($attributeSetId);
$product->setAssociatedProductIds($associatedProductIds);// Setting Associated Products
$product->setCanSaveConfigurableAttributes(true);
$product->save();
echo "configurable product id: ".$configurable_product->getId()."\n";
?>
I will suggest you to use magmi / magmi-m2 for insertion product from outside magento .This can reduce effort of you .See the configurables product insertion here.

How to Create Product Using Custom Module?

I want to create a product at the time of my custom module installation.how can i achieve that ?
this is my custom module setup script
$this->startSetup();
$product= new Mage_Catalog_Model_Product();
$product->setSku("basic-plan");
$product->setName("Basic Plan");
$product->setDescription("Description for the product");
$product->setShortDescription("Short Description for the product");
$product->setPrice(20);
$product->setTypeId('simple');
$product->setAttributeSetId(4); // enter the catalog attribute set id here
$product->setCategoryIds(array(16)); // id of categories
$product->setWeight(1.0);
$product->setTaxClassId(0); // taxable goods
$product->setVisibility(1); // catalog, search
$product->setStatus(1); // enabled
$product->setStoreIDs(array(1));
$product->setStockData(
array(
'is_in_stock' => 1,
'qty' => 1000,
'manage_stock' => 1
)
);
$product->setIsRecurring('1');
$product->setRecurringProfile(array(
'schedule_description' => 'Basic Plan',
'period_unit' => 'week',
'period_frequency' => 1
));
// assign product to the default website
//$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
$product->setWebsiteIds(array(1));
$product->save();
/*Subscription Plan Products Ends Here*/
$this->endSetup();
setup is running but i am not able to create product using this script
Hello Pls Try this Code
<?php
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product');
// if(!$product->getIdBySku('testsku61')):
$product
// ->setStoreId(1) //you can set data in store scope
->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array
->setAttributeSetId(9) //ID of a attribute set named 'default'
->setTypeId('simple') //product type
->setCreatedAt(strtotime('now')) //product creation time
// ->setUpdatedAt(strtotime('now')) //product update time
->setSku('testsku61') //SKU
->setName('test product21') //product name
->setWeight(4.0000)
->setStatus(1) //product status (1 - enabled, 2 - disabled)
->setTaxClassId(4) //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(24)
->setNewsFromDate('06/26/2014') //product set as new from
->setNewsToDate('06/30/2014') //product set as new to
->setCountryOfManufacture('AF') //country of manufacture (2-letter country code)
->setPrice(11.22) //price in form 11.22
->setCost(22.33) //price in form 11.22
->setSpecialPrice(00.44) //special price in form 11.22
->setSpecialFromDate('06/1/2014') //special price from (MM-DD-YYYY)
->setSpecialToDate('06/30/2014') //special price to (MM-DD-YYYY)
->setMsrpEnabled(1) //enable MAP
->setMsrpDisplayActualPriceType(1) //display actual price (1 - on gesture, 2 - in cart, 3 - before order confirmation, 4 - use config)
->setMsrp(99.99) //Manufacturer's Suggested Retail Price
->setMetaTitle('test meta title 2')
->setMetaKeyword('test meta keyword 2')
->setMetaDescription('test meta description 2')
->setDescription('This is a long description')
->setShortDescription('This is a short description')
->setMediaGallery (array('images'=>array (), 'values'=>array ())) //media gallery initialization
->addImageToMediaGallery('media/catalog/product/1/0/10243-1.png', array('image','thumbnail','small_image'), false, false) //assigning image, thumb and small image to media gallery
->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' => 999 //qty
)
)
->setCategoryIds(array(3, 10)); //assign product to categories
$product->save();
//endif;
}catch(Exception $e){
Mage::log($e->getMessage());
}

Adding magento product from frontend with attributes

How to add product from frontend with custom attributes ?
I have this code form another stack question
//$product = Mage::getModel('catalog/product');
$product = new Mage_Catalog_Model_Product();
echo time();
// Build the product
$product->setAttributeSetId(9);// #4 is for default
$product->setTypeId('simple');
$product->setName('Some cool product name');
$product->setDescription('Full description here');
$product->setShortDescription('Short description here');
$product->setSku(time());
$product->setWeight(4.0000);
$product->setStatus(1);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);//4
print_r(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$product->setPrice(39.99);// # Set some price
$product->setTaxClassId(0);// # default tax class
$product->setStockData(array(
'is_in_stock' => 1,
'qty' => 99999
));
$product->setCategoryIds(array(27));// # some cat id's,
$product->setWebsiteIDs(array(1));// # Website id, 1 is default
//Default Magento attribute
$product->setCreatedAt(strtotime('now'));
//print_r($product);
try {
$product->save();
echo "Product Created";
}
catch (Exception $ex) {
//Handle the error
echo "Product Creation Failed";
}
But i have custom attributes also , and how to add them from that code.
For each attribute you have you need to call:
$product->setData('attribute_code_here', 'Value here');
[Edit]
For yes/no attributes do it like this:
$product->setData('attribute_code_here', 1); //1 for Yes, 0 for No
For multiple selects
$product->setData('attribute_code_here', "4,6,12"); //the ids of the values concatenated by comma.
First add a product with all attributes per hand into your Magento, so that you can figure out how Magento uses them. Load that Product and print_r all variables, then use them to save a new product.
$_product = Mage::getModel('catalog/product')->load('PRODUCT ID');
Zend_Debug::dump($_product);
Get All the Attributes you need to save a new Product and do that:
$_product = Mage::getModel('catalog/product');
$_product->setYourAttribute('...');
$_product->save();

Creating Magento orders programmatically

Is there a way that an order can be created within Magento with a "custom" product which would be defined within the order. The order wouldn't require the custom products to be created. These custom products would have custom prices and a custom product title determined when creating the order.
So when creating order, I would just simply specify some custom products with the prices then add them to the order.
Again, the products will not be defined anywhere within Magento, so I would just say, I want to add xyz at 9.99 and zxy at 1.99 and maybe I want to add another xyz at 3.99.
The order would show as,
xyz | 9.99
zxy | 1.99
xyz | 3.99
TOTAL | 15.97
Programatically create an order with custom product is possible, here is an example code:
<?php
require_once 'app/Mage.php';
umask(0);
Mage::app('admin');
$order = create();
echo $order;
function create()
{
$storeId = 1;
if (!$storeId) {
$storeIds = Mage::app()->getWebsite($customer->getWebsiteId())->getStoreIds();
reset($storeIds);
$storeId = current($storeIds);
}
$order = Mage::getModel('sales/order')
->setState('new');
$orderPayment = Mage::getModel('sales/order_payment')
->setStoreId($storeId)
->setCustomerPaymentId(0)
->setMethod('checkmo')
->setPo_number(' - ');
$order->setPayment($orderPayment);
$billingAddress = Mage::getModel('sales/order_address');
$shippingAddress = Mage::getModel('sales/order_address');
$order->setStoreId($storeId)
->setQuoteId(0)
->setGlobal_currency_code('EUR')
->setBase_currency_code('EUR')
->setStore_currency_code('EUR')
->setOrder_currency_code('EUR')
->setStatus($orderData['status']);
// set Customer data
$order->setCustomer_email('a#b.com')
->setCustomerFirstname('firstname')
->setCustomerLastname('lastname')
->setCustomer_is_guest(1);
// set Billing Address
$billingAddress
->setStoreId($storeId)
->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING)
->setPrefix('mr')
->setFirstname('firstname')
->setLastname('lastname')
->setCompany('company')
->setStreet('street')
->setCity('city')
->setCountry_id('US')
->setPostcode('12345');
$order->setBillingAddress($billingAddress);
$shippingAddress
->setStoreId($storeId)
->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING)
->setPrefix('mr')
->setFirstname('firstname')
->setLastname('lastname')
->setCompany('company')
->setStreet('street')
->setCity('city')
->setCountry_id('US')
->setPostcode('12345');
$order->setShippingAddress($shippingAddress)
->setShipping_method('freeshipping_freeshipping')
->setShippingDescription('Free Shipping - Free');
$orderItem = Mage::getModel('sales/order_item')
->setStoreId(1)
->setQuoteItemId(0)
->setQuoteParentItemId(NULL)
->setQtyBackordered(NULL)
->setTotalQtyOrdered(10)
->setQtyOrdered(10)
->setName('custom product name')
->setPrice(100)
->setBasePrice(10)
->setOriginalPrice(10)
->setRowTotal(1000)
->setBaseRowTotal(1000);
$order->addItem($orderItem);
$order->setSubtotal(2000)
->setSubtotalIncludingTax(2000)
->setBaseSubtotal(2000)
->setGrandTotal(2000)
->setBaseGrandTotal(2000)
->setTaxAmount(0)
->setTotalQtyOrdered(10);
$order->save();
return $order;
}
?>

Update products programmatically in Magento

I'm working on a script that will create or update products in my catalog.
The script works fine when the product needs to be created, but it fails when the product already exists in the database giving me (many times) the following messages :
2011-09-30T08:00:53+00:00 ERR (3): Recoverable Error: Argument 3
passed to
Mage_Catalog_Model_Resource_Eav_Mysql4_Abstract::_canUpdateAttribute()
must be an array, null given, called in ...
2011-09-30T08:00:53+00:00
ERR (3): Recoverable Error: Argument 3 passed to
Mage_Eav_Model_Entity_Abstract::_canUpdateAttribute() must be an
array, null given, called in ...
2011-09-30T08:00:53+00:00 ERR (3):
Warning: array_key_exists() [function.array-key-exists]: The
second argument should be either an array or an object in ...
I've been looking at the method quoted in the message, but I can't find any reason why the script fails.
The script first try to load a product using :
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
and then test if the product was retrieved using a simple if(!$product) { //creation }.
All the code that follow the if statement is shared for creation or update and consists of setter calls on product object.
Here is the code I use :
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);
if(!$product) {
// the product doesn't exist yet
$product = new Mage_Catalog_Model_Product();
$product->setSku($sku);
$product->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE);
$product->setCreatedAt(strtotime('now'));
}
// setters calls
$product->setTeinte(trim((string)$record->web_teinte));
// ...
// finally save the product
$product->save();
Maybe someone has already faced the same problem.
Any help is welcome ! Thank you.
Chances are, in your "setter calls" you are trying to set something that cannot be directly set on $product. It could even be the "setTeinte" as I am not sure what that is trying to set. But as we cannot see all your code, it is a little difficult to say, so as I guide, take a look at the code below, which sets some information directly on the product and then stock levels. It does therefore, illustrate how certain data has to be set. I hope it helps.
$SKU = (string)$XMLproduct->Sku;
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$SKU);
if ($product) {
//Product found, so we need to update it in Magento.
$product->setName((string)$XMLproduct->Name);
$product->setPrice((real)$XMLproduct->SalePrice);
//$product->setDescription((string)$XMLproduct->LongDescription);
//$product->setShortDescription((string)$XMLproduct->Description);
$product->save();
$productId = $product->getId();
$stockItem =Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
$stockItemId = $stockItem->getId();
$stockItem->setData('manage_stock', 1);
$stockItem->setData('qty', (integer)$XMLproduct->QtyInStock);
$stockItem->save();
echo $SKU," Updated: Name: '",(string)$XMLproduct->Name,"', Price: ",(real)$XMLproduct->SalePrice,", Stock level: ",$XMLproduct->QtyInStock,PHP_EOL;
$updated++;
}
Adding Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); before saving product solves the error. The sample code below updates product's cost.
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$productId = 160;
$newCost = 80;
$product = Mage::getModel('catalog/product')->load($productId);
$product->setCost($newCost)->save();
//here what i use in codeigniter
function updateProducts(){
$params = array('name' => 'adminhtml'); // frontend or adminhtml
$this->load->library('magento', $params);
error_reporting(E_ALL | E_STRICT);
//$mageFilename = 'app/Mage.php';
//require_once $mageFilename;
Mage::setIsDeveloperMode(true);
umask(0);
Mage::app();
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$obj = new stdClass();
$obj->Sku = '25484684';
$obj->Name = 'test product 2';
$obj->SalePrice = 55;
$obj->LongDescription = 'test product long decription.test product long decription.test product long decription.';
$obj->Description = 'short descrption';
$res = $this->updateMagentoProduct($obj);
//dumb($res);
}
function updateMagentoProduct($XMLproduct){
$SKU = (string)$XMLproduct->Sku;
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$SKU);
if (!$product) {//insert new product
$product = Mage::getModel('catalog/product');
$product->setSku($SKU);
}
//$product = new Mage_Catalog_Model_Product();
// Build the product
$product->setAttributeSetId(4); // 4 means Default AttributeSet
$product->setTypeId('simple');
$product->setName((string)$XMLproduct->Name);
$product->setCategoryIds(array(2,3,4,5,6,7));
$product->setWebsiteIDs(array(1)); # Website id, 1 is default
//$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
//$product->setWebsiteIDs(array(1)); # Website id, my is 1 (default frontend)
$product->setDescription((string)$XMLproduct->LongDescription);
$product->setShortDescription((string)$XMLproduct->Description);
$product->setPrice((real)$XMLproduct->SalePrice);
# Custom created and assigned attributes
//$product->setHeight('my_custom_attribute1_val');
//$product->setWidth('my_custom_attribute2_val');
//$product->setDepth('my_custom_attribute3_val');
//$product->setType('my_custom_attribute4_val');
//Default Magento attribute
$product->setWeight(1.0);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$product->setStatus(1);
$product->setTaxClassId(0); # My default tax class
/*$product->setStockData(array(
'is_in_stock' => 1,
'qty' => 99999
));*/
$product->setCreatedAt(strtotime('now'));
try {
$product->save();
$productId = $product->getId();
$stockItem =Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
$stockItemId = $stockItem->getId();
$stockItem->setData('manage_stock', 1);
$stockItem->setData('qty', 99999);//(integer)$XMLproduct->QtyInStock
$stockItem->save();
echo '<h5>'.$SKU," Updated: Name: '",(string)$XMLproduct->Name,"', Price: ",(real)$XMLproduct->SalePrice,", Stock level: ",PHP_EOL.'</h5>';
}
catch (Exception $ex) {
//Handle the error
echo '<h5>'.$ex->getMessage().'</h5>';
}
// assign product to the default website
return $product->save();
}
Easy with Magento API,
also can use methods....
example
$data = array('qty'=>1, 'is_in_stock'=>1)
$stockModel = new Mage_CatalogInventory_Model_Stock_Item_Api;
$stockModel->update($product_id, $data);
Also can set Admin mode
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
<?php
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product');
$product
// ->setStoreId(1) //you can set data in store scope
->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array
->setAttributeSetId(9) //ID of a attribute set named 'default'
->setTypeId('simple') //product type
->setCreatedAt(strtotime('now')) //product creation time
// ->setUpdatedAt(strtotime('now')) //product update time
->setSku('testsku61') //SKU
->setName('test product21') //product name
->setWeight(4.0000)
->setStatus(1) //product status (1 - enabled, 2 - disabled)
->setTaxClassId(4) //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(24)
->setNewsFromDate('06/26/2014') //product set as new from
->setNewsToDate('06/30/2014') //product set as new to
->setCountryOfManufacture('AF') //country of manufacture (2-letter country code)
->setPrice(11.22) //price in form 11.22
->setCost(22.33) //price in form 11.22
->setSpecialPrice(00.44) //special price in form 11.22
->setSpecialFromDate('06/1/2014') //special price from (MM-DD-YYYY)
->setSpecialToDate('06/30/2014') //special price to (MM-DD-YYYY)
->setMsrpEnabled(1) //enable MAP
->setMsrpDisplayActualPriceType(1) //display actual price (1 - on gesture, 2 - in cart, 3 - before order confirmation, 4 - use config)
->setMsrp(99.99) //Manufacturer's Suggested Retail Price
->setMetaTitle('test meta title 2')
->setMetaKeyword('test meta keyword 2')
->setMetaDescription('test meta description 2')
->setDescription('This is a long description')
->setShortDescription('This is a short description')
->setMediaGallery (array('images'=>array (), 'values'=>array ())) //media gallery initialization
->addImageToMediaGallery('media/catalog/product/1/0/10243-1.png', array('image','thumbnail','small_image'), false, false) //assigning image, thumb and small image to media gallery
->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' => 999 //qty
)
)
->setCategoryIds(array(3, 10)); //assign product to categories
$product->save();

Resources