magento2-full-path-category-product-breadcrumb
magento2-full-path-category-product-breadcrumb copied to clipboard
only showing last category
I have 3 level categories but for some reason on only see the last category not the last
Same here. $product->getCategoryIds()
returns only the last category somehow. Magento 2.4.1 . Wondering how it can be fixed?
OK, I've found the fix. The code for loading categories need to looks like this:
$productCategories = $this->getCurrentProduct()->getCategoryCollection();
if (count($productCategories) === 0) {
return [];
}
$productCategory = current(iterator_to_array($productCategories));
$categories = $productCategory->getParentCategories();
$excludedCategoriesIds = $this->getExcludedCategoriesIds();
$filteredCategories = array_filter(
$categories,
function ($category) use($excludedCategoriesIds) {
return !in_array($category->getId(), $excludedCategoriesIds);
}
);
return $filteredCategories;
If you want, you can use our fork https://github.com/groomershop/module-full-breadcrumbs that has that fixed. Beware, that fork changes a bit the way the breadcrumbs template looks and behaves ; it assumes that you use theme-creativeshop . If you don't, you'll have to tweak the breadcrumbs.phtml
and maybe move the <block name="full_breadcrumbs" />
somewhere else in your catalog_product_view.xml
.
P.S. I think whether it needs to be changed or not might depend on your store setting. Like, there's this setting in magento, that controls whether the product's categories contains all its' categories' ancestors; or just the deepest children? I'm not expert in magento, so I'm not sure, but just leaving this as a side comment.