How to create dynamic static blocks for multiple store views in Magento 1.x

If you ever wanted to create dynamic static blocks for multiple store views in Magento e-commerce platform here is the quick help.

Step 1

  1. Create your static blocks in magento.
  2. Login to your Magento admin panel
  3. Select CMS menu
  4. Click on Static Blocks
  5. Click on Create New Static Block and then compose your content.

Step 2 

  1. Open your folder structure in Magento root directory
  2. Locate this directory , /app/design/frontend/YOUR-THEME-FOLDER/template/page
  3. Create phtml file name as, yourphtml.phtml
  4. Copy paste following code.
  5. Change $storecode ($storeCode==”austria”) according your settings. (Please note that you can find your store codes from, Login to Magento Admin->System->Manage Store>Click on your store view and copy and paste store view code)
  6. Copy and paste your static block ids setBlockId(‘your-static-block-id-austria’)

<!--Adding dynamic static block for multiple store view-->
<?php
//GET CURRENT SELECTED STORE CODE
$storeCode = Mage::app()->getStore()->getCode();
?>
<?php if ($storeCode=="austria"): { ?>
<!--Austrian Store-->
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('your-static-block-id-austria')->toHtml() ?>
<!--End Austrian Store-->
<?php } endif; ?>
<?php if ($storeCode=="canadian"): { ?>
<!--Canadian Store-->
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('your-static-block-id-canadian')->toHtml() ?>
<!--End Canadian Store-->
<?php } endif; ?>
<?php if ($storeCode=="uk"): { ?>
<!--English Store-->
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('your-static-block-id-uk')->toHtml() ?>
<!--End English Store-->
<?php } endif; ?>
<?php if ($storeCode=="german"): { ?>
<!--German Store-->
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('your-static-block-id-german')->toHtml() ?>
<!--End German Store-->
<?php } endif; ?>
<?php if ($storeCode=="ireland"): { ?>
<!--Ireland Store-->
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('your-static-block-id-ireland')->toHtml() ?>
<!--End Ireland Store-->
<?php } endif; ?>
<?php if ($storeCode=="usa"): { ?>
<!--USA Store-->
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('your-static-block-id-usa')->toHtml() ?>
<!--End USA Store-->
<?php } endif; ?>
<!--End Adding dynamic static block for multiple store view-->

Step 3

  1. Open file /layout/page.xml ( /app/design/frontend/YOUR-THEME-FOLDER/layout/page.xml)
  2. Copy paste following code and change your phtml as you wish.

<!--Adding static blocks for right hand side column-->
<block type="core/template" name="yourphtml" template="page/yourphtml.phtml"/>
<!--End Adding static blocks for right hand side column-->

Note: Make sure you copy paste above code under following block
<block type="page/html" name="root" output="toHtml" template="page/3columns.phtml">
<!-- block goes here -->
</block>

Step 4

  1. Open your /template/page directory. ( /app/design/frontend/YOUR-THEME-FOLDER/template/page)
  2. Open 2columns-right.phtml file (If you need to place static block in left hand side open, 2columns-left.phtml)
  3. Copy and paste following code following div tag (<div class=”col-right sidebar”>)

<div class="col-right sidebar">
<?php echo $this->getChildHtml('right') ?>
<!--Adding static blocks for right hand side static blocks-->
<?php echo $this->getChildHtml('yourphtml') ?>
<!--End Adding static blocks for right hand side static blocks-->
</div>

Q: And how will this work for SEO?!

A: For SEO point of view it’s doesn’t have any effect. Because static block contain html and it’s dynamically converted by PHP echo statements. So dynamic static blocks will work normally. SEO will work normally.

Don’t forget to share your ideas. Thanks for reading.