2

Breadcrumbs is not showing the home ,category names in product details page.

It shows Only Product name. How do I resolve it,Please provide me a solution

In latest version 2.3.3 also same issue only showing Home > Product name Not

showing Name of category before product name

Like : https://prnt.sc/qg9rnb

2 Answers 2

7

In app/design/frontend/VendorTheme/ThemeName/Magento_Catalog/layout/catalog_product_view.xml

<?xml version="1.0"?>
<page>
<body>
    <referenceBlock name="breadcrumbs" remove="true" />
    <referenceContainer name="page.top">
        <block class="XXX\Breadcrumbs\Block\Breadcrumbs" name="product.view.breadcrumbs" template="Magento_Theme::html/breadcrumbs.phtml" />       
    </referenceContainer>
</body>
</page>

In app/design/frontend/VendorTheme/ThemeName/Magento_Theme/templates/html/breadcrumbs.phtml:

<?php $crumbs = $block->getBreadcrumb(); ?>
<?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 else: ?>
                        <?= $block->escapeHtml($crumbInfo['label']) ?>
                    <?php endif; ?>
                </li>
            <?php endforeach; ?>
        </ul>
    </div>
<?php endif; ?>

In App/Code/XXX/Breadcrumbs/Block/Breadcrumbs.php:

<?php

namespace XXX\Breadcrumbs\Block;

use Magento\Catalog\Helper\Data;
use Magento\Framework\View\Element\Template\Context;

class Breadcrumbs extends \Magento\Theme\Block\Html\Breadcrumbs
{

    /**
     * Catalog data
     *
     * @var Data
     */
    protected $_catalogData = null;
    protected $path = array();

    /**
     * @param Context $context
     * @param Data $catalogData
     * @param array $data
     */
    public function __construct(Context $context, Data $catalogData, array $data = [])
    {
        $this->_catalogData = $catalogData;
        parent::__construct($context, $data);
    }

    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 getBreadcrumb()
    {
        $this->addCrumb(
                'home', [
            'label' => __('Home'),
            'title' => __('Go to Home Page'),
            'link' => $this->getBaseUrl()
                ]
        );
        foreach ((array) $this->path as $name => $breadcrumb) {
            $this->addCrumb($name, $breadcrumb);
        }
        return $this->getCrumbs();
    }

    protected function _prepareLayout()
    {
        $this->path = $this->_catalogData->getBreadcrumbPath();
        $title = [];
        foreach ((array) $this->path as $name => $breadcrumb) {
            $title[] = $breadcrumb['label'];
        }
        return $this->pageConfig->getTitle()->set(join($this->getTitleSeparator(), array_reverse($title)));
        //return parent::_prepareLayout();
    }

    public function getCrumbs()
    {
        return $this->_crumbs;
    }

    public function getBaseUrl()
    {
        return $this->_storeManager->getStore()->getBaseUrl();
    }

}

If you got it, let me know.

4
  • Can you tell me the exact path where I need to put these codes Commented Jun 17, 2019 at 9:57
  • I update exact path where I need to put these codes Commented Dec 27, 2019 at 7:55
  • I tried this and got 2 exception(s): Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid block type: XXX\Breadcrumbs\Block\Breadcrumbs Exception #1 (ReflectionException): Class XXX\Breadcrumbs\Block\Breadcrumbs does not exist
    – Chris
    Commented Aug 28, 2020 at 14:13
  • Confirmed that this is still working in Magento 2.3.5-p1. Make sure that within the catalog_product_view.xml the Block Template points to Magento_Theme and not XXX_Breadcrumbs though Commented Sep 28, 2020 at 10:07
0

Try This

app/code/VedoreName/ModuleName/view/frontend/layout

catalog_product_view.xml

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="breadcrumbs" template="Magento_Theme::html/breadcrumbs.phtml" />
        <block class="Magento\Catalog\Block\Breadcrumbs" />
    </body>
</page>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.