3

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

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

2 Answers 2

1

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

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

I have created a demo extension for you.

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

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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