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
$storeId=0 // put your current store id here
'title' => 'CMS SAMPLE PAGE' , |
'identifier' => 'test_url' , |
'content' => 'cms page content' , |
'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
'title' => 'Test CMS Block' , |
'identifier' => 'sample_block' , |
'content' => 'cms block content' , |
'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;
}
$storeId=1 // put your current store id here
'title' => 'CMS SAMPLE PAGE' , |
'identifier' => 'test_url' , |
'content' => 'cms page content' , |
'stores' => array ( $storeId), |
'root_template' => 'three_columns' |
saveCmsData($cmsPage
, $storeId, true);
$storeId=1 // put your current store id here
'title' => 'Test CMS Block' , |
'identifier' => 'sample_block' , |
'content' => 'cms block content' , |
'stores' => array ( $storeId) |
); saveCmsData($staticBlock, $storeId, false); |
Hope this helps. Thanks.:)
This comment has been removed by the author.
ReplyDeletesorry for ask but i'm new on this, where should i put this code?
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteWhere to put the installer, and how to make it to work.
ReplyDelete