Magento - programmatically save static block content
-
14-06-2021 - |
Question
I have a static block that I would like updating by a script which gets run via cron.
I've found out how to create or retrieve a block programmatically, but not how to edit an existing one.
This works to retrieve a block:
// Retrieve the layout object
$layout = Mage::getSingleton('core/layout');
// Generate a CMS block object
$block = $layout->createBlock('cms/block');
// Set the block ID of the static block
$block->setBlockId('my_block_id');
// Write the static block content to screen
echo $block->toHtml();
I think I'm missing something simple here, but doing setContent() and then save() on this block just results in "Invalid method Mage_Cms_Block_Block::save"
La solution
By block id:
Mage::getModel('cms/block')->load($id)
->setData('content', 'Example content')
->save();
By identifier:
Mage::getModel('cms/block')
->getCollection()
->addFieldToFilter('identifier', 'my_block_id')
->load()
->setData('content', 'Example content')
->save();
Autres conseils
$identifier = 'footer_links';
Mage::getModel('cms/block')
->load($identifier, 'identifier')
->setData('content', 'Your new block content')
->save()
;
Or if you know the block id:
$id = 1;
Mage::getModel('cms/block')
->load($id)
->setData('content', 'Your new block content')
->save()
;
Update and add static block using magento scripts
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();
}
please refer my blog for step by step explanation
http://www.pearlbells.co.uk/how-to-create-and-update-the-static-blocks-using-magento-script/
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow