How to Pass Data From CMS Block to PHTML File in Magento 2
Magento 2 CMS is popular among the E-commerce store owners owing to its flexibility. It allows fulfilling the business requirements that are based on customer expectations or to improve the onsite user experience.
As a part of such requirement fulfilment and enriching the store frontend, the following solution to pass data from CMS block to PHTML file in Magento 2 can be helpful.
Using the below code, one can update the category on the home page to display the subcategories, display product blocks, offers launched, etc.
Method to pass data from CMS block to PHTML file in Magento 2:
Add the following code in any static block:
1 |
{{block class="Magento\Framework\View\Element\Template" template="Magento_Catalog::display-product.phtml" product-sku="VVXL10"}} \\ Here you can put your custom block class |
Create display-product.phtml in app/design/[Namespace]/[Theme]/Magento_Catalog/templates
1 2 3 4 5 |
<?php $productSku = $block->getData('product-sku'); echo "Product SKU ".$productSku; |
That’s it.
Any doubts? Please mention them in the Comments section below and I’d be happy to help.
Feel free to share the solution with Magento 2 community via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Apply an Admin Theme in Magento 2
How To Add Custom CSS and JS in Magento 2
Next