0

I want to get the current product parent category on the product detail page in Magento2?

My Code:

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

But I am not getting the value

Any help would be appreciated.

2 Answers 2

0

Please use below code :

<?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->getParentCategory()->getName();
} 
?>
0

create custom block in your custom module

Vendor\Module\Block\Yourblock.php

<?php
namespace Vendor\Module\Block;
class Yourblock extends \Magento\Framework\View\Element\Template
{
    protected $_registry;
    protected $_categoryModel;
    public $_categoryNames = array();

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


    public function getCurrentProduct() {

        return $this->_registry->registry('current_product');
    }


    protected function getCurrentProductCategories() {
        $currentProduct = $this->getCurrentProduct(); // Your current product object

        $categories = $currentProduct->getCategoryIds(); /*returns category ids array assigned to product*/
        foreach($categories as $category):
            $cat = $this->_categoryModel->load($category);
            $this->_categoryNames[] =  $cat->getName();
        endforeach;
        return $this->_categoryNames;
    }

}

Vendor\Module\view\frontend\templates\category_name.phtml

<?php 
$currentCategories = $block->getCurrentProductCategories();

if ($currentCategories): 
foreach($currentCategories as $key => $categoryName):
?>

    <div class="your-html-data">
        <?php echo $categoryName; ?>
    </div>

<?php           
endforeach;
endif; 
?>

Vendor\Module\view\frontend\layout\catalog_product_view.xml

<referenceContainer name="content">
   <block class="Vendor\Module\Block\Yourblock" name="product_category_name" template="Vendor_Module::category_name.phtml" >
   </block>
</referenceContainer>

I hope it will work for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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