We can update the attribute parameter (update the label for the store) programmatically using the following script
I was able to update the value of the product attribute parameter with this script
function getOptionId($atributeCode,$optionValue){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$eaov = $resource->getTableName('eav_attribute_option_value');
$eao = $resource->getTableName('eav_attribute_option');
$ea= $resource->getTableName('eav_attribute');
$attributeId = $connection->fetchOne("SELECT 'attribute_id' FROM $ea WHERE 'attribute_code' = '$atributeCode' AND 'entity_type_id' = '4'");
$sql = "select * from $eao join $eaov on $eaov.option_id = $eao.option_id where $eaov.value='$optionValue' AND $eao.attribute_id='$attributeId'";
$result = $connection->fetchRow($sql);
return $optionId = isset($result['option_id']) ? $result['option_id']: null;
}
function creatOrGetId($atributeCode,$optionValue)
{
$optionId = getOptionId($atributeCode,$optionValue);
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
if(!$optionId ){
$attributeRepository = $objectManager->create('\Magento\Eav\Model\AttributeRepository');
$attributeId = $attributeRepository->get('catalog_product', $atributeCode)->getAttributeId();
$option = $objectManager->create('\Magento\Eav\Model\Entity\Attribute\Option');
$attributeOptionLabel = $objectManager->create('\Magento\Eav\Api\Data\AttributeOptionLabelInterface');
$attributeOptionManagement = $objectManager->create('\Magento\Eav\Api\AttributeOptionManagementInterface');
$option->setValue($optionValue);
$attributeOptionLabel->setStoreId(0);
$attributeOptionLabel->setLabel($optionValue);
$option->setLabel($attributeOptionLabel);
$option->setStoreLabels([$attributeOptionLabel]);
$option->setSortOrder(0);
$option->setIsDefault(false);
$attributeOptionManagement->add('catalog_product', $attributeId, $option);
return $optionId = getOptionId($atributeCode,$optionValue);
}else{
return $optionId;
}
}
$manufacturer = array['aa', 'bb', 'cc', 'dd']
$manufacturerId = creatOrGetId('manufacturer',$manufacturer);
$product->setManufacturer($manufacturerId);
source
share