5

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);
    }
}
7
  • Why? You could create dummy store views but depending on what problem you want to solve there is probably a more elegant solution. Commented Jul 8, 2015 at 14:42
  • Because I want to do it the proper way, although at this point, I am about to abandoned my efforts and go with dummy stores. Commented Jul 8, 2015 at 14:57
  • The question was, why do you need that? Unfortunately what you are trying is not intended, so there is no proper way Commented Jul 8, 2015 at 15:01
  • It's for an integration with Liquid Pixels. Each product is a template which can be customized with different emblems, and custom text. The attribute will contain a description, category and sku of the figure (stored in Liquid Pixels). Here is the site and the custom products, step 2-3 deals with this. Currently it is just data stored in a static DB, I wanted to make it an attribute that the client can update when they need to add new emblems. Commented Jul 8, 2015 at 15:12
  • 2
    If this is a configurable product, why not store this information in the corresponding simple products instead? Commented Jul 8, 2015 at 15:15

1 Answer 1

2

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.

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.