4

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

5

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.

  1. Calling Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); has no effect in the install scripts.
  2. You should not create products with the scripts from the sql folder. Use the data folder for that. I don't where you placed the code, but in case you put it in the sql folder move it to data/{resource_name_setup}/data-install-1.0.0.php (change the version according to your needs).

Now the script that worked for me.

$websiteIds = Mage::getModel('core/website')->getCollection()
    ->addFieldToFilter('website_id', array('neq'=>0))
    ->getAllIds();

$product = Mage::getModel('catalog/product');
$product->setStoreId(0); //use this instead of Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product->setWebsiteIds($websiteIds);
$product->setTypeId('virtual');
$product->addData(array(
    'name' => 'Product name here',
    'attribute_set_id' => $product->getDefaultAttributeSetId(), //use the default attribute set or an other id if needed.
    'status' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED, //set product as enabled
    'visibility' => Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH, //set visibility in catalog and search
    'meta_title' => 'Meta title here',
    'weight' => 1,
    'sku' => 'sku-here',
    'tax_class_id' => 2, //could not find a non-hardcoded value for this
    'description' => 'Description here',
    'short_description' => 'Short description here',
    'stock_data' => array( //set stock data
        'manage_stock' => 1,
        'qty' => 999, //set the qty
        'use_config_manage_stock' => 1,
        'use_config_min_sale_qty' => 1,
        'use_config_max_sale_qty' => 1,
        'use_config_enable_qty_increments' => 1,
        'in_stock' => 1
    ),

));
$product->save();

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.

  • thanks. I added the following code to customise the attribute set: $attributeSetName = "Credits"; $entityTypeId = Mage::getModel('eav/entity') ->setType('catalog_product') ->getTypeId(); $attributeSetId = Mage::getModel('eav/entity_attribute_set') ->getCollection() ->setEntityTypeFilter($entityTypeId) ->addFieldToFilter('attribute_set_name', $attributeSetName) ->getFirstItem() ->getAttributeSetId(); – Antonio Pedicini Nov 10 '14 at 17:11
  • Same problem, solved moving the upgradescript from /sql/ to /data/. – Dr. Gianluigi Zane Zanettini May 22 '17 at 11:00
2

I guess in magento 1.9 doing this

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

do not work reason is Update process run flag is set to 1 during installation script so creating category or product from installation script lead to problem like if you are running multiple site and want to create category in all store view using installation script it fails and create category/product for default store. To avoid this do as following

$currentStore = Mage::app()->getStore()->getId();
<b>Mage::app()->setUpdateMode(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

You code to Do what you Want

Mage::app()->getStore()->setId($currentStore);
Mage::app()->setUpdateMode(1);<br/>

Hope this help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.