Add custom select dropdown in magento grid

Add custom select dropdown in magento grid

July 22, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Add custom select dropdown in magento grid

Adding a Custom Select Dropdown to Magento Grid

You’re facing an issue while trying to add a custom dropdown to your Magento grid using the addColumn method of the collection class. The code you provided seems correct in terms of syntax and parameters, but it isn’t functioning as expected because you’re not populating the column with actual data from your database or API.

In order to display a select dropdown on your grid, you need to map your API data to the appropriate columns in the grid. Let me guide you through this process step by step:

Step 1: Fetch Data from the API

Assuming you have an API that returns JSON data containing ‘First’ and ‘Second’, you first need to fetch this data before populating the grid.

$apiUrl = 'https://your-api-url.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$apiResponse = curl_exec($ch);
curl_close($ch);

// Parse the JSON response
$data = json_decode($apiResponse, true);

Step 2: Populate the Grid Column

Now that you have fetched data from the API, you can map it to your grid column using a custom function. Create a new file app/code/local/Namespace/Modulename/Model/Observer.php (replace Namespace and Modulename with your actual namespace and module name) and add the following code:

class Namespace_Modulename_Model_Observer
{
    public function addCustomDropdownToGrid($observer)
    {
        $block = $observer->getBlock();
        if (!($block instanceof Mage_Adminhtml_Block_Catalog_Product_Grid)) {
            return;
        }

        $collection = $block->getCollection();

        // Assuming your API returns data for 'First' and 'Second'
        foreach ($data as $item) {
            $productId = $item['id'];
            $product = Mage::getModel('catalog/product')->load($productId);

            $dropdownValue['firstvalue'] = $item['First'];
            $dropdownValue['secondvalue'] = $item['Second'];

            // Add the dropdown value to the collection
            $collection->addAttributeToSelect(array(
                'dropdown' => array(
                    'attribute_code'  => 'dropdown',
                    'data' => $dropdownValue,
                ),
            ));
        }
    }
}

Step 3: Implement the Observer

In app/code/local/Namespace/Modulename/etc/config.xml, add the following configuration to trigger our observer:

<config>
    <global>
        <events>
            <catalog_product_collection_load_after>
                <observers>
                    <namespace_modulename_observer>
                        <type>singleton</type>
                        <class>Namespace_Modulename_Model_Observer</class>
                        <method>addCustomDropdownToGrid</method>
                    </namespace_modulename_observer>
                </observers>
            </catalog_product_collection_load_after>
        </events>
    </global>
</config>

Conclusion

By following these steps, you should be able to display a custom select dropdown on your Magento grid using data fetched from an API. Make sure to replace the placeholders (Namespace, Modulename, your-api-url.com) with your actual values.

Remember that this is a basic example and may need adjustments based on your specific requirements and the structure of your API response. Always refer to the official Magento Developer Documentation for detailed guides and best practices.

If you’re still facing issues or have any further questions, feel free to explore our comprehensive Magento development resources at LaravelCompany. We provide in-depth tutorials, code examples, and expert consultations to help you master Magento development.

Database API Extensions Admin

Our network