Для улучшения ответа https://stackoverflow.com/a/5994209/1025437 от clockworkgeek:
Я решил не использовать наблюдателей, на мой взгляд, эти события слишком глобальны и приводят к тому, что наш наблюдатель вызывается много раз. Используя следующую переписать в вашем собственном модуле config.xml:
<config>
<global>
<blocks>
<adminhtml>
<rewrite>
<catalog_product_grid>Myname_Catalogextended_Block_Adminhtml_Catalog_Product_Grid</catalog_product_grid>
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
Со следующим файлом в
app/code/local/Myname/Catalogextended/Block/Adminhtml/Catalog/Product/Grid.php
содержит что-то вроде:
<?php
class Myname_Catalogextended_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid
{
/* Overwritten to be able to add custom columns to the product grid. Normally
* one would overwrite the function _prepareCollection, but it won't work because
* you have to call parent::_prepareCollection() first to get the collection.
*
* But since parent::_prepareCollection() also finishes the collection, the
* joins and attributes to select added in the overwritten _prepareCollection()
* are 'forgotten'.
*
* By overwriting setCollection (which is called in parent::_prepareCollection()),
* we are able to add the join and/or attribute select in a proper way.
*
*/
public function setCollection($collection)
{
/* @var $collection Mage_Catalog_Model_Resource_Product_Collection */
$store = $this->_getStore();
if ($store->getId() && !isset($this->_joinAttributes['special_price'])) {
$collection->joinAttribute(
'special_price',
'catalog_product/special_price',
'entity_id',
null,
'left',
$store->getId()
);
}
else {
$collection->addAttributeToSelect('special_price');
}
parent::setCollection($collection);
}
protected function _prepareColumns()
{
$store = $this->_getStore();
$this->addColumnAfter('special_price',
array(
'header'=> Mage::helper('catalog')->__('special_price'),
'type' => 'price',
'currency_code' => $store->getBaseCurrency()->getCode(),
'index' => 'special_price',
),
'price'
);
return parent::_prepareColumns();
}
}
В этом примере атрибут с именем special_price
добавляется после столбца price
. Поскольку этот атрибут имеет область видимости магазина, добавляется проверка на наличие магазина.