0

ex: I have category list as follows:

Men
--top
---shoes
--bottom

I have current_category is shoes. so how to add class to this and all parent of this.

  • what you want to do for category, have you want current category's all parent category id? – Rakesh Jesadiya Feb 29 '16 at 7:03
5

Using below code you can get parents of current category

<?php 
  /* $block \Magento\Catalog\Block\Category\View */
foreach ($block->getCurrentCategory()->getParentCategories() as $parent) {
    echo $parent->getId();
}
?>
| improve this answer | |
  • This code for category is working and is bringing all the categories of that product but if i want to target the Men CategoryID only. how to do that. Not other subcategories. – Zeeshan Apr 24 '19 at 10:43
0

Create Block and extended \Magento\Catalog\Block\Category\View

For Example

namespace Companyname\Categorylist\Block\Category;

use Magento\Framework\View\Element\Template;

class View extends \Magento\Catalog\Block\Category\View
{
    protected $_coreRegistry = null;

    public function __construct(
        Template\Context $context, 
        \Magento\Catalog\Model\Layer\Resolver $layerResolver, 
        \Magento\Framework\Registry $registry, 
        \Magento\Catalog\Helper\Category $categoryHelper, 
        \Magento\Catalog\Model\CategoryFactory  $categoryFactory,
        array $data = array()) 
    {
        parent::__construct($context, $layerResolver, $registry, $categoryHelper,$data);
        $this->_categoryFactory = $categoryFactory;
     }

    public function getCategoryList()
    {
      $_category  = $this->getCurrentCategory();
      $collection = $this->_categoryFactory->create()->getCollection()->addAttributeToSelect('*')
              ->addAttributeToFilter('is_active', 1)
              ->setOrder('position', 'ASC')
              ->addIdFilter($_category->getChildren());
      return $collection;

    }
| improve this answer | |

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.