Skip to content

Instantly share code, notes, and snippets.

@drewgillson
Created June 10, 2012 17:40
Show Gist options
  • Save drewgillson/2906728 to your computer and use it in GitHub Desktop.
Save drewgillson/2906728 to your computer and use it in GitHub Desktop.
Example - add custom columns to the Magento administration Catalog > Product grid
<?php
class Zyn_Common_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid {
protected function _prepareColumns() {
$this->addColumn('zyn_featured',
array(
'header'=> 'Show on Homepage',
'width' => '50px',
'index' => 'zyn_featured',
'type' => 'options',
'options' => array(0 => 'No', 1 => 'Yes')
));
parent::_prepareColumns();
}
protected function _prepareCollection() {
$store = $this->_getStore();
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('sku')
->addAttributeToSelect('name')
->addAttributeToSelect('attribute_set_id')
->addAttributeToSelect('type_id')
->addAttributeToSelect('zyn_featured');
if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
$collection->joinField('qty',
'cataloginventory/stock_item',
'qty',
'product_id=entity_id',
'{{table}}.stock_id=1',
'left');
}
if ($store->getId()) {
$adminStore = Mage_Core_Model_App::ADMIN_STORE_ID;
$collection->addStoreFilter($store);
$collection->joinAttribute(
'name',
'catalog_product/name',
'entity_id',
null,
'inner',
$adminStore
);
$collection->joinAttribute(
'custom_name',
'catalog_product/name',
'entity_id',
null,
'inner',
$store->getId()
);
$collection->joinAttribute(
'status',
'catalog_product/status',
'entity_id',
null,
'inner',
$store->getId()
);
$collection->joinAttribute(
'visibility',
'catalog_product/visibility',
'entity_id',
null,
'inner',
$store->getId()
);
$collection->joinAttribute(
'price',
'catalog_product/price',
'entity_id',
null,
'left',
$store->getId()
);
}
else {
$collection->addAttributeToSelect('price');
$collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
$collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
}
$this->setCollection($collection);
//parent::_prepareCollection();
$this->getCollection()->addWebsiteNamesToResult();
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment