Hi,
I'm trying to show category ids (or names) in product list , because i'm developing an ajax layered navigation system.
I only got the attributes, but for the categories it seems that there is no function for showing them.
Using Magento CE 2.3.5
Solved! Go to Solution.
@Bhanu Periwal wrote:Hi @webmasterc055b
You need some customisation with page. Get category name by product id or current product.
You can take reference from below code :$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/* You can get product by using id also */ $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product'); echo $product->getId(); $categories = $product->getCategoryIds(); /*will return category ids array*/ foreach($categories as $category){ $cat = $objectManager->create('Magento\Catalog\Model\Category')->load($category); echo $cat->getName(); }
It may help you to resolve issue.
If issue resolve, please click on 'Kudos' & Accept as Solution!
Hi,
I have the phtml Product list template which use
\Magento\Catalog\Block\Product\ListProduct class, i have $_product istance , i need to override this class right? and the object manager seems to be deprecated
Hi @webmasterc055b
You need some customisation with page. Get category name by product id or current product.
You can take reference from below code :
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/* You can get product by using id also */ $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product'); echo $product->getId(); $categories = $product->getCategoryIds(); /*will return category ids array*/ foreach($categories as $category){ $cat = $objectManager->create('Magento\Catalog\Model\Category')->load($category); echo $cat->getName(); }
It may help you to resolve issue.
If issue resolve, please click on 'Kudos' & Accept as Solution!
Hi @webmasterc055b
As we know a product can be assigned to multiple categories. You can category ids using product id as mentioned in below blog.
category-ids-from-product-magento-2/
Using category id you can get category name.
https://stackoverflow.com/a/38692903
I hope it will help you!
Should I override the Magento\Catalog\Block\Product\ListProduct.php file?
I followed the first guide , in the phtml I added the following, but now i see the block empty.
<?php print_r($block->getCategoryIds($_product->getId())); ?>
@Bhanu Periwal wrote:Hi @webmasterc055b
You need some customisation with page. Get category name by product id or current product.
You can take reference from below code :$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/* You can get product by using id also */ $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product'); echo $product->getId(); $categories = $product->getCategoryIds(); /*will return category ids array*/ foreach($categories as $category){ $cat = $objectManager->create('Magento\Catalog\Model\Category')->load($category); echo $cat->getName(); }
It may help you to resolve issue.
If issue resolve, please click on 'Kudos' & Accept as Solution!
Hi,
I have the phtml Product list template which use
\Magento\Catalog\Block\Product\ListProduct class, i have $_product istance , i need to override this class right? and the object manager seems to be deprecated
Hi @webmasterc055b ,
No need to override the ListProduct.php.if you have block associated with your phtml file then you can simply add class dependency in that and add function logic.
If you dont have block class associated with html then try to use view model approach.
https://devdocs.magento.com/guides/v2.4/extension-dev-guide/view-models.html
Problem solved! Click kudos & Accept as Solution.
@Nishu Jindal wrote:Hi @webmasterc055b ,
No need to override the ListProduct.php.if you have block associated with your phtml file then you can simply add class dependency in that and add function logic.
If you dont have block class associated with html then try to use view model approach.
https://devdocs.magento.com/guides/v2.4/extension-dev-guide/view-models.html
Problem solved! Click kudos & Accept as Solution.
I created a block rewrite of ListProduct for adding getCategoryIds() function , done the setup:upgrade , the setup:di:compile, flushed cache and deployed content , i added an echo and a die() in constructor method but it doesn't do nothing