PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 22, 2022

[FIXED] How to get category collection using product id in product details page in Magento 2

 August 22, 2022     magento2, php     No comments   

Issue

How can I get a category collection in the product details page? I've tried using the below code, but it's not getting the collection.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')
    ->registry('current_category');
$categoryProducts = $category->getCategoryProducts($categoryId);

Solution

on the product detail page you can just call getCategoryCollection function on product's Object to get Category Collection as following :

$_product = $block->getProduct();
$categoryCollection = $_product->getCategoryCollection();


Answered By - Emizen Tech
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing