Magento2 判断是否是 首页,列表页,产品页

2.32K 浏览开发笔记

Magento2 判断是否是 首页,列表页,产品页

$router = $this->getRequest()->getRouteName().'_'.$this->getRequest()->getControllerName().'_'.$this->getRequest()->getActionName();
if($router=="cms_index_index"){
    //当前页是首页
}
if($router=="catalog_category_view"){
    //当前页是分类列表页
}
if($router=="catalog_product_view"){
    //产品详细页面
}

判断是否为列表页
$router = $this->getRequest()->getRouteName().'_'.$this->getRequest()->getControllerName().'_'.$this->getRequest()->getActionName();
if($router=="catalog_category_view"){
    //当前页是分类列表页
}

判断是否为产品页
$router = $this->getRequest()->getRouteName().'_'.$this->getRequest()->getControllerName().'_'.$this->getRequest()->getActionName();
if($router=="catalog_product_view"){
    //产品详细页面
}

判断用户是否登录
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$customerSession = $objectManager->get('MagentoCustomerModelSession');

if ($customerSession->isLoggedIn()) {
    //判读当前用户是否登录
    var_dump($customerSession->getCustomer());
    //用户登录则打印用户信息
}else{
    echo '未登录';
}

3