3

1> I have created a custom attribute with dropdown as "product_brand" from Admin end

2> I want to add values for this option box, from my other module which in named as "brand"

 For e.g. Currently my Brand Module has three values "Reebok", "Armani", "Adidas"

Now when I will add  a new product, I want these values to come in my custom attribute "Product_Brand"

3> I have tried following Upgrade script, by overloading Mage/Catalog in local.

4> This is the code for upgrade script

<?php   
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();

$attributeCode = 'product_brand';
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', $attributeCode);
$count=0;
$_brand = Mage::getModel('brand/brand')->getCollection();
        $brandNames =array();

        foreach ($_brand as $brand) {

            $brandNames[] = $brand->getName();
        }

if($attribute->getId() && $attribute->getFrontendInput()=='select') {
    foreach($_brand as $brand){
        $option['value']['option'.$count][0] = $brand->getName();
        $count++;
    }

    $option['attribute_id'] = $attribute->getId();
    $option['value']        =  $brandNames;
    $installer->addAttributeOption($option);
}

//OR
/*
if($attribute->getId() && $attribute->getFrontendInput()=='select') {
    $option['attribute_id'] = $attribute->getId();
    $option['value']['r'][0] = 'Red';
    $option['value']['b'][1] = 'Black';
    $option['value']['y'][2] = 'Yellow';
    $installer->addAttributeOption($option);
}*/

$installer->endSetup();
?>
1

I have two functions which you can use here. Its not an upgrade scripts but two simple functions which can be used in any file. I use it and they work fine, it may help you.

$_brand = Mage::getModel('brand/brand')->getCollection();
$brandNames =array();

foreach ($_brand as $brand) {

    $brandNames[] = $brand->getName();
}
$this->addAttributeValues('product_brand',$brandNames);

function addAttributeValues($attributeCode, $attributeValues)
{
    $i = 0;
    $attribute_model = Mage::getModel('eav/entity_attribute');
    $attribute_code  = $attribute_model->getIdByCode('catalog_product', $attributeCode);
    $attribute       = $attribute_model->load($attribute_code);
    foreach($attributeValues as $attributeValue){
        if (!$this->attributeValueExists($attributeCode, $attributeValue)) {
            $value['option'] = array(
            $attributeValue
            );
            $order['option'] = $i;
            $result          = array(
                'value' => $value,
                'order' => $order
            );
            $attribute->setData('option', $result);
            $attribute->save();
        }
        $i++;
    }
}

function attributeValueExists($arg_attribute, $arg_value)
{
    $attribute_model         = Mage::getModel('eav/entity_attribute');
    $attribute_options_model = Mage::getModel('eav/entity_attribute_source_table');
    $attribute_code          = $attribute_model->getIdByCode('catalog_product', $arg_attribute);
    $attribute               = $attribute_model->load($attribute_code);
    $attribute_table         = $attribute_options_model->setAttribute($attribute);
    $options                 = $attribute_options_model->getAllOptions(false);
    foreach ($options as $option) {
        if ($option['label'] == $arg_value) {
            return $option['value'];
        }
    }
    return false;
}
  • Yes this is also a way. But still it needs to be dynamically changed as per Brand module. Means if I add new Brand to my Brand module, then those changes should get reflected here – Mrunmay Deswandikar Jan 31 '17 at 9:46
  • 1
    For that you need to create an observer on brand module save event. Put above functions in observer. That would be much easier as you only have to pass newly added values in $attributeValues. – Jaimin Sutariya Jan 31 '17 at 9:49
  • 1
    Yes ! I have implemented it in another way. But thanks for idea, I am accepting it. I am doing like this now, when I save my Brand, on the same action I will add this code and add the same values to Attribute at same time. – Mrunmay Deswandikar Jan 31 '17 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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