3

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?

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

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

  • let me check and I will tell you is it working or not? – Dhaval Jun 9 '16 at 14:12
  • it's working perfect!!! – Dhaval Jun 9 '16 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 Jun 9 '16 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 Jun 9 '16 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 Jun 9 '16 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>
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, privacy policy and cookie policy

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