2

I have made a custom button on category edit page in admin. The button redirects to custom admin controller. I need to fetch the current category id in the controller.

2

You can get this by following code

$this->getRequest()->getParams();
  • Category id is not included in the params – Sufyan Khot Jul 11 at 7:20
  • 2
    you need to pass it as parameter on your button – Mohit Rane Jul 11 at 7:21
  • 1
    Thanks. I passed the id as a parameter to the controller. – Sufyan Khot Jul 11 at 7:41
  • glad i could help – Mohit Rane Jul 11 at 13:17
2

You can get by using Magento\Framework\Registry in the constructor of your block file.

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

    public function __construct(\Magento\Framework\Registry $registry) 
    {
        $this->_registry = $registry;
    }

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

Then you can get current category ID as below:

$this->getCurrentCategory->getId();
2

You have to pass the category id to the button .

Let assume that you have added the button using block class files like the https://magento.stackexchange.com/a/260643/4564

$categoryId = (int)$category->getId();

add

onclick="window.location.href='<?php $this->getUrl('frontentId/Controllername/ActioName',['category_id'=> $categoryId])?>';"

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.