I upgraded Magento to 2.2.4 & now I don't have breadcrumbs on product pages. They show up on other pages but not on products. I checked the source & noticed that there is a div with the 'breadcrumbs' class & some json params, but it's empty (no errors on console).
Any idea?
UPDATE:
I found out that for a reason I can't explain , Magento started building the breadcrumbs of the product pages based on the top-menu navigation using JS & since in my case I changed the menu & used different CSS selectors, it stopped working.
I believe that now I'll be able to fix this, but I can't see any good reason for them to do that, it's way too fragile...
My Temporary Workaround (If it helps someone...):
1. Build a module & add a block that extends \Magento\Theme\Block\Html\Breadcrumbs in order to add the method getCrumbs() *No need for di.xml
namespace Vendor\Module\Block\Html;
class Breadcrumbs extends \Magento\Theme\Block\Html\Breadcrumbs
{
public function getCrumbs()
{
return $this->_crumbs;
}
public function getBaseUrl()
{
return $this->_storeManager->getStore()->getBaseUrl();
}
}
2. Override the breadcrumbs template on product page (app/design/frontend/Vendor/Theme/Magento_Catalog/templates/product/breadcrumbs.phtml)
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$catalogData = $objectManager->create('Magento\Catalog\Helper\Data');
$crumbs = false;
if ($breadcrumbsBlock = $objectManager->create('Vendor\Module\Block\Html\Breadcrumbs')) {
$breadcrumbsBlock->addCrumb(
'home',
[
'label' => __('Home'),
'title' => __('Go to Home Page'),
'link' => $breadcrumbsBlock->getBaseUrl()
]
);
$path = $catalogData->getBreadcrumbPath();
foreach ((array)$path as $name => $breadcrumb) {
$breadcrumbsBlock->addCrumb($name, $breadcrumb);
}
$crumbs = $breadcrumbsBlock->getCrumbs();
}
?>
<?php if ($crumbs && is_array($crumbs)) : ?>
<div class="breadcrumbs">
<ul class="items">
<?php foreach ($crumbs as $crumbName => $crumbInfo) : ?>
<li class="item <?= /* @escapeNotVerified */ $crumbName ?>">
<?php if ($crumbInfo['link']) : ?>
<a href="<?= /* @escapeNotVerified */ $crumbInfo['link'] ?>" title="<?= $block->escapeHtml($crumbInfo['title']) ?>"><?= $block->escapeHtml($crumbInfo['label']) ?></a>
<?php elseif ($crumbInfo['last']) : ?>
<strong><?= $block->escapeHtml($crumbInfo['label']) ?></strong>
<?php else: ?>
<?= $block->escapeHtml($crumbInfo['label']) ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>