1

I have the following code where I am trying to change a 'color' attribute option label:

<?php

require_once 'app/Mage.php';

Mage::app();

$attributeId = 92; // color

/** @var $attribute \Mage_Catalog_Model_Resource_Eav_Attribute */
$attribute = Mage::getModel('catalog/resource_eav_attribute');
$attribute->load($attributeId);

// Register attribute for the easy use of the existing block
Mage::register('entity_attribute', $attribute);

// Use options tab from edit to easy get values    
/** @var $block \Mage_Adminhtml_Block_Catalog_Product_Attribute_Edit_Tab_Options */
$block = Mage::app()->getLayout()
        ->getBlockSingleton('adminhtml/catalog_product_attribute_edit_tab_options');

// Get current labels, easy...

$label = $block->getLabelValues();

// Build options array

$options = array('value' => array());
foreach ($block->getOptionValues() as $optionValue) {
    $storeOption = array();
    foreach (Mage::app()->getStores() as $store) {
        $key = 'store' . $store->getId();
        $storeOption[$store->getId()] = $optionValue->$key;
    }
    $options['value'][] = $storeOption;
}

// Show output

var_dump($label);
var_dump($options);

// Update value 0 for store 2

$options['value'][0][2] = 'New value';

// Update and save data

$attribute->setFrontendLabel($label)->setOption($options)->save();

=====================================================================

When I am launching the script, I get the following Fatal Error:

Fatal error: Uncaught exception 'Mage_Core_Exception' with message 'Default option value is not defined' in C:\xampp\htdocs\mag\app\Mage.php:595 Stack trace: #0 C:\xampp\htdocs\mag\app\code\core\Mage\Eav\Model\Resource\Entity\Attribute.php(353): Mage::throwException('Default option ...') #1 C:\xampp\htdocs\mag\app\code\core\Mage\Eav\Model\Resource\Entity\Attribute.php(198): Mage_Eav_Model_Resource_Entity_Attribute->_saveOption(Object(Mage_Catalog_Model_Resource_Eav_Attribute))
#2 C:\xampp\htdocs\mag\app\code\core\Mage\Catalog\Model\Resource\Attribute.php(61): Mage_Eav_Model_Resource_Entity_Attribute->_afterSave(Object(Mage_Catalog_Model_Resource_Eav_Attribute))
#3 C:\xampp\htdocs\mag\app\code\core\Mage\Core\Model\Resource\Db\Abstract.php(463): Mage_Catalog_Model_Resource_Attribute->_afterSave(Object(Mage_Catalog_Model_Resource_Eav_Attribute))
#4 C:\xampp\htdocs\mag\app\code\core\Mage\Core\Model\Abstract.php(318): Mage_Core_Model_Resource_Db_Abstract->save(Object(Mage_Catalog_Model_Resource_Eav_Attribute))
#5 C:\xampp\htdo in C:\xampp\htdocs\mag\app\Mage.php on line 595

Does anyone know why?

Thanks a lot!

===========================================================================

Here is how the var_dump() of the $options variable looks like:

array(1) {
  ["value"]=>
  array(2) {
    [0]=>
    array(6) {
      [1]=>
      string(0) ""
      [6]=>
      string(5) "GREEN"
      [2]=>
      string(9) "New GREEN"
      [3]=>
      string(5) "GRUEN"
      [4]=>
      string(5) "VERDE"
      [5]=>
      string(5) "VERDE"
    }
    [1]=>
    array(6) {
      [1]=>
      string(0) ""
      [6]=>
      string(4) "BLUE"
      [2]=>
      string(4) "BLEU"
      [3]=>
      string(4) "BLAU"
      [4]=>
      string(3) "BLU"
      [5]=>
      string(4) "AZUL"
    }
  }
}
2

The exception is thrown in the method Mage_Eav_Model_Resource_Entity_Attribute::_saveOption by this code:

if (!isset($values[0])) {
    Mage::throwException(Mage::helper('eav')->__('Default option value is not defined'));
}

This means that $values[0] is not set

$values comes from this code in the same method foreach ($option['value'] as $optionId => $values) {

Apparently in your code $options['value'][Some numeric key here][0] is not set. See how the $options array looks like before passing it to the setOption method (last line in your code).

  • Thanks a lot marius! $options['value'][Some numeric key here][0] was the admin value that I do not want to change. I want it to remain the same and only update the other options for each StoreView. – Supersonic Mar 4 '15 at 13:53
  • How do you set the $options['value'][Some numeric key here][0] to what it actually is in the Database? Many thanks! – Supersonic Mar 4 '15 at 14:01
  • How do you set the $options['value'][Some numeric key here][0] to what it actually is in the Database? Do you have to save it again in order to update the other ones? Thank you very much! Promiss, no more fishing ;-). – Supersonic Mar 4 '15 at 14:10
  • Post in your question how the $values var looks like. – Marius Mar 4 '15 at 14:19
  • both of your array elements are missing the 0 key. that's the option label for the admin store. You should try to replace this line foreach (Mage::app()->getStores() as $store) { with foreach (Mage::app()->getStores(true) as $store) { – Marius Mar 4 '15 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.