I have created one CMS static block.
I want to know that can I change its content by code (programmatically).
Is there any way to do it?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityUse this
Load by block id:
Mage::getModel('cms/block')->load($id)
->setData('content', 'SET CONTENT HERE')
->save();
Load By identifier:
Mage::getModel('cms/block')
->getCollection()
->addFieldToFilter('identifier', 'block_identifier')
->load()
->setData('content', 'Example content')
->save();
setData
on a collection will not work. You should squeeze in a call to getFirstItem
: Mage::getModel('cms/block') ->getCollection() ->addFieldToFilter('identifier', 'block_identifier') ->load() ->getFirstItem() ->setData('content', 'Example content') ->save();
This will work in most cases, but when there are multiple blocks with the same identifier and you want to update all of them you should process all items in the filtered collection.
Nov 15, 2019 at 21:01
Using below code you can load cms block using identifier
Mage::getModel('cms/block')->load('{BLOCK_IDENTIFIER}')->setContent('your-content')->save();