Magento 2 add static Block

To update cms static block for particular store programmatically you can use following code

  1. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  2. $identifier = ‘block_identifier’;
  3. $store_id = 2;
  4. try {
  5. $block = $objectManager>create(‘Magento\Cms\Model\Block’);
  6. $block>setStoreId($store_id); // store for block you want to update
  7. $block>load($identifier, ‘identifier’);
  8. $block>setIdentifier($identifier);
  9. $block>setTitle(‘Block Title’);
  10. $block>setIsActive(1);
  11. $block>setStores($store_id);
  12. $block>setContent($content);
  13. $block>save();
  14. echo “Static block updated! \n”;
  15. } catch (Exception $e) {
  16. echo $e>getMessage();
  17. }

Leave a Comment