Magento 2

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

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.

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. To do the same, first we need to create a block file at the following location.
app\code\Vendor\Extension\Block\Cmsblock.php

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;
   
 }
}

Note: Make sure you have replaced ‘block_identifier‘ with your CMS block identifier.
Now to use these block class into our Magento template file, we need to specify the following code in our “vendor_extension_index.xml“.
app\code\Vendor\Extension\view\frontend\layout\vendor_extension_index.xml



  
      
         
      
  

Lastly, we need to create “cmsdata.phtml” file at below location to fetch block data.
app\code\Vendor\Extension\view\frontend\templates\cmsdata.phtml


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.
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!

Click to rate this post!
[Total: 13 Average: 4.9]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

16 hours ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

3 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

3 days ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

4 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

6 days ago

HYVÄ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We have brought exciting news for Magento store owners. Hyvä Themes recently released 1.3.6 and…

6 days ago