0

I'm going to set an attribute value to simple product programmatically. I have used below code:

$configurableAttributes = json_decode($simpleProduct['configurable_attribute'], true);
// array('color' => 4);     
foreach ($configurableAttributes as $key => $value) {
        //Color Attribute
        $attr = $product->getResource()->getAttribute($key);
        // Black
        $attributeOption = $attr->getSource()->getOptionText($value);

        $product->setData($key,$attributeOption);
        $product->save();
     }

I have also tried:

$product->setCustomAttribute($key,$attributeOption);

But when check in grid in Catalog. It show that the product have not attribute "Color" value. I'm wondering where I was wrong? Someone please help!

2

Issue seems to be in below code

$attributeOption = $attr->getSource()->getOptionText($value);
$product->setData($key,$attributeOption);

You are assigning option label to the product instead you should assign the option value. Try below code

$product->setData($key,$value);
  • Yes. It must be the id instead of the label. Thanks! – Nero Phung Feb 10 '18 at 8:39

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.