Magento Asked by Kozame on December 28, 2021
I would like to get my current product in my phtml.
I saw lot of examples with registry in the Block file, but now, in M2.3, it’s deprecated.
Someone have an idea how to do ?
From this article: https://www.atwix.com/magento-2/alternatives-for-deprecated-registry-class-magento-2-3/
Starting from Magento 2.3 the Registry class that is (or was) used by a lot of developers and extension vendors is declared to be deprecated. In class comments it’s told to use service classes or data providers, but no examples provided.
Here an example to get the product from a phtml using a ViewModel.
<?php
declare(strict_types=1);
namespace NamespaceModuleViewModel;
use MagentoCatalogApiDataProductInterface;
use MagentoCatalogApiProductRepositoryInterface;
use MagentoCatalogModelSession as CatalogSession;
use MagentoFrameworkExceptionNoSuchEntityException;
use MagentoFrameworkViewElementBlockArgumentInterface;
class Product implements ArgumentInterface
{
/**
* @var CatalogSession
*/
private $catalogSession;
/**
* @var ProductRepositoryInterface
*/
private $productRepository;
/**
* Current Product
*
* @var ProductInterface
*/
private $currentProduct;
/**
* @param CatalogSession $catalogSession
* @param ProductRepositoryInterface $productRepository
*/
public function __construct(
CatalogSession $catalogSession,
ProductRepositoryInterface $productRepository
)
{
$this->catalogSession = $catalogSession;
$this->productRepository = $productRepository;
}
/**
* @return ProductInterface
* @throws NoSuchEntityException
*/
public function getProduct(): ProductInterface
{
if (!isset($this->currentProduct)) {
$productId = $this->getProductId();
if ($productId) {
$this->currentProduct = $this->productRepository->getById($productId);
}
}
return $this->currentProduct;
}
/**
* @return string
*/
public function getProductId(): string
{
return $this->catalogSession->getData('last_viewed_product_id');
}
}
Answered by Danilo Argentiero on December 28, 2021
To get the current product:
use block class: MagentoCatalogBlockProductView
Get product using: $block->getProduct() in your phtml or use the class and get in the block file.
Hope it Helps.Happy coding!!
Answered by Vijay R on December 28, 2021
I recently moved to Magento 2.3.2, and I had the same question
After reading this allready pointed out article (thanks https://magento.stackexchange.com/users/31910/shoaib-munir)
https://www.atwix.com/magento-2/alternatives-for-deprecated-registry-class-magento-2-3/
I found this implementation by @Vinai Kopp (https://github.com/Vinai)
https://github.com/Vinai/module-current-product-example
Basically, this trigger an observer based catalog_controller_product_init_after
event
for grabbing the actual displayed product and push the result into a custom registry
So after that, you can have it in your Block like the below code, or use ViewModel
as suggested by @Vinai
<?php
namespace VendorModuleBlock;
use MagentoFrameworkViewElementTemplate;
use VendorModuleRegistryCurrentProduct;
class YourBlock extends Template
{
/**
* @var CurrentProduct
*/
private $_currentProduct;
public function __construct(
TemplateContext $context,
...
CurrentProduct $currentProduct,
array $data)
{
$this->_currentProduct = $currentProduct;
parent::__construct($context, $data);
}
public function getCurrentProduct()
{
return $this->getProductId();
}
/**
* @return mixed
*/
private function getProductId()
{
return $this->_currentProduct->get()->getId();
}
}
you could also return the full product, but not sure, that is really needed
public function getCurrentProduct()
{
return $this->_currentProduct->get();
}
Hope this helps,
Answered by ffab on December 28, 2021
try something like this:
$myBlock = MagentoFrameworkAppObjectManager::getInstance()->get('MagentoCatalogModelSession')->getData('last_viewed_product_id');
I found an article for depreciated registry in Magento 2.3. It is getting category from catalog session, I don't know if my code works or not but from this you will have a path where you need to do some debugging
Reference: https://www.atwix.com/magento-2/alternatives-for-deprecated-registry-class-magento-2-3/
Answered by Shoaib Munir on December 28, 2021
My firm recently contacted Magento about this. According to them, there is no alternative way implemented yet.
Answered by Matthias_CompactCode on December 28, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP