0

We use the following helper to get the current category:

class Category extends \Magento\Framework\View\Element\Template
{
    protected $_registry;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Framework\Registry $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.

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

1

You can simply use below 2 lines once you have fetched current category

$pcategory = $category->getParentCategory();
$subcats = $pcategory->getChildrenCategories();
2
  • Thanks a lot! Works perfect
    – JGeer
    Jan 21 at 11:17
  • Your Welcome, Happy to Help :) Jan 21 at 13:55
1

Try This

app/code/VendoreName/ModuleName/Block

CustomBlock.php

<?php

namespace VendoreName\ModuleName\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;

class CustomBlock extends Template
{

    protected $registry;

    public function __construct(
        Context $context,
        \Magento\Framework\Registry $registry,
        array $data = []
    ) {
        $this->registry = $registry;
        parent::__construct($context, $data);
    }

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

    public function getLevelCurrentCategory()
    {
        $childCategories = [];
        if ($this->getCurrentCategory()) {
            if ($this->getCurrentCategory()->getParentCategories()) {
                foreach ($this->getCurrentCategory()->getParentCategories() as $parent) {
                    if ($parent->getChildrenCategories()) {
                        foreach ($parent->getChildrenCategories() as $childKey => $childVal) {
                            if ($this->getCurrentCategory()->getLevel() == $childVal->getLevel()) {
                                $childCategories[] = $childVal->getData();
                            }
                        }
                    }
                }
            }
        }
        return $childCategories;
    }
}

app/code/VendoreName/ModuleName/view/frontend/templates

categorylist.phtml

<?php
echo "<pre>";
$childCat =  $block->getLevelCurrentCategory();
if(count($childCat))
echo "*** Same Categories Name ***<br/><br/>";
foreach ($childCat as $key => $value) {
    echo $key.") ".$value['name']."<br/>";
}
echo "</pre>";
?>

app/code/VendoreName/ModuleName/view/frontend/layout

catalog_category_view.xml

<?xml version="1.0"?>

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="content">
            <block class="VendoreName\ModuleName\Block\CustomBlock" name="product.list.customblock" template="VendoreName_ModuleName::categorylist.phtml"/>
        </referenceContainer>
    </body>
</page>

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.