16 April, 2024

Magento Static Blocks

Static Blocks can be an excellent way to include HTML content in your Magento eCommerce website, particularly if the block content gets frequently changed. These blocks can help you to edit just the block content rather than editing the full page content. 

Let us see how to create a static block and use it in a different way

CREATING A STATIC BLOCK:

  • Go to admin CMS->Static Blocks
  • Click Add New Block in the top right corner
  • Give your Block a Title. For Example: Sale Banner
  • Give your block an identifier which will be used to call the block. Make sure the identifier is all lowercase and separted by underscores to follow Magento’s nomenclature i.e. your_block_id
  • Set status to the block
  • Enter your HTML in the content field
  • Click Save block or Save and Continue Edit to save your settings

Now you have created a block called “Sale Banner”. Let us now see how to show this block in front end.

Also Read: How to Create Structural Block in Magento

There are four ways to show a block in front end.

  • How to call the static block in the layout XML file.
  • How to add the static block in the PHTML file.
  • How to add the Static block in the CMS Page .
  • How to add Static block in the email template.

1.HOW TO CALL THE STATIC BLOCK IN THE LAYOUT XML FILE:

Magento uses XML layout files to help dynamically position blocks. For example, If you want to add a static block to the home page, you need to work with page.xml.

To find the layout files, I navigate to app > design > frontend > default > your_theme > layout.

For example if you want to place a static block in the right section of the page, add the following code to page.xml.

<reference name=”right”>

<block type=”cms/block” name=”your_block_id” before=”-“>

<action method=”setBlockId”><block_id>your_block_id</block_id></action>

</block>

</reference>

Now your block will appear in right section of all pages.

2.HOW TO ADD THE STATIC BLOCK IN THE PHTML FILE:

Adding your static block inline with PHP is the quickest way to get your block in your template. If you want to display your block in cart page alone, go to app > design > frontend > default > your_theme > template > checkout > cart.phtml and place the following code where you want to display your block.

<?php

echo $this->getLayout()->createBlock(‘cms/block’)->setBlockId(‘your_block_id’)->toHtml();

?>

3.HOW TO ADD THE STATIC BLOCK IN THE CMS PAGE:

Magento has a option to place your static block while Magento’s admin is creating CMS pages or other static blocks.

For example if you want to display the contact information in all cms pages, simply create a static block and add the following code to your cms page.

{{block type=”cms/block” block_id=”your_block_id”}}

This code will place the block “your_block_id” inline in your CMS page.

4.HOW TO ADD STATIC BLOCK IN THE EMAIL TEMPLATE:

You can use your static block as a mail template content.

For example if you want to place a static content about your company in all emails, just create a static block and include the following line in your mail template.

{{block block_id=”your_block_id”}}

So, this is how you create a static block on your Magento eCommerce website. From now, you need not have to edit the full page of your site; instead you can edit just the block content.


Leave a Reply

Your email address will not be published. Required fields are marked *