1

By Default if we see at category page (listing page) it's show product collection of category but I want to change it.

What I want to do is below.

For ex: I have category test and it has subcategory demo1 , demo2 , demo3 , demo4 and all these demo subcategory have products.

SO if I open test category then I want to show all demo subcategory with it's name , image and product count not test category products.

After that when I click on any subcategory like demo1 then it's show product collection of demo1 category.

How can I get this?

1

Here is little help but yet you will need to override template apart from this and also will need little changes in your code as per your requirement,

$_helper    = $this->helper('Magento\Catalog\Helper\Output');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
$childcategories = $category->getChildrenCategories();

foreach($childcategories as $child)
{
    $cat = $objectManager->create('Magento\Catalog\Model\Category')->load($child->getId()); 


     if ($_imgUrl = $cat->getImageUrl())
             {
        $_imgHtml = '<div class="category-image"><img src="' . $_imgUrl . '" alt="' . $block->escapeHtml($cat->getName()) . '" title="' . $block->escapeHtml($cat->getName()) . '" class="image" /></div>';
        $_imgHtml = $_helper->categoryAttribute($cat, $_imgHtml, 'image');
        /* @escapeNotVerified */ echo $_imgHtml;
}
    echo $cat->getName();

I have added above code in vendor/magento/module-catalog/view/frontend/templates/category/products.phtml for testing purpose. Strongly do not recommend to change core files that was just testing purpose. You will need to override file and then needs to check if category is parent/top level only then above code gets executed else not.

Also currently I have not added product count but with little code you can explore that too.

Hope this helps!

  • Thanks @RJ07 your code is working perfect. now if category don't have sub-category then I want to show category product. How can I do that? – Dhaval Jul 6 '16 at 4:59
  • Glad to know :) In above code you can get count($childcategories) , if you get count > 0 let the rest of the custom code execute else you can continue default code. – Rushvi Jul 6 '16 at 5:02
  • How can I get sub-category url? – Dhaval Jul 6 '16 at 5:08
  • And can you explain me $_helper = $this->helper('Magento\Catalog\Helper\Output'); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category'); because so many times I see above code but not understand. – Dhaval Jul 6 '16 at 5:11
  • 1
    $helper object is in code to call helper function of categoryAttribute for the purpose to display image that I have used in code. And object manager creates instance of class and then you can retrieve current category using that instance from registry. So As I did. You can also refer this link alanstorm.com/magento_2_object_manager for concept purpose. – Rushvi Jul 6 '16 at 5:16

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.