Skip to content

Instantly share code, notes, and snippets.

@antoinekociuba
Last active October 20, 2015 12:38
Show Gist options
  • Save antoinekociuba/f7c7531659d3c7a00c74 to your computer and use it in GitHub Desktop.
Save antoinekociuba/f7c7531659d3c7a00c74 to your computer and use it in GitHub Desktop.
Magento CMS Blocks installer script (useful to deploy safely).
<?php
/**
* CMS Blocks Installer
*/
try {
/* @var $installer Mage_Core_Model_Resource_Setup */
$installer = $this;
$block1Content = <<<HTML
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus amet asperiores aut, deserunt, eius, eligendi enim explicabo fugit minima mollitia nam necessitatibus obcaecati officia quasi qui quod tempore ut vero!
HTML;
$block2Content = <<<HTML
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus amet asperiores aut, deserunt, eius, eligendi enim explicabo fugit minima mollitia nam necessitatibus obcaecati officia quasi qui quod tempore ut vero!
HTML;
$cmsBlocksData = array(
array(
'title' => 'Lorem Lorem 1',
'identifier' => 'lorem1', // Must be unique
'content' => $block1Content,
'is_active' => true,
'stores' => array(Mage_Core_Model_App::ADMIN_STORE_ID)
),
array(
'title' => 'Lorem Lorem 2',
'identifier' => 'lorem2', // Must be unique
'content' => $block2Content,
'is_active' => true,
'stores' => array(Mage_Core_Model_App::ADMIN_STORE_ID)
)
);
/* @var $cmsBlockModel Mage_Cms_Model_Block */
$cmsBlockModel = Mage::getModel('cms/block');
foreach ($cmsBlocksData as $data) {
$cmsBlock = clone $cmsBlockModel;
// Load block, for appropriate store
$cmsBlock->setStoreId($data['stores'][0])
->load($data['identifier'], 'identifier');
// Create CMS block if it doesn't exist
if (!$cmsBlock->getId()) {
$cmsBlock->setData($data);
// Otherwise, we update it
} else {
// Specific data update
$cmsBlock->addData(array(
'content' => $data['content']
));
}
$cmsBlock->save();
}
} catch (Exception $e) {
Mage::logException($e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment