3

I have an attribute with the input type Dropdown named "Example Attribute" and two Store Views.

This attribute has a few options, but only the Admin values are set:

enter image description here

How can I programmically modify the values for each Store View? For example let's say I'd like to add the name of the Store View to each value:

enter image description here

I have found some very complicated answers which basically propose to completely overwrite all the values, but none did exactly what I need. I have tried various snippets of code that I found but none have worked for me. I've also looked through Magento's core classes but I was unable to find anything related to my problem.

Amit Bera
77.8k21 gold badges127 silver badges240 bronze badges
asked Mar 28, 2015 at 13:55

3 Answers 3

3

While all of the above answers are correct, I have also found that if you want to just update one option, you can use a shorthand version:

No need to update all the array of options, just set the one you like, like this

Here is an example adding an option to an attribute with values:

//option id = 42
//store value => beer
//default value => Beer
//2nd store value => Bière (french)
//load attribute as in other examples
$attribute = $attribute_model->load($attribute_code);
$option_id = 42; // NUMERIC VALUE
$opt_default_name='beer';
$opt_default_store = 'Beer';
$opt_2nd_store = 'Biére';
$attribute->setData('option', array('value'=> array(
 $option_id => array ($opt_default_name, $opt_default_store, $opt_2nd_store));
)));
$attribute->save();

This will just modify the option "beer" for the default and second store (ok. the example is quick, just check the others for correctly getting store IDs..)

Amit Bera
77.8k21 gold badges127 silver badges240 bronze badges
answered Aug 22, 2015 at 17:42
1
7

What I have come up with is that it seems that in order to update any attribute option value, you'll have to prepare a complete dataset with all the option values for the attribute, including all the Store View specific labels. In this dataset you can make your changes and then you'll have to completely replace the existing attribute options with the newly prepared dataset.

Needless to say this seems like an overly complex solution, so if anybody can come up with a simpler one please post it!

// Load the required attribute
$attributeCode = "your_attribute_code";
$attributeId = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter($attributeCode)->getFirstItem()->getAttributeId();
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
// Get the Admin Store View (default) attribute options
$attributeOptions = Mage::getResourceModel('eav/entity_attribute_option_collection')
 ->setAttributeFilter($attributeId)
 ->setStoreFilter(0)
 ->setPositionOrder()
 ->load()
 ->toOptionArray();
// Save the attribute options as an array where the key is the option ID and the value is the label
$attributeOptionsNew = array();
foreach($attributeOptions AS $attributeOption) {
 $attributeOptionsNew[$attributeOption['value']] = $attributeOption['label'];
}
$attributeOptions = $attributeOptionsNew;
// Get the attribute options for each Store View
$storeViewsAttributeOptions = array();
foreach(Mage::app()->getStores() AS $storeViewId => $storeView) {
 $storeViewAttributeOptions = Mage::getResourceModel('eav/entity_attribute_option_collection')
 ->setAttributeFilter($attributeId)
 ->setStoreFilter($storeViewId)
 ->setPositionOrder()
 ->load()
 ->toOptionArray();
 // Save the attribute options as an array where the key is the option ID and the value is the label
 $storeViewAttributeOptionsNew = array();
 foreach($storeViewAttributeOptions AS $attributeOption) {
 $storeViewAttributeOptionsNew[$attributeOption['value']] = $attributeOption['label'];
 }
 $storeViewAttributeOptions = $storeViewAttributeOptionsNew;
 // Save the attribute options for this Store View
 $storeViewsAttributeOptions[$storeViewId] = $storeViewAttributeOptions;
}
// Prepare the array where the updated complete attribute options will be saved
$attributeOptionsUpdated = array();
// Loop through the attribute options from the Admin Store View
foreach($attributeOptions AS $attributeOptionId => $attributeOptionValue) {
 // Prepare the array where the updated attribute option will be saved
 $attributeOptionNew = array();
 /*
 *
 * This is where you'd modify the Admin Store specific (default) value for the attribute option
 *
 */
 $attributeOptionNew[0] = $attributeOptionValue;
 // Loop through the Store Views to check if their attribute option value for the current attribute option is different
 // to the one from the Admin Store View
 foreach(Mage::app()->getStores() AS $storeViewId => $storeView) {
 $storeViewAttributeOptionValue = $storeViewsAttributeOptions[$storeViewId][$attributeOptionId];
 // If the attribute option value for the current Store View is different than the one for the Admin Store View,
 // we don't want to overwrite it as it has been set manually, and we trust that it has been updated correctly
 // by the Admin
 if($attributeOptionValue !== $storeViewAttributeOptionValue) {
 $attributeOptionNew[$storeViewId] = $storeViewAttributeOptionValue;
 } else {
 /*
 *
 * This is where you'd modify the Store View specific values for the attribute option
 *
 */
 $attributeOptionNew[$storeViewId] = '';
 }
 }
 // Update the array that contains the complete set of updated attribute options
 $attributeOptionsUpdated[$attributeOptionId] = $attributeOptionNew;
}
// Prepare the updated data set with the attribute options
$attributeOptionsNew = array(
 'option' => array(
 'value' => $attributeOptionsUpdated
 )
);
// Update the attribute options by completely replacing the existing values with the
// dataset that we have just prepared
try {
 $attribute->addData($attributeOptionsUpdated)->save();
} catch (Exception $e) {
 die(var_dump($e));
}
answered Mar 28, 2015 at 14:56
6

Get store ids

$storeIds=array(); 
$storeIds=array(0,1,2,3); 

get all option it value and label of admin store

$attributeFrontend = Mage::getModel('eav/config')->getAttribute('catalog_product', 'color');
if ($attributeFrontend->usesSource()) {
$items = $attributeFrontend->getSource()->getAllOptions(false,true);
}

load attribute by attribute id

$attribute = Mage::getModel('catalog/resource_eav_attribute');
$attribute->load(92);

Init some temp arrays in which assign value and it all parameters

$value=array(); 
$order=array();
$delete=array();
$options = array();

Create new array and save value

if ($attribute->usesSource()) {
 foreach ($items as $eachOption) {
 /* set Option value for store */ 
 $optionValuetionId=$eachOption['value'];
 foreach($storeIds as $sr):
 if($sr==0):
 $value[$optionValuetionId][$sr]=$eachOption['label'];
 else:
 $value[$optionValuetionId][$sr]="BERA";
 endif;
 endforeach;
 /* end of Option value for store */ 
 $order[$optionValuetionId]=11;
 /*$delete[$optionValuetionId] set for null for not delete */ 
 $delete[$optionValuetionId]='';
 }
/* NEW VaLUES */
$data=array('value'=>$value,'delete'=>$delete,'order'=>$order); 
$SetData=array('option'=>$data);
$attribute->addData($SetData); 
$attribute->save();
}

Array format is like below and has been taken from Mage_Adminhtml_Catalog_Product_AttributeController saveAction print_r($data['option']).

2015年03月28日T18:31:29+00:00 DEBUG (7): Array
(
 [value] => Array
 (
 [20] => Array
 (
 [0] => Black
 [1] => Black
 [2] => 
 [3] => 
 )
 [27] => Array
 (
 [0] => Blue
 [1] => Blue
 [2] => 
 [3] => 
 )
 [221] => Array
 (
 [0] => Brown
 [1] => Brown
 [2] => 
 [3] => 
 )
 [17] => Array
 (
 [0] => Charcoal
 [1] => Charcoal
 [2] => 
 [3] => 
 )
 [24] => Array
 (
 [0] => Green
 [1] => Green
 [2] => 
 [3] => 
 )
 [12] => Array
 (
 [0] => Grey
 [1] => Grey
 [2] => 
 [3] => 
 )
 [26] => Array
 (
 [0] => Indigo
 [1] => Indigo
 [2] => 
 [3] => 
 )
 [13] => Array
 (
 [0] => Ivory
 [1] => Ivory
 [2] => 
 [3] => 
 )
 [25] => Array
 (
 [0] => Khaki
 [1] => Khaki
 [2] => 
 [3] => 
 )
 [226] => Array
 (
 [0] => Oatmeal
 [1] => Oatmeal
 [2] => 
 [3] => 
 )
 [19] => Array
 (
 [0] => Orange
 [1] => Cognac
 [2] => 
 [3] => 
 )
 [21] => Array
 (
 [0] => Pink
 [1] => Pink
 [2] => 
 [3] => 
 )
 [18] => Array
 (
 [0] => Purple
 [1] => Purple
 [2] => aaaa
 [3] => 
 )
 [28] => Array
 (
 [0] => Red
 [1] => Red
 [2] => 
 [3] => 
 )
 [16] => Array
 (
 [0] => Royal Blue
 [1] => Royal Blue
 [2] => hhhhhhh
 [3] => 
 )
 [15] => Array
 (
 [0] => Silver
 [1] => Silver
 [2] => 
 [3] => 
 )
 [14] => Array
 (
 [0] => Taupe
 [1] => Taupe
 [2] => 
 [3] => 
 )
 [22] => Array
 (
 [0] => White
 [1] => White
 [2] => 
 [3] => 
 )
 [23] => Array
 (
 [0] => Yellow
 [1] => Yellow
 [2] => amit
 [3] => 
 )
 )
 [order] => Array
 (
 [20] => 0
 [27] => 0
 [221] => 0
 [17] => 0
 [24] => 0
 [12] => 0
 [26] => 0
 [13] => 0
 [25] => 0
 [226] => 0
 [19] => 0
 [21] => 0
 [18] => 0
 [28] => 0
 [16] => 0
 [15] => 0
 [14] => 0
 [22] => 0
 [23] => 0
 )
 [delete] => Array
 (
 [20] => 
 [27] => 
 [221] => 
 [17] => 
 [24] => 
 [12] => 
 [26] => 
 [13] => 
 [25] => 
 [226] => 
 [19] => 
 [21] => 
 [18] => 
 [28] => 
 [16] => 
 [15] => 
 [14] => 
 [22] => 
 [23] => 
 )
)
answered Mar 28, 2015 at 19:12
2
  • Thanks mate. It seems you also had to pre-populate the $SetData array with ALL the option values for all Store Views, even in the case that you'd only wanted to update a single attribute option value for just one Store View, right? Commented Mar 28, 2015 at 19:18
  • According to the var_dump in saveAction you need to set delete value to 1 for it to delete. Empty string is to keep 1 is to delete. Commented Jan 16, 2016 at 11:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.