I am just trying to add simple product pro grammatically in magento,and i tried the below code
$product = new Mage_Catalog_Model_Product();
// Build the product
$product->setSku(8000);
$product->setAttributeSetId('some_int_value_of_some_attribute');
$product->setTypeId('simple'); $product->setName('order1');
$product->setCategoryIds(array(4)); # some cat id's, my is 7
$product->setWebsiteIDs(array(1)); # Website id, my is 1 (default frontend)
$product->setDescription('Full description here');
$product->setShortDescription('Short description here');
$product->setPrice(39.99); # Set some price
# Custom created and assigned attributes
$product->setHeight('500');
$product->setWidth('500');
$product->setDepth('2');
$product->setType('simple');
//Default Magento attribute
$product->setWeight(4.0000); $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' => 99 )); $product->setCreatedAt(strtotime('now')); try {
$product->save(); } catch (Exception $ex) {
//Handle the error }
try{
$product_model = Mage::getSingleton('catalog/product');
// Load product
$_sku = "8000";
$my_product_id = $product_model->getIdBySku($_sku);
$my_product = $product_model->load(43);
$qty_value = 1;
// Add to cart
$cart = Mage::getModel('checkout/cart');
$cart->init();
$cart->addProduct($my_product, array('qty' => $qty_value));
$cart->save();
print_r($cart->getItemsQty().PHP_EOL);
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
var_dump("working"); } catch(Exception $e){
return $e->getMessage(); }
i am using magento version 1.9.0.1
but i am getting blank page without any error and product is not added in backed.
so, Can anybody help me to find easiest way to create simple product programmatically.
Product save depending on Attribute and store.
First, check how many attribute are required for products
Second,You need to set those attribute value to products
Third,
set store
$product->setStore($storeId);
If you are doing this code using script then add below code before start of product create code
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
More details:-
http://www.codetweet.com/magento/create-simple-product-magento-programmatically/