0

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?

0
$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().

  • Awesome thank you! I did it how you said and got rid of the object manager and put it instead in a custom block in a separate module. – Rudolf Nov 7 '18 at 15:34
  • Good job :) you're welcome ! – PЯINCƏ Nov 7 '18 at 15:39

Your Answer

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

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