Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Dynamic values in dropdown on products in the backend
 
ddcas
Jr. Member
 
Total Posts:  7
Joined:  2012-04-23
 

Hi,

I\’m developing a module that integrates with an existing third party remote API. I want to create the functionality such that when adding a new product, one of the product attributes is a drop down list, where the options in the drop down are populated via a remote procedure call.

I\’m happy with the code necessary to get the JSON RPC working, but I\’ve no idea how to populate a product attribute dropdown from code. Any help with this would be very much appreciated.

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Avatar
Total Posts:  2205
Joined:  2009-11-18
Bucharest, Romania
 

Hello
You’re in luck. Magento supports this kind of attributes up to some degree.
You need to create a custom source model for an attribute.
Here is the clean way to do it.
You need to create your own extension that adds a new attribute.
Let’s call that extension Easylife_Custom. (Replace Easylife with your company name and Custom with your attribute code in all the files and filenames that follow).
You need to create the following files:
app/code/local/Easylife/Custom/Model/Attribute/Source/Custom.php with this content;

class Easylife_Custom_Model_Attribute_Source_Custom extends Mage_Eav_Model_Entity_Attribute_Source_Abstract{
    
protected $_options null;
    public function 
getAllOptions($withEmpty false){
        
if (is_null($this->_options)){
            $this
->_options = array();
                        
//here you call your extenral API and build an array with possible values like this
                       //$this->_options[] = array('label'=>'HERE GOES THE LABEL', 'value'=>'HERE GOES THE VALUE');
        
}
        $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_SMALLINT;
            
$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);
    
}
}
Now you need to add the attribute to products:
app/code/local/Easylife/Custom/sql/easylife_custom_setup/mysql4-install-0.0.1.php
You can change most of the values in this one. Don’t change the source.
<?php
$this
->startSetup();
$this->addAttribute('catalog_product''custom', array(
        
'group'                => 'General',
        
'type'              => 'int',
        
'backend'           => '',
        
'frontend_input'    => '',
        
'frontend'          => '',
        
'label'             => 'Custom',
        
'input'             => 'select',
        
'class'             => '',
        
'source'            => 'custom/attribute_source_custom',
        
'global'             => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        
'visible'           => true,
        
'used_in_product_listing' =>true,
        
'frontend_class'     => '',
        
'required'          => false,
        
'user_defined'      => true,
        
'default'           => '',
        
'searchable'        => false,
        
'filterable'        => false,
        
'comparable'        => false,
        
'visible_on_front'  => false,
        
'unique'            => false,
        
'position'            => 60,
    ));
$this->endSetup();
Now you need the config file of your extension:
app/code/local/Easylife/Custom/etc/config.xml
<?xml version="1.0"?>
<config>
    <
modules>
        <
Easylife_Custom>
            <
version>0.0.1</version>
        </
Easylife_Custom>
    </
modules>
    <global>
        <
models>
            <
custom>
                <class>
Easylife_Custom_Model</class>
            </
custom>
        </
models>
        <
resources>
            <
easylife_custom_setup>
                <
setup>
                    <
module>Easylife_Custom</module>
                    <class>
Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
                </
setup>
                <
connection>
                    <use>
core_setup</use>
                </
connection>
            </
easylife_custom_setup>
        </
resources>
    </global>
</
config>

Now you need to tell magento to activate your module:
app/etc/modules/Easylife_Custom.xml

<?xml version="1.0"?>
<config>
    <
modules>
        <
Easylife_Custom>
            <
active>true</active>
            <
codePool>local</codePool>
            <
depends>
                <
Mage_Catalog />
            </
depends>
        </
Easylife_Custom>
    </
modules>
</
config>

I hope I did not spell something wrong.

Good luck.
Marius.

 
Magento Community Magento Community
Magento Community
Magento Community
 
ddcas
Jr. Member
 
Total Posts:  7
Joined:  2012-04-23
 

Wow, thank you so much for the comprehensive reply. I’ll see if I can make this work.

 
Magento Community Magento Community
Magento Community
Magento Community
 
contattiweb
Jr. Member
 
Total Posts:  2
Joined:  2013-05-15
 

hi,
I created the module step by step, but I did not understand one thing, how do you activate the module? is self-installing? you have to run the file mysql-install-0.0.1.php? how do you activate the module? if creating files only nothing happens

thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
contattiweb
Jr. Member
 
Total Posts:  2
Joined:  2013-05-15
 

don’t work :(

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top