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!
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityIs 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!
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
}
I guess, we can directly use
$this->getRequest()->getFullActionName()
in .phtml file to get current page action.
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');
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.
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 }
getCategoryPage
anywhere in Magento 2, at least not in 2.1.10