1

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 !

asked Mar 20, 2015 at 11:51

2 Answers 2

2

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();
 }
}
answered May 1, 2015 at 12:05
1

Issue with _prepareColumns function, need return Mage_Adminhtml_Block_Widget_Grid::_prepareColumns() instead of parent::_prepareColumns().

Code:

 protected function _prepareColumns()
 {
 $this->addColumnAfter('vendor_name', array(
 'header' => Mage::helper('catalog')->__('Vendor'),
 'index' => 'vendor_name',
 'type' => 'text',
 ), 'visibility');
 parent::_prepareColumns();
 return Mage_Adminhtml_Block_Widget_Grid::_prepareColumns();
 }

Change at _prepareCollection function.But does not sure 100%.it given on idea

 protected function _prepareCollection() {
 parent::_prepareCollection();
 $collection = $this->getCollection();
 $collection->getSelect()
 ->join(
 array('v' => $collection>getTable('udropship/vendor')),
 'e.entity_id = v.vendor_id',
 array('vendor_name')
 );
 $collection->printLogQuery(true);
 $this->setCollection($collection);
 return $this;
 }
answered Mar 20, 2015 at 12:15
1
  • Concerning, the _prepareColumns function, you seems to do the exact same thing as me. The Mage_Adminhtml_Block_Catalog_Product_Grid::_prepareColumns() already return Mage_Adminhtml_Block_Widget_Grid::_prepareColumns(). Am I wrong ? For the _prepareCollection method, my function seems to be ok (at least the data it returns are correct). Commented Mar 20, 2015 at 13:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.