magento load product change attribute value programmatically code example

Example: magento 2 how to update custom product attribute programatically

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$eavConfig = $objectManager->get('\Magento\Eav\Model\Config');
$attribute = $eavConfig->getAttribute('catalog_product', 'your_attribute_code');
$options = $attribute->getSource()->getAllOptions();
$value = '';
foreach($options as $option) {
    if($option['label'] == "your text"){
        $value = $option['value'];
    }
}
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager
$product = $objectManager->create('\Magento\Catalog\Model\Product');
$prod = $product->loadByAttribute('sku', 'sku');

$attr_code = 'your_attribute_code';
$prod->setCustomAttribute($attr_code, $value);
$prod->save();

Tags:

Misc Example