How can i get product id on product page without registry? Such as class registry is deprecated in magento 2.3
2 Answers
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. Commented Apr 14, 2019 at 10:16
-
-
You are right, but, you created class and extends it from
AbstractProduct
that is using registry Commented Apr 14, 2019 at 10:12 -
extension updated plz check again, this time I am using product view block in the layout Commented Apr 14, 2019 at 16:33
-
Yes, but block you are using, use registry to get a product. Commented Apr 16, 2019 at 12:14