Magento 2 get current category Id using registry
You need to use this in your constructer
/** * @var Magento\Framework\Registry */ private $registry;
public function __construct(\Magento\Framework\Registry $registry)
{ $this->registry = $registry; }
then you can get the current category by this code
$category = $this->registry->registry('current_category');