Question

I want to get category name using multiple category ids.

I have two category id like :- 3 or 4.Those two id using i am want to category name.

Please help me how to achieve this.

THANKS.

Was it helpful?

Solution

namespace Company\Module\Block;

class CustomBlock extends \Magento\Framework\View\Element\Template 
{
    protected $_categoryFactory;    

    public function __construct(
    \Magento\Catalog\Model\CategoryFactory $categoryFactory,
    \Magento\Framework\View\Element\Template\Context $context,
    ) {
        $this->_categoryFactory = $categoryFactory;
        parent::__construct($context);
    }

    public function getCategoryName($catIds)
    {
        $catNames = [];
        foreach ($catIds as $key => $catId) {
         $category = $this->_categoryFactory->create()->load($catId);
         $catNames[$catId] = $category->getName();
        }
        return $catNames;
    }
    
    public function getAllCatName()
    {
      $catIds = array(3,4);
      $categoryNames = $this->getCategoryName($catIds);
      print_r($categoryNames);die;
    }
}

In phtml file you can call $block->getAllCatName(); Or if you have ids 3,4 in array phtml like $catIds = array(3,4); you can directly call $categoryNames = $block->getCategoryName($catIds);

OTHER TIPS

where you need to get the categories name? phtml? block? Model?

for example, in a phtml file you can inject a viewModel and in that class you can get the categories name.

To get the categories name by multiple ids, you can made a collection filtering by entity_id and selecting only the "name" attribute, or you can instantiate the CategoryRepository and with a loop you can load the categoryModel for every id.

ok, so you need to edit the layout xml (if you need this info on the product page, you need to edit the catalog_product_view.xml). Find the block tag and add this code:

<arguments><argument name="viewModel" xsi:type="object">Vendor\Module\ViewModelFolder\ViewModelClass</argument></arguments>

This class must implement the ArgumentInterface.

In his constructor you can instantiate a CategoryRepository class, like:

protected $categoryRespository;

public function __construct(\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository) 
{
    $this->categoryRepository = $categoryRepository;
}

Or you can instantiate the CollectionFactory:

protected $categoryCollectionFactory;

public function __construct(
    \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory
) {
    $this->categoryCollectionFactory = $categoryCollectionFactory;
}

After this you need to create a method that accept an array with the ids:

public function getCategoryNameByIds(array $ids = [])
{
    $names = [];
    //for categoryRepository
    foreach ($ids as $id) {
        /** @var \Magento\Catalog\Model\Category $category */
        $category = $this->categoryRepository->get($id);
        $names[$id] = $category->getName();
    }

    //for collectionFactory
    $categoryCollection = $categoryCollectionFactory->create();
    $categoryCollection->addFieldToFilter('entity_id', [in => $ids]);

    // either call getItems() and iterate over it, or do whatever you need
    foreach ($categoryCollection->getItems() as $category) {
        /** @var \Magento\Catalog\Model\Category $category */
        $names[$category->getId()] = $category->getName();
    }
    return $names;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top