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

 -Shirt (Id-35)

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

  • what is the issue? – Amit Bera Jun 19 '18 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... – Ketan Borada Jun 19 '18 at 12:27

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

        $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">

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


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.

  • 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 – Ketan Borada Jun 20 '18 at 10:01
  • @Ketan Please check I have updated the code in my answer. – techie Jun 20 '18 at 11:44
  • Thank you! Worked great for me! In myTheme../Magento_Catalog/templates/product/breadcrumbs.phtml – Iveta Allogenes Jul 13 '18 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.