0

I am using an object manager to get the attribute value even though it's not recommended. I am getting NULL in product-object which causing error "GetattributeText() on NULL". Below is the code that so far I tried

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
        $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
        echo gettype($product);
        $slot_break = $product->getAttributeText('choose_slot_break');

when I echo gettype it shows "NULLObject". I have done so much googling before posting this question and have already tried almost all the solutions in the Magento stack exchange

The above code-sniffer is working well to show the value in a PHTML but here I am trying to get this inside a function which I needed for some calculation which is in a programmatically created dropdown attribute class which extends "\Magento\Eav\Model\Entity\Attribute\Source\AbstractSource ". Pardon me if this ain't met the qualification to ask as a question.

  • please check and update me if its work. – sarvesh Dineshkumar Patel Feb 10 at 11:02
  • Not sure what the context is in your case, but the problem you are facing is that the current product is not loaded in the $product variable and the call to get the attribute value fails with the error: "GetattributeText() on NULL". My best guess would be that this is either not happening on product page, or your code is getting executed before the "current_product" global variable is defined in Magento registry. – Zan Kolev Feb 10 at 11:40
  • @Aashik registry('current_product') this return an currently appear product means it's works on that page where only sigle product are see. Please check you page – Msquare Feb 10 at 14:48
  • I did not get like this can you show me error? also please screenshots for all – sarvesh Dineshkumar Patel Feb 11 at 6:08
  • or if it is live share me url please – sarvesh Dineshkumar Patel Feb 11 at 6:09
0

please check

   $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
   $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
   echo $choose_slot_break = $product->getData('choose_slot_break');

hope it works.

| improve this answer | |
  • same error "Call to a member function getData() on null in /var/www/html/magento2sample/app/code/Today/Slot/Model/Config/Source/Options.php:65" – Aashik Feb 10 at 12:02
  • I did not get like this can you show me error? also please screenshots for all – sarvesh Dineshkumar Patel Feb 11 at 6:08

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.