Magento - CMS Block/Page installer

Saturday 21 September 2013

You can manually create CMS Pages in Magento from CMS -> Pages. Similarly, you can create Static Blocks from CMS -> Static Blocks.
This article will show how to create and update CMS pages and Static blocks programmatically from code for single store websites and multi store websites. Also it will resolve your issue "A block identifier with the same properties already exists in the selected store" when you try to update a cms block.
1.Single store websites
  • Create/update CMS Pages
$storeId=0 // put your current store id here
$cmsPage array(
            'title' => 'CMS SAMPLE PAGE',
            'identifier' => 'test_url',
            'content' => 'cms page content',            
            'is_active' => 1,
            'sort_order' => 0,
            'stores' => array($storeId),
            'root_template' => 'three_columns'
            );
             
$page = Mage::getModel('cms/page')->load($cmsPage ['identifier']);
        if ($page->getId()) {
            $page->setContent($cmsPage ['content']);
           $page->setTitle($cmsPage ['title']);
           $page->save();
        } else {
            Mage::getModel('cms/page')->setData($cmsPage )->save();
        }

  • Create/update Static Blocks


$storeId=0 // put your current store id here
$staticBlock array(
                'title' => 'Test CMS Block',
                'identifier' => 'sample_block',                   
                'content' => 'cms block content',
                'is_active' => 1,                   
                'stores' => array($storeId)
                );
                 
$block= Mage::getModel('cms/block')->load($staticBlock ['identifier']);
        if ($block->getId()) { // resolving issue " A block identifier with the same properties                                                         //already exists in the selected store "            $block->setContent($staticBlock ['content']);
           $block->setTitle($staticBlock ['title']);
           $block->save();
        } else {
            Mage::getModel('cms/block')->setData($staticBlock )->save();
        }
1.Multi store websites
In muti store websites there will be multiple cms blocks and cms pages with same identifier. So when we fetch cms blocks/pages using identifier  it may load wrong block and should cause issues on updates.

We can use a custom function for resolving this issue,
/**
common function for creating and updating cms pages and blocks in multi store websites
*/
function saveCmsData($data, $storeId, $isPage = false) {
        $store = array($storeId);
        $store[] = Mage_Core_Model_App::ADMIN_STORE_ID;
        if ($isPage) {
            $model = Mage::getModel('cms/page');
        } else {
            $model = Mage::getModel('cms/block');
        }
        $collection = $model->getCollection()
                ->addFieldToFilter('identifier', $data['identifier'])
                ->addStoreFilter($storeId)
                ->addFieldToFilter('store_id', array('in' => $store));
        $cmsItem = $collection->getFirstItem();
        if ($cmsItem && ($cmsItem->getBlockId()||$cmsItem->getPageId())) {
            $cmsItem->load();
            $oldData = $cmsItem->getData();
            $data = array_merge($oldData, $data);
            $cmsItem->setData($data)->save();
        } else {
            $model->setData($data)->save();
        }
        return;
    }
  • Create/update CMS Pages
$storeId=1 // put your current store id here
$cmsPage array(
            'title' => 'CMS SAMPLE PAGE',
            'identifier' => 'test_url',
            'content' => 'cms page content',            
            'is_active' => 1,
            'sort_order' => 0,
            'stores' => array($storeId),
            'root_template' => 'three_columns'
            );
saveCmsData($cmsPage , $storeId, true);
  • Create/update CMS Blocks
$storeId=1 // put your current store id here
$staticBlock array(
                'title' => 'Test CMS Block',
                'identifier' => 'sample_block',                   
                'content' => 'cms block content',
                'is_active' => 1,                   
                'stores' => array($storeId)
                );saveCmsData($staticBlock, $storeId, false);


Hope this helps. Thanks.:)

4 comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. sorry for ask but i'm new on this, where should i put this code?

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Where to put the installer, and how to make it to work.

    ReplyDelete

 

Contact Form

Name

Email *

Message *

Most Reading