3

I want to create a product attribute where the values are enumerated string codes like "value_1", "value_2", "value_2" so that I can match them cleanly without having to rely on an ID or a user facing label, which are both subject to change.

I created a "select" type attribute via installer and used my own source class with a simple getAllOptions() method. It shows up when editing products. Saving the value takes. The value gets stored in catalog_product_entity_varchar as it should.

Here is an example version of the custom source class:

<?php
class My_Catalog_Model_Source_Billingfrequency extends 
    Mage_Eav_Model_Entity_Attribute_Source_Abstract
{

    public function getAllOptions()
    {
        if (!$this->_options) {
            $this->_options = array(
                array(
                    'label' => 'Monthly',
                    'value' => 'monthly',
                ),
                array(
                    'label' => 'Annually',
                    'value' => 'annually',
                ),
            );
        }
        return $this->_options;
    }
}
?>

The problem is that Magento does not push this value to flat tables. I also ensured that used_in_product_listing was on and reindexed.

Does anyone know if it's possible, using Magento product attributes to create an enumerated attribute without having to use the standard Magento ID/label values like you would set in the admin?

1 Answer 1

0

You need to add this to your custom source model

/**
 * Retrieve flat column definition
 *
 * @access public
 * @return array
 */
public function getFlatColums()
{
    $attributeCode = $this->getAttribute()->getAttributeCode();
    $column = array(
        'default'   => null,
        'extra'     => null
    );
    if (Mage::helper('core')->useDbCompatibleMode()) {
        $column['type']     = 'varchar';
        $column['is_null']  = true;
    } else {
        $column['type']     = Varien_Db_Ddl_Table::TYPE_TEXT;
        $column['nullable'] = true;
        $column['comment']  = 'Billing frequency column';
    }
    return array($attributeCode => $column);
}

/**
 * Retrieve Select for update attribute value in flat table
 *
 * @access public
 * @param   int $store
 * @return  Varien_Db_Select|null
 */
public function getFlatUpdateSelect($store)
{
    return Mage::getResourceModel('eav/entity_attribute_option')
        ->getFlatUpdateSelect($this->getAttribute(), $store, false);
}

Your Answer

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

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