1

If product belongs to Blue-Shirt(may be Shirt also). and category and tree is like

-Root(Id-2)
 -Shirt (Id-35)
  -Blue-Shirt(Id-36)

I want to get category ID like breadcrumbs 1/2/35/36 .May be i can get it from breadcrumbs somewhere?

enter image description here

2
  • what is the issue?
    – Amit Bera
    Jun 19, 2018 at 12:21
  • I have a category attribute and i want to display on product page,if value is empty in child-category it should check for it's parent and so on... Jun 19, 2018 at 12:27

2 Answers 2

1

Try below code snippet to your product/view/details.phtml

<div>
    <?php
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
        $categoryIds = $product->getCategoryIds();
        $categoryFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');

        $categories = $categoryFactory->create()->addAttributeToSelect('*')->addAttributeToFilter('entity_id', $categoryIds)->setOrder('position', 'ASC'); ?>
        <ul class="items">
        <?php

        foreach ($categories as $category){?>
             <li class="item" style="display:inline;"><?php
            echo $category->getName()."(".$category->getId().")";
         ?>
            </li>
            <?php echo "/";} 

    ?> 
    </div>

This will display all the assigned categories to the products Like,

Default Category(2) / All Products(9) / Fashion(4) / Fragrance(6)

Likewise you can get the value of you category attribute and display it here.

3
  • If i haven't select 'Fashion' then it will not show and it will not show according level oreder it will show first which you selected first Jun 20, 2018 at 10:01
  • @Ketan Please check I have updated the code in my answer.
    – techfoodie
    Jun 20, 2018 at 11:44
  • Thank you! Worked great for me! In myTheme../Magento_Catalog/templates/product/breadcrumbs.phtml Jul 13, 2018 at 15:20
0

The response of @techie is not exactly correct. $product->getCategoryIds() delivers only all categories which have been selected in the product. It only works when selecting the while category path (and only that) in the Magento backend.

Furthermore, you should avoid using ObjectManager directly! Use dependency injection via constructor of a ViewModel instead.

Here some potential way of implementing this:

use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Framework\App\ActionInterface;

class MyViewModel implements ArgumentInterface
{
    private CollectionFactory $collectionFactory;
    public function __construct(
        CollectionFactory $categoryRepository
    ) {
        $this->collectionFactory = $categoryRepository;
    }

    private function getCategories(?array $categoryIds, int $storeId, string $levelSortOrder = 'ASC'): array
    {
        $categoryList = [];
        if ($categoryIds) {
            $categories = $this->collectionFactory->create()
                ->setStoreId($storeId)
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('is_active', 1)
                ->addAttributeToFilter('entity_id', $categoryIds)
                ->addAttributeToFilter('level', ['gteq' => 2])
                ->setOrder('level', $levelSortOrder);
            foreach ($categories as $category) {
                /* @var $category Category */
                $categoryList[$category->getId()] = $category;
            }
        }
        return $categoryList;
    }

    public function getBreadcrumbCategoryPath(ProductInterface $product, ?int $prevVisitedCategoryId = null): array
    {
        $categories = $this->getCategories($product->getCategoryIds(), $product->getStoreId(), 'DESC');
        $category = $categories($prevVisitedCategoryId] ?? array_shift($categories);

        if ($category) {
            return $this->getCategories($category->getPathIds(), $product->getStoreId());
        }

        return [];
    }
}

You only need to find some way to track which category the customer was visiting previously.

Rendering these categories as breadcrumbs in the template can be done similar to @techie's example.

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.