0

I want to fetch Sub categories name of a particular Parent category using Parent category Id. Please note, i dont want to use Object manager directly in phtml file. I am using block to write function.

  • please check this link, I hope it will be helpful in your case magento.stackexchange.com/questions/125939/… – Hitesh Balpande Jan 9 at 6:29
  • @HiteshBalpande As already mentioned. i dont want to use Object manager – Nafsss Jan 9 at 6:44
  • please check 4th answers in above link Magento\Catalog\Model\CategoryFactory they used CategoryFactory not objectmanager – Hitesh Balpande Jan 9 at 6:53
  • @HiteshBalpande I tried that , it does not work – Nafsss Jan 9 at 6:54
0

You can try using category model:

Code for block file

......
protected $_categoryFactory;
......

public function __construct(
.....
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
.....
) {
        ......
        $this->_categoryFactory = $categoryFactory;
        ......
}

public function getSubCatNameById($catId) {
        $categoryName = $this->_categoryFactory->create()->load($catId)->getName();
        return $categoryName;
}

Code for phtml file

$catId = 2;  //2 is your category id

echo $block->getSubCatNameById($catId);

Hope it will help you!

| improve this answer | |
  • Does this return child category name? – Nafsss Jan 9 at 6:43
  • @Nafisa yes, by using this you can get the category name. – Faisal Sheikh Jan 9 at 6:47
  • Shiekh ......... In your code, no where it is mentioned about child category. your code will return parent category name. – Nafsss Jan 9 at 6:48
  • @Nafisa updated, you can check now. – Faisal Sheikh Jan 9 at 6:49
  • Have you tried this code? and checked what does it return? I want to use Parent category id to get child category name – Nafsss Jan 9 at 6:52
0

Please copy this code in your custom module block function, I hope it will be helpful for you.

   protected $categoryFactory;

   public function __construct(
        \Magento\Catalog\Model\CategoryFactory $categoryFactory
    ) {
        $this->categoryFactory = $categoryFactory;
    }


    public function getSubCategories() {
        $_category = $this->categoryFactory->create();
        $catId = 12;    // your parent category name
        $category = $_category->load($catId);

        $collection = $category->getCollection()
                ->addIsActiveFilter()
                ->addOrderField('name')
                ->addIdFilter($category->getChildren());
        return $collection;   //Get subcategory collection of parent category


    }       

call above function in your phtml file like this:

    $subCategories= $block->getSubCategories();

    foreach($subCategories as $subCats):
            echo $subCats->getName();
    endforeach:
| improve this answer | |
  • How to print sub categpry name in phtml file then? – Nafsss Jan 9 at 7:06
  • please check my updated answer. – Hitesh Balpande Jan 9 at 7:16
  • getting this error Recoverable Error: Object of class Magento\\Catalog\\Model\\ResourceModel\\Category\\Collection could not be converted to string – Nafsss Jan 9 at 7:20
  • okay you need to debug the $subCategories = $block->getSubCategories(); print_r($subCategories);exit; in your phtml file before foreach loop – Hitesh Balpande Jan 9 at 7:22
  • Hi @Nafisa, can you please accept the answer if code is worked for you. – Hitesh Balpande Jan 13 at 6:55

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.