0

my products has a custom attribute named producing_days

enter image description here

and im pretty sure that it has a value in catalog_product_entity_varchar enter image description here

now im trying to access producing_days from quote item

/* $item is a quote item*/
$product = $item->getProduct();

$id = $product->getId(); //this works and return 3272
$producing_days = $product->getProducingDays(); //this returns null

i tried accessing it on stores 4, 14 and 19 and its all null in there. and it shouldnt be an issue because the "All Stores" has a value. any idea why i cant access the producing_days?

6
  • did you try $product->getAttributeText('producing_days'); ? – Pawan Feb 18 at 6:24
  • Or make sure you are getting custom attribute while var_dump i.e var_dump($product->getData()); – Pawan Feb 18 at 6:27
  • i already tried getAttributeText('producing_days'). and checking $product->getData() producing_days is not in there. do you an idea why its not being retrieved? it has an entry in the DB and also visible in the admin – mendz Feb 18 at 6:38
  • 1
    @Pawan found the solution. i needed to retrieve the product from Magento\Catalog\Api\ProductRepositoryInterface. and there i have all my attributes. which is weird because i already have the product instance from the quote item. but im already stock here for so long, so i guess this will do – mendz Feb 18 at 6:49
  • 1
    correct you need to load product by repo Or by factory! – Pawan Feb 18 at 6:50
0

The idea is that even if you can receive an instance of \Magento\Catalog\Model\Product as a return from calling other object's methods, only the repository model triggers all data loading.

0
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $productId = item->getProduct()->getId();
 $product = $objectManager->get('Magento\Catalog\Model\Product')->load($productId);
 $value = $product->getResource()->getAttribute('attribute_code')->getFrontend()->getValue($product);
      



  

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.