M2 2.2.4和2.2.5里产品页面不显示面包屑导航,无内容
M2 2.2.4和2.2.5里产品页面不显示面包屑导航,无内容
这应该是2.2.4的一个bug,或者说可能是主题或者其他插件影响了。
不过不要紧,我们能解决。
两步搞定。
1,xml里删除旧breadcrumbs,添加自定义的breadcrumbs
vim 主题路径/Magento_Catalog/catalog_product_view.xml
<referenceBlock name="breadcrumbs" remove="true" /> <referenceContainer name="page.top"> <block class="MagiccartAlothemesBlockEvercrumbs" name="oriarm-breadcrumbs" as="oriarm-breadcrumbs" after="-"/> </referenceContainer>
2,编写block代码
vim 插件路径/Block/Evercrumbs.php
<?php namespace MagiccartAlothemesBlock; use MagentoCatalogHelperData; use MagentoFrameworkViewElementTemplateContext; use MagentoStoreModelStore; use MagentoFrameworkRegistry; class Evercrumbs extends MagentoFrameworkViewElementTemplate { protected $_template = 'Magento_Theme::html/breadcrumbs.phtml'; /** * Catalog data * * @var Data */ protected $_catalogData = null; /** * @param Context $context * @param Data $catalogData * @param array $data */ public function __construct( Context $context, Data $catalogData, Registry $registry, array $data = []) { $this->_catalogData = $catalogData; $this->registry = $registry; parent::__construct($context, $data); } public function getCrumbs() { $evercrumbs = array(); $evercrumbs[] = array( 'label' => 'Home', 'title' => 'Go to Home Page', 'link' => $this->_storeManager->getStore()->getBaseUrl() ); $path = $this->_catalogData->getBreadcrumbPath(); $product = $this->registry->registry('current_product'); $categoryCollection = clone $product->getCategoryCollection(); $categoryCollection->clear(); $categoryCollection->addAttributeToSort('level', $categoryCollection::SORT_ORDER_DESC)->addAttributeToFilter('path', array('like' => "1/" . $this->_storeManager->getStore()->getRootCategoryId() . "/%")); $categoryCollection->setPageSize(1); $breadcrumbCategories = $categoryCollection->getFirstItem()->getParentCategories(); foreach ($breadcrumbCategories as $category) { $evercrumbs[] = array( 'label' => $category->getName(), 'title' => $category->getName(), 'link' => $category->getUrl() ); } $evercrumbs[] = array( 'label' => $product->getName(), 'title' => $product->getName(), 'link' => '' ); return $evercrumbs; } protected function _toHtml() { $crumbs = $this->getCrumbs(); if (is_array($crumbs)) { reset($crumbs); $crumbs[key($crumbs)]['first'] = true; end($crumbs); $crumbs[key($crumbs)]['last'] = true; } $this->assign('crumbs', $crumbs); return parent::_toHtml(); } }
3,测试
清缓存测试,不出意外的话 就没问题。