0
votes

I have successfully written data from product attribute value options custom field to a custom table. Here's how I did:

app/code/local/MyModule/Control/etc/config.xml

<events>
    <catalog_entity_attribute_save_before>
        <observers>
            <control_catalog_product_attribute_save_before>
                <type>singleton</type>
                <class>control/catalog_product_observer</class>
                <method>saveAttributeValueDescription</method>
            </control_catalog_product_attribute_save_before>
        </observers>
    </catalog_entity_attribute_save_before>
<events>

<entities>
    <productattributesvalues>
        <table>mymodule_product_attributes_values_description</table>
    </productattributesvalues>
</entities>

app/code/local/MyModule/Control/Model/Catalog/Product/Observer.php

public function saveAttributeValueDescription($observer)
{
    $attribute = $observer->getEvent()->getAttribute();
    $attribute_id = $attribute->getAttributeId();
    $option = $attribute->getOption();
    $description = $option["description"];
    $valueDescriptionModel = Mage::getModel('control/productattributesvalues');
    $deleteCollection = $valueDescriptionModel->getCollection()->addFieldToFilter('attribute_id', array('eq' => $attribute_id));
    foreach($deleteCollection as $coll)
        $valueDescriptionModel->load($coll->getDescriptionId())->delete();
    $valueDescriptionModel->unsetData();
    foreach ($description as $option_id => $option_description) {
        if ("" !== $option_description) {
            $valueDescriptionModel
                ->setAttributeId($attribute_id)
                ->setOptionId($option_id)
                ->setDescription($option_description)
                ->save()
                ->unsetData();
        }
    }
    return $this;
}

app/code/local/MyModule/Control/Model/Mysql4/Productattributesvalues.php

class MyModule_Control_Model_Mysql4_Productattributesvalues extends Mage_Core_Model_Mysql4_Abstract
{
    public function _construct()
    {
        $this->_init('control/productattributesvalues', 'description_id');
    }
}

app/code/local/MyModule/Control/Model/Mysql4/Productattributesvalues/Collection.php

class MyModule_Control_Model_Mysql4_Productattributesvalues_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
    public function _construct()
    {
        parent::_construct();
        $this->_init('control/productattributesvalues');
    }
}

Custom table

CREATE TABLE IF NOT EXISTS `mymodule_product_attributes_values_description` (
    `description_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
    `attribute_id` int(11) unsigned NOT NULL,
    `option_id` int(11) unsigned NOT NULL,
    `description` text NOT NULL,
    PRIMARY KEY (`description_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

catalog/product/attribute/options.phtml

// line 88 added:
<th><?php echo Mage::helper('catalog')->__('Description') ?></th>
// line 101:
<td class="a-left"><input class="input-text" type="text" name="option[description][{{id}}]" value="{{description}}" /></td>
// line 126:
'<td><input class="input-text" type="text" name="option[description][{{id}}]" value="{{description}}" /><\/td>'+

And now I have no clue how to read that data in catalog/product/attribute/options.phtml and in frontend product view page...

Shoud I do the observer route as well? If so, how to do that? Can anyone point me to the right direction?

Thanks!

1

1 Answers

0
votes

Got it! :)

app/code/local/MyModule/Control/Model/Productattributesvalues.php

public function getAttributeValueDescription($attributeId, $optionId)
{
  $descriptionCollection = $this->getCollection()
    ->addFieldToFilter('attribute_id', array('eq' => $attributeId))
    ->addFieldToFilter('option_id',    array('eq' => $optionId));

  $descriptionArray = $descriptionCollection->getData();

  if (isset($descriptionArray[0]["description"]))
    return $descriptionArray[0]["description"];
  else
    return;
}

catalog/product/attribute/options.phtml

// line 90 added:
<?php $optionValues = $this->getOptionValues() ?>

<?php foreach ($optionValues as &$val): ?>
  <?php $val->_data["description"] = Mage::getModel('control/productattributesvalues')->getAttributeValueDescription($this->getRequest()->getParam('attribute_id'), $val->id) ?>
<?php endforeach ?>
// replaced all $this->getOptionValues() for $optionValues