The site is setup to

Use Categories Path for Product URLs - No

Use Canonical Link Meta Tag For Categories - Yes

Use Canonical Link Meta Tag For Categories - Yes

The category page shows the category & sub-category structure in the breadcrumbs.

However the product page only shows the product name in the breadcrumbs even if the product page was open through a category page.

How can I get the product page breadcrumb to show the category structure the product page was visited from.

Example category page - cat1 which lists product xyz

When product xyz is visted from the cat1 page listing, the breadcrumb should show

Home > Cat1 > xyz

Note: the product is also part of other categories.

1 Answer 1


You need to customize your breadcrumb, it will work even if you open product from home page

First you have to add block in your module

Path: app\code\Vendor\Module\Block\Crumbblock.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->addAttributeToSort('level', $categoryCollection::SORT_ORDER_DESC)->addAttributeToFilter('path', array('like' => "1/" . $this->_storeManager->getStore()->getRootCategoryId() . "/%"));
        $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"?>
    <referenceBlock name="breadcrumbs" remove="true" />
    <referenceContainer name="page.top">
        <block class="Vendor\Module\Block\Crumbblock" name="crumbs" as="crumbs" template="Vendor_Module::crumbs.phtml" />

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">
            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']) ?>
                    <?php else: ?>
                        <strong><?= $block->escapeHtml($crumbInfo['label']) ?></strong>
                    <?php endif; ?>
            endforeach; ?>
<?php endif; ?>

I hope this will help

  • However the same product is also part of other categories, will it still work? Jul 29, 2019 at 13:04
  • @user2240778 it will show the first category.
    – Korbin
    Jul 29, 2019 at 13:24
  • What about the sub-categories, won't that show, for example "Home > Category > Sub-category > Sub-category > Product Name" Jul 29, 2019 at 14:36
  • It will get the large number of categories and show it on breadcrumb. like if you assign a product in "New arrival" and also in "Grocery -> Fresh Product" then it will show "Grocery->Fresh Product". Please try this code, it will work Jul 30, 2019 at 5:48
  • It shows all the categories in the breadcrumbs, and it doesn't show the category I used to get to the product, it shows a different category. Jul 30, 2019 at 13:21

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.