How To

How to Pass data from CMS Block to phtml file in Magento 2

Promoting & showcasing new products are often difficult as part of marketing to attract new customer and bring a sale. There are thousands of ways to let your customer know about newly launched products but showcasing such products on the home page or other landing page lets your customer quickly explore store products. It is extremely beneficial for your potential customers quickly purchase products that save your marketing costs and efforts.
Many time while Magento Custom Development, we came across such requirements where the customer wants to showcase newly launched products or want to features products from a specific category that meets business needs. At that time, we simply create new block inside Magento 2 store and call that block to the .phtml file where we want to display block.
First, we need to call phtml file from static block using this code.

{{block class="Vendor\Extesnion\Block\Customblock" name="customblock" template="Vendor_Extension::products.phtml" categories="1,2,3"}}

In above code, you pass the category id with this code categories=”1,2,3″.
Now we need to get that category Ids on phtml file like this.

$CategoryId = $block->getData(‘categories’);

or

$CategoryId = $block->getCategories();

That’s it. Simply clear cache and you are done with adding product block in page.
That’s it for today, Let us know if you are facing an issue while implementing this code by commenting below.
Happy Coding!

Click to rate this post!
[Total: 5 Average: 4.2]
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

Magento 2: Add Quantity Increment and Decrement on Category Page

Hello Magento Friends, In this blog, we will discuss about adding quantity increment and decrement…

9 hours ago

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

3 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

6 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

6 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

7 days ago

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…

1 week ago