How to call CMS Block content into Block File in Magento 2?

How to call CMS Block content into Block File

The way to make it different from others is to customize the way you love & your viewers want to see. Having Magento as open source CMS you are free to personalize and develop the way you love by installing the theme and extensions. And this theme is made up of several elements like code, blocks, widgets, media and so on. But when it comes to the static block, it is helpful in several ways to display text, sliders, product carousels, ads as well as chunks of HTML/JS/CSS code.

hyva theme development

These Magento CMS Blocks are pretty useful when we want to make manipulation of content easier instead of having a static one. Also, changing block content can take just a few minutes and can save tons of time Instead of looking into thousands of line code. You can consider this example to show discount tables for different purchase total dynamically.

First, we need to create a “Cmsblock.php” file inside the extension in the below directory

app\code\Vendor\Extension\Block

<?php 
namespace Vendor\Extension\Block;

class Cmsblock
{
	protected $cmsblock;
 
	public function __construct(\Magento\Cms\Block\Block $cmsblock)
	{
   	    $this->cmsblock = $cmsblock;
	}
 
	public function Fetchcmsblockdata()
	{
  	    $cmsblockdata = $this->cmsblock->setBlockId('block_identifier')->toHtml();
            return $cmsblockdata;
  	
	}
}

Magento version upgrade

Note: Make sure you have replaced ‘block_identifier‘ with your CMS block identifier.

After that, we need to create the “vendor_extension_index.xml” file inside the extension in the below directory

app\code\Vendor\Extension\view\frontend\layout\

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="Vendor\Extension\Block\Cmsblock" name="custom.cms.block" template="Vendor_Extension::cmsblock.phtml"/>
        </referenceContainer>
    </body>
</page>

Lastly, we need to create a “cmsdata.phtml” file at the below location to fetch block data.

app\code\Vendor\Extension\view\frontend\templates

<?php echo $block->fetchCmsBlockData(); ?>

And that’s it! Now discount tables will be visible on the store frontend depending on purchase total! You can also play with these CMS blocks according to your need.

Magento 2 Speed & Performance Optimization Services
If you need help regarding the above code? Don’t forget to leave a comment and if this found it helpful smash that stars.
Happy Coding!

Previous Article

How to Install Google reCAPTCHA on Magento custom form

Next Article

How to Get Current Visitor Id from Visitor Collection in Magento 2

Write a Comment

Leave a Comment

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

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨