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.
1 Answer 1
app/code/core/Adminhtml/Block/Catalog/Product/Grid.php is the right place to start. What you need to do is rewrite Mage_Adminhtml_Block_Catalog_Product_Grid::_prepareCollection and join magentoudropship_vendor, so that you have access to the vendor_name for the grid. I'm assuming you have vendor_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...
protected function _prepareCollection()
{
$resource = Mage::getSingleton('core/resource');
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('sku')
->addAttributeToSelect('name')
->addAttributeToSelect('attribute_set_id')
->addAttributeToSelect('type_id');
$collection->getSelect()
->join(
array('v' => $resource->getTableName('...')), // Insert approprirate alias to get `magentoudropship_vendor`
'main_table.vendor_id = v.vendor_id',
array('vendor_name') // List of wanted columns from the custom table
);
...
}
Alias for the getTableName() method is found in your config.xml of the uDropShip extension. e.g. model_alias/table_name. Copy and paste the <models>...<models> if you're still not sure.
-
The block holds the collection, so you could also call
$block->getCollection()in the observer, right?7ochem– 7ochem2015年03月16日 13:21:47 +00:00Commented Mar 16, 2015 at 13:21 -
Thanks for you response. I just have a few questions: 1. where does $resource come from ? I don't have this variable in the Grid.php 2. you talk about the alias to get the database ? is it the database name or should I use something else ? And you're right, the udropship_vendor used in my custom column is a product's attribute.chris– chris2015年03月16日 13:27:39 +00:00Commented Mar 16, 2015 at 13:27
-
@7ochem, I extend
_prepareCollectionwhen I modify grids because you can do everything in one rewrite. I have used other approaches in modifying grids (observer, XML update, etc.) but I find the block rewrite to be the best.laketuna– laketuna2015年03月16日 13:31:43 +00:00Commented Mar 16, 2015 at 13:31 -
@christophe, updated the ansnwer.laketuna– laketuna2015年03月16日 13:36:48 +00:00Commented Mar 16, 2015 at 13:36
-
This works now, I'll continue to look for information about database manipulation with Magento though. Thank you.chris– chris2015年03月16日 15:28:05 +00:00Commented Mar 16, 2015 at 15:28
Explore related questions
See similar questions with these tags.