0

I made the following in Magento 1 for a multi-store website:

$rootCategory = Mage::getModel('catalog/category')->load( Mage::app()->getStore()->getRootCategoryId() );

$sameStoreCategories = Mage::getResourceModel('catalog/category_collection')
        ->addIdFilter($_product->getCategoryIds())
        ->addFieldToFilter('path', array('like' => $rootCategory->getPath() . '/%'))
        ->getItems();

Is there a way to get the same in Magento 2 in a template file?

I tried the following in my phtml file located at Mage_Catalog > template > product > view:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $block->getProduct();
$cats = $product->getCategoryIds();

The problem is, it brings all the category IDs of all my stores, not only the current one.

  • Use this ->setStore($this->_storeManager->getStore()); //categories from current store will be fetched – Partab Saifuddin Zakir Sep 17 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.