How to Identify Current Page in Magento 2 Phtml File

How to Identify Current Page in Magento 2 Phtml File

Magento 2 is the most preferred CMS among E-commerce store owners owing to its flexibility and customization ability. It allows fulfilling the business requirements to meet customer expectations or to improve the onsite user experience.

As a part of such a business requirement and customizing the default features, the following solution to identify current page in Magento 2 phtml file can be helpful.

You can identify whether the current page is a category page, product page, checkout page, the home page, or CMS page. You can also customize or add the functionalities based on the current page.

For instance, if you want to add a custom notification bar on a particular page i.e., the Home page or CMS page, that notifies the current deals or festive discounts. Moreover, while working with any custom module, you may need to display the same header on every page instead of the CMS page. 

In such cases, you first have to identify the current page and apply conditions like if the current page is a home page then add a notification bar otherwise don’t display it. Or if the current page is a category page then display a percentage discount on the particular product.

Method to Identify Current Page in Magento 2 Phtml File

  1. Create Test.php file at app/code/Vendor/Module/Block/

  2. Use the below script to get current page in the template file.

Simple. Isn’t it?

That’s it.

Any doubts can be mentioned in the Comments section below and I’d be happy to help!

Do share the solution with fellow developers via social media.

Thanks.

Get Weekly Updates

Never miss Magento tips, tricks, tutorials, and news.

Thank you for subscribing.

Something went wrong.

5
(based on 8 Reviews)

He is a Magento developer at Meetanshi with a jolly, extrovert, & friendly personality. He loves playing cricket and is a passionate foodie!

Leave a reply:

Your email address will not be published.