Magento 1.9 – Problem Saving Attribute with Modified Option Label

eavmagento-1.9

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"
    }
  }
}

Best Answer

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).

Related Topic