1
use \Magento\Framework\Registry;
class Test extends Action
{ 
   protected $registry;

   public function __construct(
        Registry $registry
    )
    {
        $this->registry = $registry;
    }

  public function execute()
   {
     $proid = $this->registry->registry('current_product')->getId();
   }

 }

->getId()` showing Error "Fatal error: Uncaught Error: Call to a member function getId() on null"

Jugal Kishor
  • 1,225
  • 13
  • 36

1 Answers1

1

You can a registry variable value if that registry variable value already set.

In your code, at the controller, you have tried to fetch current_product variable value but on this page the registry variable does not set yet. So, it is not possible to get that variable value.

A Magento registry like a PHP Global variable which can access through the current page, if that variable value already set at that page.

Check out the blog how to use Magento 2 Registry & Register

EDIT

As you have submitted the form and posting the data from the product page to your URL then suggesting to send current product id as hidden input to this form So that you can get the product id to your custom action

Amit Bera
  • 77,456
  • 20
  • 123
  • 237