21

Is there a way I can check that If a user on a category page,or an individual product page in Magento 2?

Any help would be greatly appreciated!

2
  • You mean after a random user login we should come to know the user is on which page.
    – Arjun
    Sep 12, 2016 at 5:23
  • It's not required for user to login. I just want to check the page is category page or product page. Like in Magento 1 We can check it like this: $category = Mage::registry('current_category'); Sep 12, 2016 at 5:24

5 Answers 5

43

You can try below code it might help you.

Inject an instance of \Magento\Framework\App\Request\Http in your class constructor.

If you are in a controller you don't need to do it. You can already access it like this $request = $this->getRequest()

public function __construct(
    ...
    \Magento\Framework\App\Request\Http $request
) {
    ...
    $this->_request = $request;
}

Then you can check if is category or product like this:

if ($this->_request->getFullActionName() == 'catalog_product_view') {
    //you are on the product page
}
if ($this->_request->getFullActionName() == 'catalog_category_view') {
    //you are on the category page
}
3
  • 7
    If you directly want it in phtml file then use like this - $this->getRequest()->getFullActionName() here is the if condition I used : if($this->getRequest()->getFullActionName() == 'catalog_category_view'){...}
    – KA9
    Dec 27, 2016 at 5:38
  • Hi @Arjun, How would you then get the name of the category you're in?
    – John
    Jul 3, 2018 at 14:48
  • how to check this using jquery? @Arun
    – Manjunath
    Apr 5, 2022 at 12:04
12

I guess, we can directly use

$this->getRequest()->getFullActionName()

in .phtml file to get current page action.

1
  • perfect answer!!! made my day :) +1 Jan 3, 2020 at 6:06
9

You can use instance of \Magento\Framework\App\Request\Http in your class constructor. If you are in a controller you don't need to do it.

You can already access it like this $request = $this->getRequest()

public function __construct(
    ...
    \Magento\Framework\App\Request\Http $request
) {
    ...
    $this->_request = $request;
}

Then you can check if is homepage or categorypage or productpage like this:

if ($this->_request->getFullActionName() == 'cms_index_index') {
    //you are on the homepage
}
if ($this->_request->getFullActionName() == 'catalog_product_view') {
    //you are on the product page
}
if ($this->_request->getFullActionName() == 'catalog_category_view') {
    //you are on the category page
}

Otherwise directly use in phtml file using object manager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$request = $objectManager->get('\Magento\Framework\App\Request\Http');
2
  • Have you try above code ? Sep 12, 2016 at 6:38
  • how to check this using jquery?
    – Manjunath
    Apr 5, 2022 at 12:03
3

If you are using a template file i.e. .phtml then directly you can call on the page using below code:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$request = $objectManager->get('\Magento\Framework\App\Request\Http');

if ($request->getFullActionName() == 'cms_index_index') {
//you are on the homepage
}

if ($request->getFullActionName() == 'catalog_product_view') {
//you are on the product page
}

if ($request->getFullActionName() == 'catalog_category_view') {
//you are on the category page
}

and if you to get a current category and its data you can do it via

$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');

$category->getData('schbang_category_name');

where schbang_category_name is my custom category attribute

I hope this might help someone.

-2

you can add conditional, such as

if($this->getProductPage()) {
  //this is a product page, do some stuff
}
elseif($this->getCategoryPage()) {
  //this is a category page, do some stuff
}
1
  • There is no function getCategoryPage anywhere in Magento 2, at least not in 2.1.10
    – Jacques
    Jan 2, 2018 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.