2

Hi all I am using Magento 2 and I want to get the product Id on all pages. Now i amable to get the product id in product details pae (Product single page). but i am not getting those in product listing pages. it says calling undefined object like error message.

 <?php

namespace Vendor\module\Plugin;

class Product
{       
public function afterGetPrice(\Magento\Catalog\Model\Product $subject, $result)
{
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
    $resource = $objectManager->get('Magento\Framework\App\ResourceConnection');

    $product- = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
    // here this $product is seems like undefined only in product listing pages.

    return $result + 100;
}
}

Please see comment in my code, how can I get the product id here? Please help.

1 Answer 1

2

Logic is not right. You are using a plugin, so do not need to use registry current_product.

$subject can provide the product id using $subject->getId()

2
  • Yes that is perfect!
    – Sanjun Dev
    Sep 9, 2019 at 9:11
  • One more question, i got the ID, but the same way i am not getting the weight and price of the product like this. $subject->getWeight(); but it is coming in detail page of that product
    – Sanjun Dev
    Sep 9, 2019 at 17:08

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.