1

I want to add a static block programmatically. The way I'm trying to do that is next:

  • I created file data-upgrade-1.0.0-1.0.1.php (there were none other files except data-install...) inside setup directory app\code\local\[theme]\Customizations\data\[theme]​_customizations_setup
  • Added this code:

    $stores = Mage::getModel('core/store')->getCollection()
        ->addFieldToFilter('store_id', array('gt' => 0))->getAllIds();
    
    foreach ($stores as $storeId) {
        $block = Mage::getModel('cms/block')
            ->setIsActive(1)
            ->setTitle('Test CMS Static Block')
            ->setIdentifier('cms_block_test')
            ->setContent('<!-- edit me -->')
            ->setStores(array($storeId))
            ->save();
    }
    

  • After that, I changed config.xml version 1.0.1
  • Cleared all Magento cache

But my static block won't show under CMS > Static Blocks.
Am I missing something?

3
  • Did the data-install-1.0.0.php run in the first place?
    – mam08ixo
    Mar 6, 2015 at 13:07
  • I'm not sure what you think by that. Is there a way to run it? Would it overwrite any data if I do run it?
    – mihajlo
    Mar 6, 2015 at 13:12
  • Question rephrased: Did the other data script(s) run successfully in the past? If not, this is not an issue with the current script but with your module setup. Also check if your module's resource identifier shows up in core_resource table.
    – mam08ixo
    Mar 6, 2015 at 13:58

1 Answer 1

1

All Cms blocks are linked to one or more stores. Perhaps setting the store ID's will help:

->setStores(array($storeId))

Normally I build the code like this in my install/upgrade scripts:

$stores = Mage::getModel('core/store')->getCollection()
    ->addFieldToFilter('store_id', array('gt' => 0))->getAllIds();

foreach ($stores as $storeId) {
    // Block creation code goes here
}

Was the block even added to the database? If it was, try to find the differences between the records to a block that you have added through the admin interface.

If not, then your script has probably not even run. You could add a log line somewhere (Mage::log('Data upgrade @ ' . __FILE__)) and see if it has appeared in your var/log/system.log.

Also check if the version of your module isn't yet at "1.0.1" by checking the version in the core_resource table.

If the version in the core_resource table is "1.0.1" or higher you could do two things

  • Update the record in the core_resource table to "1.0.0" version (don't know what the side effects might be)
  • Update your script and the config.xml to be version "1.0.3" so that Magento sees a newer version of the module and kicks off the upgrade
14
  • Updated the code, still nothing.
    – mihajlo
    Mar 6, 2015 at 12:01
  • Updated the answer with some things you could check/do
    – 7ochem
    Mar 6, 2015 at 14:07
  • I have no access to Magento DB so I can't check any tables. Will check for logs.
    – mihajlo
    Mar 6, 2015 at 14:29
  • Logs are out of the question too :/. For some reason, I can't download them to check them out. FileZilla throws error. Any other way maybe?
    – mihajlo
    Mar 6, 2015 at 14:50
  • 1
    @7ochem - we changed [theme]_customizations_setup in the database, and it worked :). Thanks everybody.
    – mihajlo
    Mar 9, 2015 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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