1. Create a file with the following layout: VendorModuleviewlayoutcatalog_product_view.xml
<?xml version="1.0"?> <page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock class="VendorModuleBlockBreadcrumbs" name="breadcrumbs" template="Magento_Theme::html/breadcrumbs.phtml"/> </body> </page>
2. Create a file with the following block: Vendor/Module/Block/Breadcrumbs.php
<?php namespace VendorModuleBlock; use MagentoCatalogHelperData; use MagentoFrameworkViewElementTemplateContext; use MagentoStoreModelStore; use MagentoFrameworkRegistry; class Breadcrumbs extends MagentoThemeBlockHtmlBreadcrumbs { /** * 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); } /** * Retrieve HTML title value separator (with space) * * @param null|string|bool|int|Store $store * @return string */ public function getTitleSeparator($store = null) { $separator = (string)$this->_scopeConfig->getValue('catalog/seo/title_separator', MagentoStoreModelScopeInterface::SCOPE_STORE, $store); return ' ' . $separator . ' '; } public function getCrumbs() { return $this->_crumbs; } /** * Preparing layout * * @return MagentoCatalogBlockBreadcrumbs */ protected function _prepareLayout() { $title = []; if ($breadcrumbsBlock = $this->getLayout()->getBlock('breadcrumbs')) { $breadcrumbsBlock->addCrumb( 'home', [ 'label' => __('Home'), 'title' => __('Go to Home Page'), 'link' => $this->_storeManager->getStore()->getBaseUrl() ] ); $path = $this->_catalogData->getBreadcrumbPath(); $product = $this->registry->registry('current_product'); if ($product && count($path) == 1) { $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) { $catbreadcrumb = array("label" => $category->getName(), "link" => $category->getUrl()); $breadcrumbsBlock->addCrumb("category" . $category->getId(), $catbreadcrumb); $title[] = $category->getName(); } //add current product to breadcrumb $prodbreadcrumb = array("label" => $product->getName(), "link" => ""); $breadcrumbsBlock->addCrumb("product" . $product->getId(), $prodbreadcrumb); $title[] = $product->getName(); } else { foreach ($path as $name => $breadcrumb) { $breadcrumbsBlock->addCrumb($name, $breadcrumb); $title[] = $breadcrumb['label']; } } $this->pageConfig->getTitle()->set(join($this->getTitleSeparator(), array_reverse($title))); return parent::_prepareLayout(); } $path = $this->_catalogData->getBreadcrumbPath(); foreach ($path as $name => $breadcrumb) { $title[] = $breadcrumb['label']; } $this->pageConfig->getTitle()->set(join($this->getTitleSeparator(), array_reverse($title))); return parent::_prepareLayout(); } } ?>
3. Override breadcrumbs.phtml below theme path:
YOUR_THEMEMagento_Themetemplateshtmlbreadcrumbs.phtml