Question

i'm trying to show the categories in the top menu with the corresponding category description but can't find any function for that.

With the object manager i can get the id, name etc. but not the description.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

// get current store’s categories
$categoryHelper = $objectManager->get('\Magento\Catalog\Helper\Category');
$categories = $categoryHelper->getStoreCategories();

foreach ($categories as $category) {
echo $category->getId() . '<br />';
echo $category->getName() . '<br />';
print_r($category->getData());. '<br />';

}

Any idea how i can get each category description?

Was it helpful?

Solution

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$categoryFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');

$categories = $categoryFactory->create()
    ->addAttributeToSelect('*')
    ->setStore($storeManager->getStore()->getId());

foreach ($categories as $category) {
    var_dump($category->getId());
    var_dump($category->getName());
    var_dump($category->getDescription());
}

Info : I gave you this solution to guide you for getting what you need but, using the object manager is not a recommended way, use a block instead, you create some function getCategoryDescription() for exemple, then you get it in your phtml like this : $block->getCategoryDescription().

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top