I've created a new module in order to add a column to the product list displayed in the admin section of Magento. The column is displaying an ID and I want it to display the name (from another table) belonging to that ID.
app/code/local/Mypackage/Customgrid/etc/Config.xml
<?xml version="1.0"?>
<config>
<modules>
<Mypackage_Customgrid>
<version>0.4.0</version>
</Mypackage_Customgrid>
</modules>
<global>
<models>
<mypackage_customgrid>
<class>Mypackage_Customgrid_Model</class>
</mypackage_customgrid>
</models>
</global>
<adminhtml>
<events>
<core_block_abstract_prepare_layout_before>
<observers>
<customgrid_column_append>
<type>model</type>
<class>Mypackage_Customgrid_Model_Observer</class>
<method>appendCustomColumn</method>
</customgrid_column_append>
</observers>
</core_block_abstract_prepare_layout_before>
</events>
</adminhtml>
</config>
app/code/local/Mypackage/Customgrid/Model/Observer.php
class Mypackage_Customgrid_Model_Observer extends Varien_Event_Observer {
public function appendCustomColumn(Varien_Event_Observer $observer) {
$block = $observer->getBlock();
if (!isset($block)) {
return $this;
}
if ($block->getType() == 'adminhtml/catalog_product_grid') {
/* @var $block Mage_Adminhtml_Block_Customer_Grid */
$block->addColumnAfter('udropship_vendor', array(
'header' => 'Vendor',
'index' => 'udropship_vendor',
'type' => 'text',
), 'visibility');
}
}
}
As you can see in the above code, I'm using the uDropship extension. The above code display the vendor ID related to each product, but I would like this column to display the vendor name related to each product.
I've tried to change the collection fetch in the file app/code/core/Adminhtml/Block/Catalog/Product/Grid.php
(only for test purpose, I'll overwrite this file if I find how to do that). But I don't understand how to get the name value from the database.
Btw, the table schema in which the data I want to display is stored looks like something like that
table: magentoudropship_vendor
vendor_id
vendor_name
[...]
Is anyone capable of guiding me through this ? Anything which can help me to understand how to manipulate the database is welcome.
Best Answer
app/code/core/Adminhtml/Block/Catalog/Product/Grid.php
is the right place to start. What you need to do is rewriteMage_Adminhtml_Block_Catalog_Product_Grid::_prepareCollection
and joinmagentoudropship_vendor
, so that you have access to thevendor_name
for the grid. I'm assuming you havevendor_id
as an attribute to the product (correct me if I'm wrong).Additionally, I would add the column to the product list grid in this rewrite. See
Mage_Adminhtml_Block_Catalog_Product_Grid::_prepareColumns
. This is where you can add additional columns.You need something like...
Alias for the
getTableName()
method is found in yourconfig.xml
of the uDropShip extension. e.g.model_alias/table_name
. Copy and paste the<models>...<models>
if you're still not sure.