1

I want to display subcategoies and their products on parent category page
I tried this code on ../Magento_Catalog/templates/category/products.phtml but it shows all product of the parent category on each subcategories

<?php $category = $block->getCurrentCategory(); ?>
<?php $subcategories = $category->getChildrenCategories(); ?>
<?php $_helper = $this->helper('Magento\Catalog\Helper\Output'); ?>
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); ?>
<?php if(count($subcategories) > 0): ?>
    <div class="products wrapper grid products-grid">
        <ol class="products list items product-items">
            <?php foreach($category->getChildrenCategories() as $subcategory): ?>
                <?php $subcategory = $objectManager->create('Magento\Catalog\Model\Category')->load($subcategory->getId()); ?>
                <li class="item  col-xs-12">
                    <div class="product-item-info">
                        <?php if ($_imgUrl = $subcategory->getImageUrl()): ?>
                            <a href="<?= $subcategory->getUrl() ?>" class="product photo product-item-photo">
                                <span class="product-image-container">
                                    <?php $_imgHtml = '<img src="' . $_imgUrl . '" />'; ?>
                                    <?php echo $_imgHtml = $_helper->categoryAttribute($subcategory, $_imgHtml, 'image'); ?>
                                </span>
                            </a>
                        <?php endif; ?>
                        <div class="product details product-item-details">
                            <strong class="product name product-item-name">
                                <a class="product-item-link" href="<?= $subcategory->getUrl() ?>"><?= $subcategory->getName() ?></a>
                            </strong>

                            <?= $block->getProductListHtml() ?>

                        </div>
                    </div>
                </li>
            <?php endforeach; ?>
        </ol>
    </div>
<?php else: ?>
    <?php if (!$block->isContentMode() || $block->isMixedMode()): ?>
        <?= $block->getProductListHtml() ?>
    <?php endif; ?>
<?php endif; ?>


help please

1
  • Have you find the solution?
    – aravind
    May 23, 2019 at 13:26

1 Answer 1

1

Create a Block function in custom module and call that function from your phtml

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\CategoryFactory $categoryFactory,
    \Magento\Framework\Registry $registry,
    array $data = []
) {
    $this->_productCollectionFactory = $productCollectionFactory;
    $this->_categoryFactory = $categoryFactory;
    $this->registry = $registry;
    parent::__construct($context, $data);
}

public function getCurrentCategory()
{
    return $this->registry->registry('current_category');
}

public function getCatgeoryProducts($categoryIds) {
    if(!empty($categoryIds)){
         $subCategory =  $this->_categoryFactory->create()->load($categoryId);
         $subCategoryIds[] = $subCategory->getChildren();
     }

    if(!empty($subCategoryIds)){
        $subCategories = implode(",",$subCategoryIds);
    }

    if(count($subCategoryIds) > 0) {
        $collection = $this->_productCollectionFactory->create()
                       ->addAttributeToSelect('*');

        $collection->addCategoriesFilter(['in' => $subCategories]);
        $collection->addAttributeToFilter('visibility',\Magento\Catalog\Model\Product\Visibility::VISIBILITY_IN_SEARCH);
        $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
    }
    return $collection;
}

From Your Phtml file,

$currentCatgeory = $block->getCurrentCategory();
 $currentCategoryId = $currentCategory->getId();
$subcategories = $block->getCatgeoryProducts($currentCategoryId); 
echo '<pre>';print_r($subcategories->getData());
5
  • it does not work for me, blank result May 23, 2019 at 16:16
  • did u get any errors?
    – aravind
    May 24, 2019 at 4:27
  • I have developer mode enabled and so can put a copy of the error. It is: Call to a member function getData() on null
    – user28163
    Jan 26, 2022 at 5:52
  • @user28163 Does the categories has subcategories associated?
    – aravind
    Jan 27, 2022 at 10:19
  • @aravind Yes it does
    – user28163
    Jan 27, 2022 at 14:02

Your Answer

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

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