$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$productID = 12; // Product ID

$option_label = 'XL'; // atttribute option label

$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productID);

$attribute = $product->getResource()->getAttribute('size'); // Get attribute 
details by code 

if ($attribute->usesSource()) {

    $option_id = $attribute->getSource()->getOptionId($option_label); // Get Option id using option label



  • 1
    yes, you are using correct method. For drop down attribute we need to first fetch the option id using the label and then use that id to assign the value to the product. Your code should work but if it is not working then you can check your attribute's scope in admin. I also faced the same issue once and setting the scope as global for the attirbute, the above code worked. Earlier the attribute's scope was set to store view. Sep 10, 2021 at 8:15


Your Answer

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

Browse other questions tagged or ask your own question.