1. Create a file with the following layout: Vendor\Module\view\layout\catalog_product_view.xml
1 2 3 4 5 6 |
<?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="Vendor\Module\Block\Breadcrumbs" name="breadcrumbs" template="Magento_Theme::html/breadcrumbs.phtml"/> </body> </page> |
2. Create a file with the following block: Vendor/Module/Block/Breadcrumbs.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
<?php namespace Vendor\Module\Block; use Magento\Catalog\Helper\Data; use Magento\Framework\View\Element\Template\Context; use Magento\Store\Model\Store; use Magento\Framework\Registry; class Breadcrumbs extends \Magento\Theme\Block\Html\Breadcrumbs { /** * 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', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store); return ' ' . $separator . ' '; } public function getCrumbs() { return $this->_crumbs; } /** * Preparing layout * * @return \Magento\Catalog\Block\Breadcrumbs */ 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_THEME\Magento_Theme\templates\html\breadcrumbs.phtml
[crayon-63d3ea947bff7025632701/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.