I would like to add CMS block to a category programmatically, how to achieve it?

  • 1
    This post appears to be off-topic as per Questions asking for code must demonstrate a minimal understanding of the problem being solved. Please include your expected results, your attempted solutions, and why they didn't work. Please see the Stack Overflow question checklist for more details. Mar 14, 2019 at 14:00
  • Hello teja, as per following solution landing_page is cms block identifier right?
    – zus
    Jan 29, 2021 at 3:48

1 Answer 1


In order to add a cms block to the Category page, first, you already have cms static block.

If you want to add CMs block to the category then set have to set cms block identifier to of category and display mode of category page should be Static block only PAGE and Static block and products PRODUCTS_AND_PAGE.

Sample code


namespace StackExchange\Magento\Test;

class CmsBlock {

     * @var \Magento\Catalog\Model\CategoryFactory
    private $categroyFactory;

    public function __construct(
     \Magento\Catalog\Model\CategoryFactory $categroyFactory
    ) {

        $this->categroyFactory = $categroyFactory;

 public function saveCMSBlockToCategory()
     $catId = 10;
        $storeId = null; // You can put your store id  here
        $category = $this->categroyFactory->create();
        if (null !== $storeId) {


Not using Service contact class \Magento\Catalog\Api\CategoryRepositoryInterface as on save method its has magento bug.

Bug link: https://github.com/magento/magento2/issues/10088


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.