Skip to content

only showing last category  #29

Open
@quickwit-dev

Description

I have 3 level categories but for some reason on only see the last category not the last

Activity

jtomaszewski

jtomaszewski commented on Feb 6, 2021

@jtomaszewski

Same here. $product->getCategoryIds() returns only the last category somehow. Magento 2.4.1 . Wondering how it can be fixed?

jtomaszewski

jtomaszewski commented on Feb 6, 2021

@jtomaszewski

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.

added a commit that references this issue on Feb 6, 2021
cfe578b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      only showing last category · Issue #29 · EaDesgin/magento2-full-path-category-product-breadcrumb