2

I am currently having difficulties displaying the correct breadcrumbs in my product view page in Magento 2.4.

What I'm currently seeing if I am in a product that is e.g. Cat 1 > Sub 1 > Product is:

Home > Productname

Here some details:

Block being used: /var/www/html/vendor/magento/module-catalog/view/frontend/templates/product/breadcrumbs.phtml

Content:

    <?php
    /**
     * Copyright © Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */
    /** @var \Magento\Theme\Block\Html\Breadcrumbs $block */
    /** @var \Magento\Catalog\ViewModel\Product\Breadcrumbs $viewModel */
    $viewModel = $block->getData('viewModel');
    ?>
    <div class="breadcrumbs"></div>
    <?php
    $widget = $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonDecode($viewModel->getJsonConfigurationHtmlEscaped());
    $widgetOptions = $this->helper(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($widget['breadcrumbs']);
    ?>
    <script type="text/x-magento-init">
        {
            ".breadcrumbs": {
                "breadcrumbs": <?= /* @noEscape */ $widgetOptions ?>
            }
        }
    </script>

Ideally this should display as: Home > Cat > Sub 1

I would not want to use any additional plugin or extension, so I am wondering if there is any method to have this adjusted quickly? Thank you

1
1

First you have to add block in your module

Path: app\code\Vendor\Module\Block\Crumbblock.php

<?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 Crumbblock extends \Magento\Framework\View\Element\Template
{

    /**
     * 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);
    }

    public function getCrumbs()
    {
        $evercrumbs = array();
        
        $evercrumbs[] = array(
            'label' => 'Home',
            'title' => 'Go to Home Page',
            'link' => $this->_storeManager->getStore()->getBaseUrl()
        );

        $path = $this->_catalogData->getBreadcrumbPath();
        $product = $this->registry->registry('current_product');
        $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) {
            $evercrumbs[] = array(
                'label' => $category->getName(),
                'title' => $category->getName(),
                'link' => $category->getUrl()
            );
        }
    
        
        $evercrumbs[] = array(
                'label' => $product->getName(),
                'title' => $product->getName(),
                'link' => ''
            );
                
        return $evercrumbs;
    }
}

After that add layout file catalog_product_view.xml in your module

Path should be: app\code\Vendor\Module\view\frontend\layout\catalog_product_view.xml

<?xml version="1.0"?>
<page>
    <body>
    <referenceBlock name="breadcrumbs" remove="true" />
    <referenceContainer name="page.top">
        <block class="Vendor\Module\Block\Crumbblock" name="crumbs" as="crumbs" template="Vendor_Module::crumbs.phtml" />
    </referenceContainer>
    </body>
</page>

After that Add phtml file in your module

Path Should be like: app\code\Vendor\Module\view\frontend\templates\crumbs.phtml

<?php $crumbs = $block->getCrumbs(); ?>
<?php if ($crumbs && is_array($crumbs)) : ?>
<div class="container">
    <div class="breadcrumbs">
        <ul class="items">
            <?php
            foreach ($crumbs as $crumbName => $crumbInfo) : ?>
                <li class="item <?php echo $crumbName == 0 ? "home" : ""; ?>">
                    <?php if ($crumbInfo['link']) : ?>
                        <a href="<?= /* @escapeNotVerified */
                        $crumbInfo['link'] ?>" title="<?= $block->escapeHtml($crumbInfo['title']) ?>">
                            <?= $block->escapeHtml($crumbInfo['label']) ?>
                        </a>
                    <?php else: ?>
                        <strong><?= $block->escapeHtml($crumbInfo['label']) ?></strong>
                    <?php endif; ?>
                </li>
            <?php
            endforeach; ?>
        </ul>
    </div>
</div>
<?php endif; ?>

I hope this will help

2
  • Thank you very much, I will try this today – sierra Nov 13 '20 at 1:22
  • Thank you, it worked! Thanks for your solution – sierra Nov 14 '20 at 1:50
0

Fot those of you who are struggling with showing the breadcrumbs of a product within magento ^2.4: please note that regenerating the categories with the bin/magento regenerate:category:tree might also help. It did in my case at least.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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