嗨我想向catolg>管理產品部分添加一列(不是產品,而是產品列表),此列需要列出產品已識別的所有相關產品它 - 也許通過sku或名字 - 沒有那裏的優先。將欄添加到Magento中管理產品目錄>管理產品
我爲製造商添加了一列,但忘了我從哪裏獲得代碼。
謝謝
嗨我想向catolg>管理產品部分添加一列(不是產品,而是產品列表),此列需要列出產品已識別的所有相關產品它 - 也許通過sku或名字 - 沒有那裏的優先。將欄添加到Magento中管理產品目錄>管理產品
我爲製造商添加了一列,但忘了我從哪裏獲得代碼。
謝謝
我最近(事實上是昨天)不得不添加一列到同一個網格。部分原因是這種做法很差,主要是因爲另一個模塊已經使用了它自己的覆蓋,我不想完全替換或重寫該類。相反,這是通過事件修改產品網格的乾淨方式。
應用程序/代碼/本地/我/模塊的/ etc/config.xml中
<config>
<adminhtml>
<events>
<adminhtml_block_html_before>
<observers>
<mymodule>
<!-- Add column to catalog product grid -->
<class>mymodule/adminhtml_observer</class>
<method>onBlockHtmlBefore</method>
</mymodule>
</observers>
</adminhtml_block_html_before>
<eav_collection_abstract_load_before>
<observers>
<mymodule>
<!-- Add column to product list -->
<class>mymodule/adminhtml_observer</class>
<method>onEavLoadBefore</method>
</mymodule>
</observers>
</eav_collection_abstract_load_before>
</events>
</adminhtml>
</config>
應用程序/代碼/本地/我/模塊/型號/ Adminhtml /觀察員
class My_Module_Model_Adminhtml_Observer
{
public function onBlockHtmlBefore(Varien_Event_Observer $observer) {
$block = $observer->getBlock();
if (!isset($block)) return;
switch ($block->getType()) {
case 'adminhtml/catalog_product_grid':
/* @var $block Mage_Adminhtml_Block_Catalog_Product_Grid */
$block->addColumn('COLUMN_ID', array(
'header' => Mage::helper('mymodule')->__('COLUMN HEADER'),
'index' => 'COLUMN_ID',
));
break;
}
}
public function onEavLoadBefore(Varien_Event_Observer $observer) {
$collection = $observer->getCollection();
if (!isset($collection)) return;
if (is_a($collection, 'Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection')) {
/* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection */
// Manipulate $collection here to add a COLUMN_ID column
$collection->addExpressionAttributeToSelect('COLUMN_ID', '...Some SQL goes here...');
}
}
}
由clockworkgeek改善回答https://stackoverflow.com/a/5994209/1025437:
我決定不使用觀察者,在我的意見ñ這些事件太全球化,導致我們的觀察員被多次召喚。在
app/code/local/Myname/Catalogextended/Block/Adminhtml/Catalog/Product/Grid.php
含有類似
<config>
<global>
<blocks>
<adminhtml>
<rewrite>
<catalog_product_grid>Myname_Catalogextended_Block_Adminhtml_Catalog_Product_Grid</catalog_product_grid>
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
具有下列文件:在您自己的模塊config.xml中使用以下重寫
<?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
之後添加。由於此屬性具有存儲範圍,因此添加了存儲檢查。
好東西,優秀:) – 2011-11-16 11:32:34
我做了這個答案的更新,以便管理員可以過濾新產品列 – 2013-01-09 06:27:08
啊!這真太了不起了!我發現唯一缺少的是對新列進行排序的功能。 – 2013-03-13 21:53:40