1

Iam using this extension for displaying left menu category sidebar https://github.com/Sebwite/magento2-category-sidebar. For main categories its working fine , i enabled flat category as well but its not showing sub categories .this function returns empty array $childCategories = $this->getSubcategories($category);

   public function getChildCategoryView($category, $html = '', $level = 1)
{
    // Check if category has children
    if ( $category->hasChildren() )
    {

        $childCategories = $this->getSubcategories($category);

        if ( count($childCategories) > 0 )
        {

            $html .= '<ul class="o-list o-list--unstyled">';

            // Loop through children categories
            foreach ( $childCategories as $childCategory )
            {

                $html .= '<li class="level' . $level . ($this->isActive($childCategory) ? ' active' : '') . '">';
                $html .= '<a href="' . $this->getCategoryUrl($childCategory) . '" title="' . $childCategory->getName() . '" class="' . ($this->isActive($childCategory) ? 'is-active' : '') . '">' . $childCategory->getName() . '</a>';

                if ( $childCategory->hasChildren() )
                {
                    if ( $this->isActive($childCategory) )
                    {
                        $html .= '<span class="expanded"><i class="fa fa-minus"></i></span>';
                    }
                    else
                    {
                        $html .= '<span class="expand"><i class="fa fa-plus"></i></span>';
                    }
                }

                if ( $childCategory->hasChildren() )
                {
                    $html .= $this->getChildCategoryView($childCategory, '', ($level + 1));
                }

                $html .= '</li>';
            }
            $html .= '</ul>';
        }
    }

    return $html;
}

/**
 * Retrieve subcategories
 *
 * @param $category
 *
 * @return array
 */

      public function getSubcategories($category)
{
    if ( $this->categoryFlatConfig->isFlatEnabled() &&   $category->getUseFlatResource() )
    {
        return (array)$category->getChildrenNodes();
    }

    return $category->getChildren();
}

can anyone help me

2

you have to just create block and inside template file call function,

namespace Vendor\Module\Block;
class Customcategory extends \Magento\Catalog\Block\Product\ListProduct
{
    protected $categoryFactory;
    protected $_catalogLayer;

    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,    
        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
        \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
        \Magento\Catalog\Model\Layer\Resolver $layerResolver,
        \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
        \Magento\Framework\Url\Helper\Data $urlHelper,     
        array $data = []
    ) {
        $this->categoryFactory = $categoryFactory; 
        $this->_catalogLayer = $layerResolver->get();        
        $this->storeManager = $context->getStoreManager();
        parent::__construct(
            $context,
            $postDataHelper,
            $layerResolver,
            $categoryRepository,
            $urlHelper,
            $data
        );
    }

    public function getCurrentCategoryData(){
        $categories = $this->_catalogLayer->getCurrentCategory()->getChildrenCategories();
        return $categories;
    }

    public function getCategoryData($id){
        return $category = $this->categoryFactory->create()->load($id);       
    }
}

Inside phtml file,

<?php
    $currentCategory = $this->getCurrentCategoryData();
?>
<?php
    foreach($currentCategory as $categoryDetails){
        $categoryId = $categoryDetails->getId();

        $categoryObj = $this->getCategoryData($categoryId);   
        $categoryName = $categoryObj->getName();
    }
?>
  • what is mistake in above code – Nadh Oct 19 '16 at 13:05
  • Hi @Rakesh its woking fine but in category list page its not showing main categories – Nadh Oct 20 '16 at 7:32

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.