Magento – Change Breadcrumbs Home link’s URL 2.1

breadcrumbsmagento2

enter image description here

By default clicking breadcrumbs Home will go to home page.
How can I change the URL for home link to be different than store home page?
For Magento 2.1.

Best Answer

You have to override Breadcrumbs block under

/vendor/magento/module-catalog/Block/Breadcrumbs.php

So, create your module and your di.xml in vendorName/moduleName/etc folder:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Block\Breadcrumbs" type="vendorName\moduleName\Block\Breadcrumbs" />
</config>

Then create your Breadcrumbs.php block file in vendorName/moduleName/Block folder:

<?php

namespace vendorName\moduleName\Block;

class Breadcrumbs extends \Magento\Catalog\Block\Breadcrumbs
{

    protected function _prepareLayout()
    {
        if ($breadcrumbsBlock = $this->getLayout()->getBlock('breadcrumbs')) {
            $breadcrumbsBlock->addCrumb(
                'home',
                [
                    'label' => __('Home'),
                    'title' => __('Go to Home Page'),
                    'link' => $this->_storeManager->getStore()->getBaseUrl()
                ]
            );

            //$breadcrumbsBlock->setTemplate('WaPoNe_HelloWorld::bread.phtml');

            $title = [];
            $path = $this->_catalogData->getBreadcrumbPath();

            foreach ($path as $name => $breadcrumb) {
                $breadcrumbsBlock->addCrumb($name, $breadcrumb);
                $title[] = $breadcrumb['label'];
            }

            $this->pageConfig->getTitle()->set(join($this->getTitleSeparator(), array_reverse($title)));
        }

    }
}

In this file you can change parameters in addCrumb() method: label, title and link.

Related Topic