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.
3 Answers
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.
I think it's better to use a plugin for this. Because overriding Magento catalog is not sufficient. You also need to overwrite Magento page, and maybe other extensions.
So the best way is to use the beforeAddCrumb
plugin method.
You need to create a module and add a di.xml file to configure your plugin :
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Theme\Block\Html\Breadcrumbs">
<plugin
name="VENDOR_MODULE_Plugin_Magento_Theme_Block_Html_Breadcrumbs"
type="VENDOR\MODULE\Plugin\Magento\Theme\Block\Html\Breadcrumbs"
sortOrder="10"
disabled="false" />
</type>
</config>
Then in your plugin file VENDOR\MODULE\Plugin\Magento\Theme\Block\Html\Breadcrumbs.php
:
<?php
namespace VENDOR\MODULE\Plugin\Magento\Theme\Block\Html;
use \Magento\Theme\Block\Html\Breadcrumbs as MagentoBreadCrumbs;
class Breadcrumbs
{
public function beforeAddCrumb(MagentoBreadCrumbs $subject, $crumbName, $crumbInfo)
{
if ($crumbName == 'home') {
$crumbInfo['label'] = __('My new Label');
$crumbInfo['link'] = 'http://mynewlink.com/';
}
return [$crumbName, $crumbInfo];
}
}
-
On Magento EE 2.2.3 this seemingly only works for category pages, not listing pages. It worked at one point, but something must have updated recently to stop this from working on listing pages only. Commented Jan 24, 2019 at 23:18
The Home link on product pages is controlled by this file:
vendor/magento/module-theme/view/frontend/web/js/view/add-home-breadcrumb.js