9

How to Get Product current category Name in Product View Page above the Product Name. And give me file path Location to get Current Product Category Name in Magento 2.

10

Create a custom module like below code.

create a module block file to get current category name.

<?php
namespace Namespace\Modulename\Block;
class Blockname extends \Magento\Framework\View\Element\Template
{
    protected $_registry;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        array $data = []
    )
    {
        $this->_registry = $registry;
        parent::__construct($context, $data);
    }


    public function getCurrentCategory()
    {
        return $this->_registry->registry('current_category');
    }

}
?>

create a phtml file with below code

<?php if ($currentCategory = $block->getCurrentCategory()): ?>
    <div class="detail-category-name">
        <?php echo $currentCategory->getName(); ?>
    </div>
<?php endif; ?>

call above phtml file via xml file.

<referenceContainer name="product.info.main">
            <block class="Namespace\Modulename\Block\Blockname" name="product.category.name" template="Magento_Catalog::product/view/yourfilename.phtml" >
            </block>
</referenceContainer>

And finally you can see category name just above product name on product detail page.

Let me know if you have any query.

  • i have Put xml code in catalog_product_view.xml in Product.info.main refrence but i cant see any div or any category name. – Nikul Jun 15 '16 at 12:41
  • You have to put it in your theme default.xml. – Suresh Chikani Jun 15 '16 at 13:21
  • Yes i have tried to Put in default.xml but its not working.. – Nikul Jun 17 '16 at 9:13
  • thanks. works like a charm. be careful at the path you're calling (mine was "Vendor_Categoryonproduct::category.phtml) and don't forget the registration and module file. – Claudiu Creanga Jul 18 '16 at 18:35
  • i am not getting any result – vijay b Feb 17 '17 at 11:56
0

You should be able to get the category Id's by using the getCategoryIds() method on a product. For example, reference:

\Magento\Catalog\Model\Product::getCategoryIds();

This means that you should be able to essentially call $product->getCategoryIds() and filter through that in your block.

There is also a method called getCategoryCollection() in the same model of which would allow you to cycle through the categories as full objects. It should cut out the middle man of loading them with the Id's.

0
**Add XML Code Theme/namespace/Magento_Catalog/templates/product/view**

<block class="Magento\Catalog\Block\Product\View" name="product.info.category" after="product.price.final" template="product/view/current_category.phtml" />


**Create New File    Theme/namespace/Magento_Catalog/templates/product/view**  

      <?php 
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
        $categories = $product->getCategoryIds(); /*will return category ids array*/
        foreach($categories as $category){
            $cat = $objectManager->create('Magento\Catalog\Model\Category')->load($category);
            echo $cat->getName();
            }

        ?>
-1
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
echo $category->getName();
?>
  • 1
    Using the ObjectManager like that is a bad practice. – Daniel Kratohvil Aug 23 '18 at 11:53

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.