4

I want to create custom list (category) page in magento2.

If current category has sub category then it's show sub category.

If current category have sub category and products then it's show both.

If current category has only products then it's show products only.

which file I need to do changes?

Can anyone give me a suggestion?

4
  • what you have done so far ?
    – Arunendra
    Commented Jun 9, 2016 at 12:26
  • @Arunendra I didn't get any code for my problem otherwise I could try it.
    – Dhaval
    Commented Jun 9, 2016 at 12:28
  • where are u want to add this code ?
    – Amit Bera
    Commented Jun 9, 2016 at 13:09
  • @AmitBera I want to add this code on app/design/frontend/<theme_vendoe>/<theme>/Magento_Catalog/templates/product/list.phtml
    – Dhaval
    Commented Jun 9, 2016 at 13:10

4 Answers 4

7

You can get sub-category by below code at list.phtml

if($this->getLayer()->getCurrentCategory()):
$subcategories=$this->getLayer()->getCurrentCategory()->getCategories($this->getLayer()->getCurrentCategory()->getId());
    /* count and check the category have any sub category */
    if($subcategories->count()>0){

        foreach($subcategories as $subcategory){
            //echo "<pre>";
            //print_r($subcategory->getData());
            echo $subcategory->getName();
        }

    }else{
        // category does not have any sub category
    }
else:
        // category does not have any sub category

endif;

Edit:

add this code:

<!-- add by amit bera -->
    <?php if($this->getLayer()->getCurrentCategory()):   ?>
        <?php 
        if($subcategories->count()>0){

            foreach($subcategories as $subcategory){
                //echo "<pre>";
                //print_r($subcategory->getData());
                echo $subcategory->getName();
            }

        }   
        ?>
    <?php endif ?>

just after <?php if (!$_productCollection->count()): ?>

and

add below code:

<!-- add by amit bera -->
    <?php if($this->getLayer()->getCurrentCategory()):   ?>
        <?php 
        if($subcategories->count()>0){

            foreach($subcategories as $subcategory){
                //echo "<pre>";
                //print_r($subcategory->getData());
                echo $subcategory->getName();
            }

        }   
        ?>
    <?php endif ?>

just after <?php $pos = $block->getPositioned(); ?>

Get source code at http://codepad.org/VwvnFZge

10
  • let me check and I will tell you is it working or not?
    – Dhaval
    Commented Jun 9, 2016 at 14:12
  • it's working perfect!!!
    – Dhaval
    Commented Jun 9, 2016 at 14:15
  • now if current category has subcategory and product both then how can I get both?and current category don't have subcategory but only product then how get product only?
    – Dhaval
    Commented Jun 9, 2016 at 14:17
  • At list.phtml ,products are displayed default. check it have a default logic ,pleaase check that <?php if (!$_productCollection->count()): ?> <div class="message info empty"><div><?php /* @escapeNotVerified */ echo __('We can\'t find products matching the selection.') ?></div></div> <?php else: ?>.... <?php endif; ?>
    – Amit Bera
    Commented Jun 9, 2016 at 14:32
  • So ,you need to merge my logic & existing logic at here ....If you want to show subcategory-Ue products at category page then please make Achor=yes
    – Amit Bera
    Commented Jun 9, 2016 at 14:37
3

Get subcategory of current category

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
$subcats = $category->getChildrenCategories();
$_helper = $this->helper('Magento\Catalog\Helper\Output');
?>
<ul class="sub-cat">
    <?php
    foreach ($subcats as $subcat) {
            $_category = $objectManager->create('Magento\Catalog\Model\Category')->load($subcat->getId());
            $_outputhelper = $this->helper('Magento\Catalog\Helper\Output');
            $subcaturl = $subcat->getUrl();            
            $_imgHtml = '';
            if ($_imgUrl = $_category->getImageUrl()) {
                $_imgHtml = '<img src="' . $_imgUrl . '" />';
                $_imgHtml = $_outputhelper->categoryAttribute($_category, $_imgHtml, 'image');
                /* @escapeNotVerified */
                echo '<li><a href="' . $subcaturl . '" title="' . $subcat->getName() . '">' . $_imgHtml . '</a></li>';
            }

    } ?>
</ul>
1

I have created an extension show subcategory in the current category. If you like this you can download it here https://github.com/magepow/magento-2-categories If you want get treeNode category you can code here https://github.com/magepow/magento-2-programmatically/blob/master/treeNode.php

0
if($this->getLayer()->getCurrentCategory()):
$subcategories=$this->getLayer()->getCurrentCategory()->getCategories($this->getLayer()->getCurrentCategory()->getId());
    /* count and check the category have any sub category */
    if($subcategories->count()>0){

        foreach($subcategories as $subcategory){
            //echo "<pre>";
            //print_r($subcategory->getData());
            echo $subcategory->getName();
        }

    }else{
        // category does not have any sub category
    }
else:
        // category does not have any sub category

endif;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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