Magento 2.x. Update/add cms static block programmatically.
To update cms static block for particular store programmatically you can use following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $identifier = 'block_identifier'; $store_id = 2; try { $block = $objectManager->create('Magento\Cms\Model\Block'); $block->setStoreId($store_id); // store for block you want to update $block->load($identifier, 'identifier'); $block->setIdentifier($identifier); $block->setTitle('Block Title'); $block->setIsActive(1); $block->setStores($store_id); $block->setContent($content); $block->save(); echo "Static block updated! \n"; } catch (Exception $e) { echo $e->getMessage(); } |