2

I am fairly new to Magento, and am having trouble figuring out what I need to add to do this. I have looked all over for this, but can not find a solution that shows how to do this for magento 2. I am looking for how to create a module that displays all of the subcategories of the category clicked. If there are no more subcategories, then I want to display the products for that category.

I have tried making blocks in the admin panel, and attaching these blocks to the product category. I would really like to just create a module that does this for me though.

2 Answers 2

2

You need to customized the category in the theme folder use the following file structure and update the following code

/app/design/frontend/<vendor>/theme/Magento_Catalog/templates/category products.phtml <?php // call current category and sub category $categoryHelper_sub = $this->helper('Magento\Catalog\Helper\Category'); $_category = $block->getCurrentCategory(); $objectManager_sub = $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $imagehelper = $objectManager->create('Magento\Catalog\Helper\Image'); $category = $objectManager_sub->get('Magento\Catalog\Model\Category'); $categories_sub = $category->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('is_active', 1)->addIdFilter($_category->getChildren())->setOrder('position', 'ASC');?> <?php if(!empty($categories_sub->getData())){ ?> <div class="page-products"> <div class="products wrapper grid products-grid"> <div class="col-xs-12 abt_head caps"> <h2><?php echo $_category->getName(); ?></h2> <p><?php echo $_category->getDescription(); ?></p> <div class="current-image"> <img src="<?php echo $_category->getImageUrl()?>" > </div> </div> <?php foreach($categories_sub as $category_sub): ?> <?php $category_sub->getParentId(); $categoryobj = $objectManager->create('Magento\Catalog\Model\CategoryFactory'); $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category $category_sub->getChildren();?> <div class='submenu'> <a href="<?php echo $categoryHelper_sub->getCategoryUrl($category_sub);?>"> <div class='submenu-img'> <img src="<?php echo $category_sub->getImageUrl()?>" alt="<?php echo $category_sub->getName()?>"></img> </div> <?php //echo $category_sub->getName()?></a> </div> <?php endforeach; ?> </div> <?php } ?> 
1

I have create small module for display sub category please check this link.

https://github.com/pratikmage/magento2-subcategory

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.