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.

3 Answers 3

3

You can get this by following code

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

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();
3

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 and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.