Skip to content

Instantly share code, notes, and snippets.

@molotovbliss
Last active April 21, 2016 08:22
Show Gist options
  • Save molotovbliss/40e78e1441553318ada95dda89598ae8 to your computer and use it in GitHub Desktop.
Save molotovbliss/40e78e1441553318ada95dda89598ae8 to your computer and use it in GitHub Desktop.
Create CMS Blocks in Magento.
<?php
// Define your data here:
$blockId = "customblock_id";
$blockTitle = " CMS BLOCK TITLE ";
// Using <<<HTML allows for easy copy/pasta of HTML data
$content = <<<HTML
<a href="#">Test</a>
HTML;
// Call function below
createCmsBlock($blockId, $blockTitle, $content);
function createCmsBlock($blockId, $blockTitle, $content, $storeId = 1, $allStores = false) {
$block = Mage::getModel('cms/block');
$block->load($blockId);
if($allStores) {
$stores = Mage::getModel('core/store')->getCollection()->addFieldToFilter('store_id', array('gt'=>0))->getAllIds();
} else {
if(!$block->getBlockId()) {
$store = $storeId;
foreach ($stores as $store){
$block->setTitle($blockTitle);
$block->setIdentifier($blockId);
$block->setStores(array($store));
$block->setIsActive(1);
$block->setContent($content);
try {
$block->save();
} catch (Exception $e) {
mage::logException($e);
zend_debug::dump($e);
}
}
} else {
$error = __FILE__." ".__LINE__." block exists: ".$blockId;
mage::log($error);
zend_debug::dump($error);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment