How to create category attribute Programmatically in Magento 2?

We can create custom category attributes with text fields programmatically in Magento 2 using a simple module.

In our demo, we have created a simple text field attribute for a category to save the value and use it on the front end.

You can see a category attribute in the backend using Catalog -> Categories Page.

Click on Any category and you can see our custom attribute in General Section after the category name.

If you are interested to create a drop-down select category attribute, Learn More Continue reading “How to create category attribute Programmatically in Magento 2?”

How to get all Root Categories ids in Magento 2?

If your Website is setup with multiple store and each store has different Root Category assigned, In that case you need to refer below blog for getting all root category ids by programmatically.

Let’s consider your store with Two Root Category available.

First Native Magento Default Category which id is 2.
Second, create custom Root Category based on your requirement which id is 42.

Now How to get above root category id using coding in Magento 2, You can get root category id by below way,

<?php
namespace Rbj\Root\Block;
 
class RootCategoryIds extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        array $data = []
    ) {
        $this->_storeManager = $storeManager;
        parent::__construct($context, $data);
    }

    /**
     * Return ids of root categories as array
     *
     * @return array
     */
    public function getRootIds()
    {
        $ids = [\Magento\Catalog\Model\Category::TREE_ROOT_ID];
        foreach ($this->_storeManager->getGroups() as $store) {
            $ids[] = $store->getRootCategoryId();
        }
        return $ids;
    }

Call from template file,

echo "<pre>";print_r($this->getRootIds());

Output as array,

Array
(
    [0] => 1
    [1] => 2
    [2] => 42
)

In above case 1 is for Root Catalog id,
2 is your Default Category id.
42 is your custom created root category id.

Let’s explore get category collection per store by,
Get Category Collection of specific store

 

How to get Current Category details in magento 2?

Current Category collection can be found if you are in category page otherwise you can’t get current category collection using Registry. Refer below code snippet for getting current category collection in Magento 2,

<?php
namespace Rbj\Category\Block;

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

    /* $categoryId as category id */
    public function getCategory(){
        try {
            return $this->registry->registry('current_category');
        } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
            return ['response' => 'Category Not Found'];
        }
    }
}

Call From template file,

<?php
$getCategory = $block->getCategory();
if($getCategory) {
    echo $getCategory->getName();echo "<br>";
    echo $getCategory->getUrlKey();echo "<br>";
    echo $getCategory->getIsActive();echo "<br>";
    echo "<pre>";print_r($getCategory->getData());
} else {
    echo 'Current page is not a category page';
}

If you are in category page, you got Current category details by above code snippet otherwise display the message like, Current page is not a category page

You can check Get Sub Category details by parent id Magento.