I need to take the main category . i try the following code
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($itemid);
$cats = $product->getCategoryIds();
print_r($cats) // It returns more than 1 category id
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();
}
I have a doubt which one is the main category of the product ... I am not have much knowledge in ecommerce. I am struggle which one i need to choose.