Magento Update CMS Pages and Blocks Programmatically

magento-1.8

I need to update the store view of all my CMS Pages and Static Blocks programmatically, but I have been unable to find any solution for the same. Is there any option available through code, or should I do the same using MySQL.

Best Answer

Create the file data/[module]_setup/data-upgrade-1.0.4-1.0.5.php with the following content:

Edit: changed file name

content = 'BLOCK CONTENT HERE';
//if you want one block for each store view, get the store collection
$stores = Mage::getModel('core/store')->getCollection()->addFieldToFilter('store_id', array('gt'=>0))->getAllIds();
//if you want one general block for all the store viwes, uncomment the line below
//$stores = array(0);
foreach ($stores as $store){
    $block = Mage::getModel('cms/block');
    $block->setTitle('Block title here');
    $block->setIdentifier('block_identifier_here');
    $block->setStores(array($store));
    $block->setIsActive(1);
    $block->setContent($content);
    $block->save();
}

After this just change the version in config.xml to 1.0.5 clear the cache and refresh any page.

Related Topic