Answers for "magento 2 how to update custom product attribute programatically"

-1

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();
Posted by: Guest on December-06-2020

Code answers related to "magento 2 how to update custom product attribute programatically"

Browse Popular Code Answers by Language