How to set store id to cms block output

How to set store id to cms block output

August 28, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to set store id to cms block output

Displaying CMS Blocks Store-Wise in Magento

In Magento, you can create static blocks and assign them to specific stores for targeted content display. However, fetching these store-specific blocks programmatically might require some additional configuration. Let’s look at how you can set the store ID to display CMS blocks output based on the current store.

Understanding the Issue with Current Code

The provided code is a general approach to fetch a static block by its identifier. It sets the block ID and store ID, which should theoretically work:

Mage::app()->getLayout()
    ->createBlock('cms/block')
    ->setBlockId('pdf_page_of')
    ->setStoreId($storeId)
    ->toHtml();

However, this method might not be sufficient because it doesn’t take into account the Magento’s built-in multi-store functionality. When you set a store ID manually like this, it bypasses the current context.

The Correct Approach: Using Magentos Built-In Helper

To ensure that the block is fetched correctly based on the current store context, use Magento’s Mage::helper('cms')->getBlockHtml method:

$block = Mage::helper('cms')->getBlockHtml($blockId);

Here’s how you can implement this in a layout file or a custom helper:

  1. First, ensure that your block identifier is unique across all stores.

  2. In your module layout configuration (e.g., config.xml), declare the block like so:

<blocks>
  <cms>
    <block type="core/text_list" name="pdf_page_of"/>
  </cms>
</blocks>
  1. Then, in your template file or custom helper, fetch the block content using the helper method:
$blockId = 'pdf_page_of'; // replace with your block ID
$storeId = Mage::app()->getStore()->getId();
$blockContent = Mage::helper('cms')->getBlockHtml($blockId);

echo $blockContent;

Ensuring Store-Wise Block Display

For the store-wise display to work correctly, ensure that:

  • Each block has a unique identifier.
  • The blocks are assigned properly to the desired stores in the Magento admin interface.
  • You’re fetching the block content within the correct context (like in a layout XML or a controller action).

Conclusion: Displaying Store-Specific CMS Blocks

In summary, to display static blocks store-wise in Magento, you should:

  1. Ensure each block has a unique identifier across all stores.
  2. Use Magento’s built-in Mage::helper('cms')->getBlockHtml method instead of setting the store ID manually.
  3. Fetch the block content within the correct context (like layout XML or controller actions).

This approach respects the multi-store architecture and ensures that the appropriate CMS block is displayed based on the current store context.

For more advanced customizations related to blocks, controllers, and Magento development best practices, I recommend exploring our comprehensive guides at Laravel Company.

Extensions Themes Configuration Customers

Our network