Magento – Magento 2: Set value of Yes/No custom attribute programmatically

custom-attributesmagento2

I created a custom Yes/No attribute for products and assigned it to default attribute group. I have to save value of Yes/No attribute programmatically.
is it possible? if Yes, How can I manage product value programmatically?

Best Answer

Use this if you dont want to save the whole product. Its much faster.

$productObj->setData('attribute_code', 1); //$value = 1 for yes and $value = 0 for No.
$productObj->getResource()->saveAttribute($productObj, 'attribute_code');