Posted on

Get all categories on same level as current category – Magento 2.4

We use the following helper to get the current category:

class Category extends MagentoFrameworkViewElementTemplate
{
protected $_registry;

public function __construct(
MagentoBackendBlockTemplateContext $context,
MagentoFrameworkRegistry $registry,
array $data = []
)
{
$this->_registry = $registry;
parent::__construct($context, $data);
}

public function _prepareLayout()
{
return parent::_prepareLayout();
}

public function getCurrentCategory()
{
return $this->_registry->registry(‘current_category’);
}

public function getCurrentProduct()
{
return $this->_registry->registry(‘current_product’);
}

}

In our template file that works perfect using $category = $block->getCurrentCategory() and also to get all child categories $subcategories = $category->getChildrenCategories();

But now we want to also load all categories that are on the same level as the current category. So not the child categories, but the ones that are on the same level. And we can also get the parent categories by $category->getParentCategories();.

So how can we get all categories that are on the same level?

Leave a Reply

Your email address will not be published.