I tried to create a product with the following code on Magento 1.5.1. The product has been saved to the database but is not displayed in the backend product grid. If I call the URL of the product view, some fields are not populated, for example: Name, Description (see: NOK).
$product = Mage::getModel('catalog/product');
$product->setSku("ABC123")
->setName("Type 7 Widget")
->setDescription("This widget will give you years of trouble-free widgeting.")
->setShortDescription("High-end widget.")
->setPrice(70.50)
->setTypeId('simple')
->setAttributeSetId('14')
->setCategoryIds("3,7")
->setWeight(1.0)
->setTaxClassId(1)
->setVisibility(4)
->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
$product->save();
This is a product stored in the database. print_r($product->debug());
[type_id] => simple
[sku] => ABC123
[has_options] => 0
[required_options] => 0
[created_at] => 2012-04-26 12:57:22
[updated_at] => 2012-04-26 12:57:22
[weight] => 1
[url_path] => .html
[price] => 70.5000
[enable_googlecheckout] => 1
[name] => Type 7 Widget
[meta_title] => Type 7 Widget
[meta_description] => Type 7 Widget, Fotos
[url_key] => type-7-widget
[options_container] => container2
[delivery_time] => 2-3 Tage
[generate_meta] => 0
[status] => 1
[tax_class_id] => 1
[visibility] => 4
[description] => This widget will give you years of trouble-free widgeting.
[short_description] => High-end widget.
[meta_keyword] => Type 7 Widget, Fotos
[media_gallery] => Array
(
[images] => Array
(
)
[values] => Array
(
)
)
[tier_price] => Array
(
)
[tier_price_changed] => 0
[stock_item (Mage_CatalogInventory_Model_Stock_Item)] => Array
(
[product_id] => 158
[product_name] => Type 7 Widget
[store_id] => 1
[product_type_id] => simple
[product_status_changed] => 1
)
[is_in_stock] => 1
[is_salable] => 1
)
Any help would be great!
Decision
Specify the store where you want to store the products with the following code:
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
source
share