4

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 ?

3

My firm recently contacted Magento about this. According to them, there is no alternative way implemented yet.

  • Ooh.. Did they explain why ? – Kozame Mar 8 at 10:10
  • Not really. I use the registry class for now until they come up with an alternative. – Matthias_CompactCode Mar 8 at 10:13
  • Seem you're right : another team say me the same thing as you.. :( – Kozame Mar 8 at 10:51
1

try something like this:

$myBlock = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Catalog\Model\Session')->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/

  • I try this code, but it's not work for me.. I didn't understand this part : $this->catalogSession->getData('last_viewed_product_id'); because this data is never set in my project – Kozame Mar 8 at 10:10
  • Oh, sorry to hear that, I don't have magento 2.3 installed that's why didn't tried it myself – Shoaib Munir Mar 8 at 10:11
  • I think this is the only way of getting current product because Catalog Session stores in Magento\Catalog\Model\Session you need to do some digging on it – Shoaib Munir Mar 8 at 10:14
  • Thanks for the tips, I'll continue my search – Kozame Mar 8 at 10:18
  • 1
    @Kozame: In that way you get only the product id of the current product, you need to load the product using that id and then you will have the object – HelgeB Mar 8 at 10:33

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.