0

my products have 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 I'm 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 it's all null in there. and it shouldn't be an issue because the "All Stores" has value. any idea why I can't access the producing_days?

6
  • did you try $product->getAttributeText('producing_days'); ?
    – Pawan
    Feb 18, 2021 at 6:24
  • Or make sure you are getting custom attribute while var_dump i.e var_dump($product->getData());
    – Pawan
    Feb 18, 2021 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, 2021 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, 2021 at 6:49
  • 1
    correct you need to load product by repo Or by factory!
    – Pawan
    Feb 18, 2021 at 6:50

3 Answers 3

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);
      



  
0

You need to make your product attribute available for your quote item by adding catalog_attributes.xml.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd">
    <group name="quote_item">
        <attribute name="producing_days"/>
    </group>
</config>

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.