Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Update Magento's single attribute option value
<?php
/**
* Données à renseigner
*/
$attributeCode = 'pack_main_color'; // Le code de l'attribut dont on veut modifier les options
$optionId = 4; // option_id dont on veut modifier la valeur
$newValueForOption = 'Nouvelle valeur'; // La nouvelle valeur pour cette option_id
/**
* Logique
*/
$attribute = Mage::getModel('catalog/product')->getResource()->getAttribute($attributeCode);
$optionsCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setAttributeFilter($attribute->getId())
->setStoreFilter($attribute->getStoreId())
->toArray();
$updatedOptionsValues = array();
$updatedOptionsPositions = array();
foreach($optionsCollection['items'] as $option) {
if($option['option_id'] == $optionId) {
$updatedOptionsValues[$option['option_id']][$attribute->getStoreId() ? $attribute->getStoreId() : 0] = $newValueForOption;
}
else {
$updatedOptionsValues[$option['option_id']][$attribute->getStoreId() ? $attribute->getStoreId() : 0] = $option['value'];
}
$updatedOptionsPositions[$option['option_id']] = $option['sort_order'];
}
$attributeOptionsData['option']['value'] = $updatedOptionsValues;
$attributeOptionsData['option']['order'] = $updatedOptionsPositions;
$attribute->addData($attributeOptionsData);
$attribute->save();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment