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

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

3 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

5 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

7 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago