Skip to main content
added 114 characters in body
Source Link

Try below code to get category level.

$product = $objectManager->create('Magento\Catalog\Model\Product')->load($itemid);
$cats = $product->getCategoryIds();
$level = 1;
foreach ($cats as $category_id) 
{                   
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    echo "<br/>categoryid". $category_id;
    $collection = $this->_categoryFactory->create()->load($category_id);
    echo "</br> Category Name ". $collection->getName();            
    echo "<br/> Parent category name" . $collection->getParentCategory()->getName();
    echo "<br/> Category level" . $collection->getLevel();
    $categoryName = $collection->getName();
    if($collection->getLevel() > $level) {
        $level = $collection->getLevel();
        $categoryName = $collection->getName();
    }                             

}
echo $level;$categoryName; exit; // this will be the inner category name

Let me know if you need further help.

Try below code to get category level.

$product = $objectManager->create('Magento\Catalog\Model\Product')->load($itemid);
$cats = $product->getCategoryIds();
$level = 1;
foreach ($cats as $category_id) 
{                   
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    echo "<br/>categoryid". $category_id;
    $collection = $this->_categoryFactory->create()->load($category_id);
    echo "</br> Category Name ". $collection->getName();            
    echo "<br/> Parent category name" . $collection->getParentCategory()->getName();
    echo "<br/> Category level" . $collection->getLevel();
    if($collection->getLevel() > $level) {
        $level = $collection->getLevel();
    }                             

}
echo $level; exit; // this will be the inner category

Let me know if you need further help.

Try below code to get category level.

$product = $objectManager->create('Magento\Catalog\Model\Product')->load($itemid);
$cats = $product->getCategoryIds();
$level = 1;
foreach ($cats as $category_id) 
{                   
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    echo "<br/>categoryid". $category_id;
    $collection = $this->_categoryFactory->create()->load($category_id);
    echo "</br> Category Name ". $collection->getName();            
    echo "<br/> Parent category name" . $collection->getParentCategory()->getName();
    echo "<br/> Category level" . $collection->getLevel();
    $categoryName = $collection->getName();
    if($collection->getLevel() > $level) {
        $level = $collection->getLevel();
        $categoryName = $collection->getName();
    }                             

}
echo $categoryName; exit; // this will be the inner category name

Let me know if you need further help.

Source Link

Try below code to get category level.

$product = $objectManager->create('Magento\Catalog\Model\Product')->load($itemid);
$cats = $product->getCategoryIds();
$level = 1;
foreach ($cats as $category_id) 
{                   
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    echo "<br/>categoryid". $category_id;
    $collection = $this->_categoryFactory->create()->load($category_id);
    echo "</br> Category Name ". $collection->getName();            
    echo "<br/> Parent category name" . $collection->getParentCategory()->getName();
    echo "<br/> Category level" . $collection->getLevel();
    if($collection->getLevel() > $level) {
        $level = $collection->getLevel();
    }                             

}
echo $level; exit; // this will be the inner category

Let me know if you need further help.