I'm trying to add a product using an upgrade script inside my module, but I have this error:
Fatal error: Call to a member function getStoreIds() on a non-object
Here is my code:
$installer = $this;
$installer->startSetup();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
try {
$product = Mage::getModel('catalog/product');
$product->setSku("credit10");
$product->setName("12€ Credit");
$product->setDescription("12€ credito");
$product->setShortDescription("12€ credito");
$product->setPrice(10.00);
$product->setTypeId('virtual');
$product->setAttributeSetId(4); // need to look this up
$product->setCategoryIds("2"); // need to look these up
$product->setWeight(1.0);
$product->setTaxClassId(2); // taxable goods
$product->setVisibility(4); // catalog, search
$product->setStatus(1); // enabled
// assign product to the default website
$product->setWebsiteIds(array(1));
$product->setStoreId(1);
$product->save();
}catch(Exception $e) {
Mage::log($e->getMessage());
}
$installer->endSetup();
What I'm wrong?
thanks
Best Answer
I don't know exactly what the problem might be for you, but here is a script that worked for me.
But first a remark or 2.
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
has no effect in the install scripts.data
folder for that. I don't where you placed the code, but in case you put it in thesql
folder move it todata/{resource_name_setup}/data-install-1.0.0.php
(change the version according to your needs).Now the script that worked for me.
Also don'w wrap your install script in
$installer->startSetup()
and$installer->endSetup()
.startSetup
disabled the foreign keys, and you don't want that while adding a product.