Display Data in Custom Column in Magento 1.9 Admin – How To

adminadminhtmlcustomgridmagento-1.9

I've already asked a question about that here:How to Fetch / Add a Custom Column in Product List Grid In Admin
Unfortunately, I didn't see that due to this solution my products name don't appear anymore.

I changed my code, and now, the collection fetch the data correctly (I printed the query and executed it in the mysql console directly), but the data aren't displayed inside the custom column for I don't know which reason.

Here's my module code:

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>
       <blocks>
            <adminhtml>
                <rewrite>
                     <catalog_product_grid>Mypackage_Customgrid_Block_Adminhtml_Catalog_Product_Grid</catalog_product_grid>
                </rewrite>
            </adminhtml>
        </blocks>
    </global>
</config>

app/code/local/Mypackage/Customgrid/Block/Adminhtml/Catalog/Product/Grid.php:

class Mypackage_Customgrid_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid {

    protected function _prepareCollection() {
        parent::_prepareCollection();
        $collection = $this->getCollection();

        $resource = Mage::getSingleton('core/resource');
        $collection->getSelect()
        ->join(
            array('v' => $resource->getTableName('udropship/vendor')),
            'e.entity_id = v.vendor_id',
            array('vendor_name')
        );

        $collection->printLogQuery(true);

        $this->setCollection($collection);
        $this->getCollection()->addWebsiteNamesToResult();

        return $this;
    }

    protected function _prepareColumns() {

        $this->addColumnAfter('vendor_name', array(
                'header'    => Mage::helper('catalog')->__('Vendor'),
                'index'     => 'vendor_name',
                'type'      => 'text',
            ), 'visibility');

        return parent::_prepareColumns();
    }
}

Any idea on what's happening here ? Thank you !

Best Answer

I've finally did it that way:

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>
        <blocks>
            <adminhtml>
                <rewrite>
                    <catalog_product_grid>Mypackage_Customgrid_Block_Adminhtml_Catalog_Product_Grid</catalog_product_grid>
                </rewrite>
            </adminhtml>
        </blocks>
    </global>
</config>

app/code/local/Mypackage/Customgrid/Block/Adminhtml/Catalog/Product/Grid.php:

class Mypackage_Customgrid_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid
{
    protected function _prepareCollection()
    {
        parent::_prepareCollection();
        $collection = $this->getCollection();

        $collection->addAttributeToSelect('udropship_vendor');

        $this->setCollection($collection);
        $this->getCollection()->addWebsiteNamesToResult();

        return $this;
    }

    protected function _prepareColumns()
    {
        $attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product','udropship_vendor');
        $attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
        $attributeData = $attribute->getData();
        $attributeOptions = $attribute->getSource()->getAllOptions();

        $attributeOptions2 = array();
        foreach ($attributeOptions as $value) {
            if(!empty($value['value'])) {
                $attributeOptions2[$value['value']] = $value['label'];
            }
        }

        $this->addColumnAfter('udropship_vendor',
            array(
                'header'=> Mage::helper('catalog')->__('Vendor Name'),
                'width' => '150px',
                'index' => 'udropship_vendor',
                'type'  => 'options',
                'options' => $attributeOptions2,
        ), 'price');

        return parent::_prepareColumns();
    }
}
Related Topic