2

I have tried:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$p = $objectManager->get('\Magento\Catalog\Model\ProductRepository')->getById($pid);
$customattr = $p->getData('customattr');
echo print_r($p->getData('sku'));
echo print_r($customattr);

And tried

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$p = $objectManager->get('\Magento\Catalog\Model\Product')->load($pid);
$customattr = $p->getData('customattr');
echo print_r($p->getData('sku'));
echo print_r($customattr);

And I get the product's sku but customatrr is always always always null.

and yet if I do this:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$p = $objectManager->get('\Magento\Catalog\Model\ProductRepository')->getById($pid);
$attr = $p->getAttributes();
foreach($attr as $a){
    $t = $a->getName();
    if($t == "customattr") break;
}
$options = $a->getOptions();
foreach($options as $o){
    $l = $o->getLabel();
    print($l);
    $v = $o->getValue();
    print($v);
    echo "\n";
}

I get a list of all the attribute values that are possible for customattr but not what ones are selected. Using dependency injection doesn't help and they've made it nearly impossible to extend the constructor for the model I am using anyway so that's why I'm using the object manager. I guess I could do a helper but if it won't work here there's no reason it should magically work somewhere else, so why isn't this working?

Someone else had the same problem and said clearing the cache fixed it: https://stackoverflow.com/questions/34853553/loading-custom-attribute-value-in-controller-in-magento2 But I have ran magento cache:clean, and redeployed and re compiled several times and I always get the same result, I'm also in developer mode.

1
  • Meh, I finally found an answer after hours of digging, getAttributeText('customattr') works
    – user44406
    Nov 20, 2016 at 4:55

1 Answer 1

1

In case someone still needs it:

$product->getResource()
->getAttribute($action)
->getFrontend()
->getValue($product);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.