Magento – Product Attributes – Add Additional Label to Options

attributesmagento-1.9product

I am having trouble figuring this out.

I want to add an additional field, after the store labels on a product attribute's options. So when I go to Catalog -> Attributes – Manage Attributes, choose to create a new attribute and choose dropdown (or multiselect) as the attribute type, and go to Manage Labels / Options, I want an additional field to appear next to each option.

So it would be display like below…

Admin | Default Store View | Custom Field 1 | Custom Field 2

Can anyone help me with this? I have tried creating a custom module, and creating a new attribute programmatically, and adding a custom source model, but it is not adding the additional label field as I had hoped.

Here is the setup file…

    <?php
$this->startSetup();
//if you want to add the attribute to the category instead of the product change on the line below 'catalog_product' to 'catalog_category'
$this->addAttribute('catalog_product', 'buckle_builder_figure_list', array(
        'group'                => 'General',
        'type'              => 'text',
        'backend'           => 'eav/entity_attribute_backend_array',
        'frontend_input'    => '',
        'frontend'          => '',
        'label'             => 'Buckle Builder Figures',
        'input'             => 'multiselect',
        'class'             => '',
        'source'            => 'bucklebuilder/attribute_source_bucklebuilder',
        'global'             => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,//can be SCOPE_WEBSITE or SCOPE_STORE
        'visible'           => true,
        'used_in_product_listing' =>true, //can also be false
        'frontend_class'     => '',
        'required'          => false,//can be true
        'user_defined'      => true,
        'default'           => '',
        'searchable'        => false,//can be true
        'filterable'        => false,//can be true
        'comparable'        => false,//can be true
        'visible_on_front'  => false,//can be true
        'unique'            => false,
        'position'            => 60,//put any number here
    ));
$this->endSetup();

and here is the source model…

 <?php
class Unleaded_BuckleBuilder_Model_Attribute_Source_BuckleBuilder extends Mage_Eav_Model_Entity_Attribute_Source_Abstract{
    protected $_options = null;
    public function getAllOptions($withEmpty = false){
        if (is_null($this->_options)){
            $this->_options = array();
                       //$this->_options[] = array('label'=>'HERE GOES THE LABEL', 'value'=>'HERE GOES THE VALUE');
            //as example
            $this->_options[] = array('label'=> $this->__('Figure Name'), value=>1);
            $this->_options[] = array('label'=> $this->__('Figure SKU'), value=>2);
            $this->_options[] = array('label'=> $this->__('Figure Style'), value=>3);
        }
        $options = $this->_options;
        if ($withEmpty) {
            array_unshift($options, array('value'=>'', 'label'=>''));
        }
        return $options;
    }
    public function getOptionText($value)
    {
        $options = $this->getAllOptions(false);

        foreach ($options as $item) {
            if ($item['value'] == $value) {
                return $item['label'];
            }
        }
        return false;
    }
    public function getFlatColums()
    {
        $attributeCode = $this->getAttribute()->getAttributeCode();
        $column = array(
            'unsigned'  => false,
            'default'   => null,
            'extra'     => null
        );

        if (Mage::helper('core')->useDbCompatibleMode()) {
            $column['type']     = 'int(10)';
            $column['is_null']  = true;
        } else {
            $column['type']     = Varien_Db_Ddl_Table::TYPE_TEXT;
            $column['length']   = 10;
            $column['nullable'] = true;
            $column['comment']  = $attributeCode . ' column';
        }

        return array($attributeCode => $column);
    }
    public function getFlatUpdateSelect($store)
    {
        return Mage::getResourceModel('eav/entity_attribute')
            ->getFlatUpdateSelect($this->getAttribute(), $store);
    }
}

Best Answer

Here is one quick and dirty way if you wanna give a try.

You can add the required values of Custom Field 1, Custom Field 2 in 'Admin' label itself, may be using some comma(,) or pipeline(|) separators.

And in the frontend, you can easily get the Admin value for that option as:

<?php
// Add Somewhere in your catalog/product/view.phtml
$attributeCode  = 'some_attribute_code'; #EDIT
$separator      = '|'; #EDIT
$optionId       = $_product->getData($attributeCode);
$attributeId = Mage::getResourceModel('eav/entity_attribute')
        ->getIdByCode('catalog_product', $attributeCode);
$collection =Mage::getResourceModel('eav/entity_attribute_option_collection')
                 ->setPositionOrder('asc')
                 ->setAttributeFilter($attributeId)
                 ->setStoreFilter(0)
                 ->load();
$adminLabels = '';
foreach ($collection->toOptionArray() as $_option) {
    if ($_option['value'] == $optionId){
        $adminLabels =  $_option['label'];
        break;
    }
}
#Zend_Debug::dump($adminLabels);
if ( !empty($adminLabel)) {
    $adminLabelParts = explode($separator, $adminLabels);
    $adminLabel     = isset($adminLabelParts[0]) ? $adminLabelParts[0] : '';
    $custom1Label   = isset($adminLabelParts[1]) ? $adminLabelParts[1] : '';
    $custom2Label   = isset($adminLabelParts[2]) ? $adminLabelParts[2] : '';
    var_dump(
        $adminLabel,
        $custom1Label,
        $custom2Label
    );
}

Hope this helps you a bit.

Related Topic