How can i get product id on product page without registry? Such as class registry is deprecated in magento 2.3

  • can you post your code? what di you try,? you can use ProductRepository – fmsthird Apr 14 '19 at 2:01

You can use a view model.

Vinal kopp, write a blog where he has to create a custom registry but note that registry, not Magento generic registry like global variable.

  • The product is loaded by \Magento\Catalog\Helper\Product::initProduct. This method dispatches the event catalog_controller_product_init_after.
  • In the event observer RegisterCurrentProductObserver the product is set on a shared instance of the class \VinaiKopp\CurrentProductExample\Registry\CurrentProduct.
  • A new template block is added to the product detail page with layout XML. In the XML the block is configured to receive a view model, an instance of the class \VinaiKopp\CurrentProductExample\ViewModel\CurrentProductExampleViewModel.
  • The view model uses the shared Registry\CurrentProduct instance to retrieve the current product. This makes it a registry but without the downsides of the global core registry.
  • The template retrieves the view model from the block and renders the required product value

More detail at https://github.com/Vinai/module-current-product-example

  • It's not proper for me, but it is the best decision among the proposed. – Petro Chaikivskyi Apr 14 '19 at 10:16
  • Credit goes to vinai koop – Amit Bera Apr 14 '19 at 10:18

I have created a demo extension for you.

Link >> https://github.com/magento1993/ProductId

enter image description here

  • You are right, but, you created class and extends it from AbstractProduct that is using registry – Petro Chaikivskyi Apr 14 '19 at 10:12
  • extension updated plz check again, this time I am using product view block in the layout – Biren Patel Apr 14 '19 at 16:33
  • Yes, but block you are using, use registry to get a product. – Petro Chaikivskyi Apr 16 '19 at 12:14

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.