Create ,update static blocks magento script

This tutorial explains how to create and update static blocks using the magento scripts.For that I have create a file in the magento root folder. Then I have created an array which contains the details of the static block as shown below.

If we are creating the CMS blocks using the script it is easy to move the blocks from the develop database to live database without redoing those changes in the magento backend.

static-block-using-magento-script

Step by Step explaination

  1. Establish the database connection
  2. Create the block array
  3. Create Static block
  4. Update static block

Establish the database connection

For creating the static blocks first we need to create the database connection.

<?php
require_once '../app/Mage.php';

ini_set('display_errors', 1);
Mage::app('admin');
Mage::setIsDeveloperMode(true);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$storeId = Mage::app()->getWebsite()->getDefaultGroup()->getDefaultStoreId();

Create the block array

As the next step we need to create an array of blocks and iterate through the array for creating the individual blocks

$blocks = array(
 
    array(
       'title' => 'Get in touch',
       'identifier' => 'get_in_touch',
       'storeId' => $storeId,
       'active' => 1,
       'content' => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s,'
       ),
    array(
        'title' => 'Subcategories',
        'identifier' => 'subcategories',
        'storeId' => $storeId,
        'active' => 1,
        'content' => '{{block type="core/template" template="catalog/category/sub.phtml"}}',
        ),
    array(
        'title' => 'Slideshow',
        'identifier' => 'slideshow',
        'storeId' => $storeId,
        'active' => 1,
        'content' => '<ul>
                    <li><img alt="" src="{{media url="wysiwyg/slideshow.jpg"}}" /></li>
                    </ul>'
        )
);

foreach($blocks as $block) {
    createBlock($block);
}

Create Static block

For create the static block we need to set the title,content and identifier as shown below.

function createBlock($blockData) {

   $block = Mage::getModel('cms/block')->load($blockData['identifier']);
    $block->setTitle($blockData['title']);
    $block->setIdentifier($blockData['identifier']);
    $block->setStores(array($blockData['storeId']));
    $block->setIsActive($blockData['active']);
    $block->setContent($blockData['content']);
    $block->save();
}

Update static block

For updating the static block using same magento script please use the same identifier.

function createBlock($blockData) {

    $block = Mage::getModel('cms/block')->load($blockData['identifier']);
    if ($block->isObjectNew()) {
        $block->setIdentifier($blockData['identifier']);
    }
    $block->setTitle($blockData['title']);
    $block->setStores(array($blockData['storeId']));
    $block->setIsActive($blockData['active']);
    $block->setContent($blockData['content']);
    $block->save();
}

If you have any further queries please leave a reply.

Thanks

Share this Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *

*