Skip to content

Instantly share code, notes, and snippets.

@herveguetin
Last active December 25, 2015 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save herveguetin/6885689 to your computer and use it in GitHub Desktop.
Save herveguetin/6885689 to your computer and use it in GitHub Desktop.
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