Skip to content

Instantly share code, notes, and snippets.

@mbahar
Last active January 17, 2019 03:54
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mbahar/82703c4b95d924d9bc8e6990202fdeba to your computer and use it in GitHub Desktop.
Save mbahar/82703c4b95d924d9bc8e6990202fdeba to your computer and use it in GitHub Desktop.
Fix for Magento 2 breadcrumbs inconsistency : Magento\Catalog\Block\Breadcrumbs
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
/**
* Catalog breadcrumbs
*/
namespace xxxx\Modules\Block\Catalog;
use Magento\Catalog\Helper\Data;
use Magento\Framework\View\Element\Template\Context;
use Magento\Store\Model\Store;
class Breadcrumbs 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,
\Magento\Framework\Registry $registry,
\Magento\Framework\ObjectManagerInterface $objectmanager,
array $data = [])
{
$this->_catalogData = $catalogData;
$this->_coreRegistry = $registry;
$this->_objectManager = $objectmanager;
parent::__construct($context, $data);
}
/**
* Retrieve HTML title value separator (with space)
*
* @param null|string|bool|int|Store $store
* @return string
*/
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 getCategory ($product) {
$_categoryFactory = $this->_objectManager->create('Magento\Catalog\Model\CategoryFactory');
// for multiple categories, select only the first one
// remember, index = 0 is 'Default' category
if (! $product->getCategoryIds())
return null;
if (isset ( $product->getCategoryIds()[1]))
$categoryId = $product->getCategoryIds()[1] ;
else
$categoryId = $product->getCategoryIds()[0] ;
// Additionally for other types of attributes (select, multiselect, ..)
$category = $_categoryFactory->create()->load($categoryId);
return ['label' => $category->getName(), 'url' => $category->getUrl() ] ;
}
/**
* Preparing layout
*
* @return \Magento\Catalog\Block\Breadcrumbs
*/
protected function _prepareLayout()
{
$product = $this->_coreRegistry->registry('current_product');
//return parent::_prepareLayout();
if ($breadcrumbsBlock = $this->getLayout()->getBlock('breadcrumbs')) {
$breadcrumbsBlock->addCrumb(
'home',
[
'label' => __('Home'),
'title' => __('Go to Home Page'),
'link' => $this->_storeManager->getStore()->getBaseUrl()
]
);
$title = [];
$path = $this->_catalogData->getBreadcrumbPath();
// If we are at the product page and the $path does not include a category,
// then we will append the category link here manually
// Magento doesn't seem to append category paths to breadcrums consistently
// Reported here; https://github.com/magento/magento2/issues/7967
if($product != null ) {
// check for category path
$foundCatPath=false;
foreach ($path as $name => $breadcrumb) {
if ( strpos ( $name, 'category' ) > -1 )
$foundCatPath=true;
}
// append the category path
if (! $foundCatPath) {
$productCategory = $this->getCategory($product) ;
if ($productCategory) {
$categoryPath = [ 'category' => ['label' => $productCategory['label'] , 'link' => $productCategory['url']] ];
$path = array_merge ($categoryPath ,$path );
}
}
}
foreach ($path as $name => $breadcrumb) {
$breadcrumbsBlock->addCrumb($name, $breadcrumb);
$title[] = $breadcrumb['label'];
}
// print_r ($path );
// die ();
$this->pageConfig->getTitle()->set(join($this->getTitleSeparator(), array_reverse($title)));
}
return parent::_prepareLayout();
}
}
@bachus03
Copy link

Hi Thanks for your solution, but it does not work with magento multistore I get categories from diferent strore in breadcrumb :D Would you be able to upgrade your script?

@valuead
Copy link

valuead commented Dec 25, 2018

Hi! Whats the status, guys?

@nagarajuk12
Copy link

Hi,
Can you please is the above solutions will fix my issue?
https://magento.stackexchange.com/questions/257932/magento-2-breadcrumbs-issue-on-product-view-page

Please suggest me? thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment