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


I have also tried:


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!


Issue seems to be in below code

$attributeOption = $attr->getSource()->getOptionText($value);

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

  • 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.