1

Sorry, I know the title sounds vague and could be misleading, but here is what I have and what I would like to achieve.

I have successfully created an extension which manages Product Distributors. The table for this holds data such as Distributor ID (entity_id), Distributor Name, Email Address, Contact Number, Contact Person, Postal Address and Comments.

What I would like to achieve now is to have a new dropdown list under the General Tab on Product Management, allowing the admin user to choose the associated Distributor for that product (Text will be distributor_name while Value will be entity_id). I realise that the Product Management screen is made up mainly of Attributes, hence me now asking how to achieve this?

2

Presuming you have set up all your Models successfully. You can do this using your own source model.

Create a file at Yourmodule/Namespace/Model/Source/Distributor.php

Inside this you need to extend Mage_Eav_Model_Entity_Attribute_Source_Abstract

You will then want to create your Options array for the dropdown, we do this by implementing the getAllOptions() method like so:

public function getAllOptions()
{
        $distributors = Mage::getModel('mymodule/distributors')->getCollection()
            ->setOrder('distributor_name', 'ASC');

        $options = array(
            NULL => '- Please Select -'
        );

        foreach ($distributors as $distributor) {
            $options [$distributor->getId()] = $distributor->getDistributorName();
        }

        return $options;
}

This will construct your option array used for the select box when setting the product attribute in the admin.

Along with getAllOptons() you will want to implement getFlatColums() and also getFlatUpdateSelect($store) in order for Magento to include the attribute in the flat table. You can do this like so:

public function getFlatColums()
    {
        $attributeCode = $this->getAttribute()->getAttributeCode();
        $column = array(
            'unsigned'  => false,
            'default'   => null,
            'extra'     => null
        );

        if (Mage::helper('core')->useDbCompatibleMode()) {
            $column['type']     = 'VARCHAR';
            $column['is_null']  = true;
        } else {
            $column['type']     = Varien_Db_Ddl_Table::TYPE_VARCHAR;
            $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've created your source model for your attribute you will want to install it. We can do this with an install script, if you have created your own custom entity i am assuming you are handy with Install Scripts so i'll leave that to you. Install it as you would any other product attribute, however when you do make sure you set the following:

'input' => 'select', //frontend input

'source => 'mymodule/source_distributor' //your source model

Once installed, make sure your attribute is assigned to the correct attribute set, then edit the product and you should see your dropdown, populated with all your distributors.

For a multiselect, simply change the input specified in the install script to multiselect.

If you need any further reading on this subject take a look at this Inchoo post on creating a custom attribute source type: Creating a custom attribute source

Hope this helps. Any questions, just ask.

Josh.

|improve this answer|||||
  • Incredible! Thank you @JoshCarter. I was going to settle on some convoluted method of setting up a Distributor attribute from the Attribute management section and then manually managing them! – maGz Apr 19 '16 at 14:34

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.