将列添加到Magento admin catolog> manage products

Gra*_*nes 10 php magento

嗨我想在catolg> manage products部分(不是产品而是产品列表)中添加一列,此列需要列出产品已经识别的任何相关产品 - 可能是sku或name - 没有优先权.

我为制造商添加了一列,但忘了我从哪里获得了代码.

谢谢

clo*_*eek 24

我最近(昨天事实上)不得不在同一个网格中添加一列.部分是因为它是不好的做法,主要是因为另一个模块已经使用了它自己的覆盖,我不想完全替换或覆盖该类.相反,这是一种通过事件修改产品网格的简洁方法.

应用程序/代码/地方/我的/模块的/ 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>
Run Code Online (Sandbox Code Playgroud)

应用程序/代码/地方/我的/模块/型号/ 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...');
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 这种"替代"方法看起来更像我原来的帖子.Bang Dao的修改意义重大,回想起来我更愿意看到他单独回答而不是劫持我的. (4认同)
  • 我对此答案进行了更新,以便管理员可以使用新添加的列过滤产品 (2认同)

Mon*_*ane 20

要通过clockworkgeek 改进回答/sf/answers/419594661/:

我决定不使用观察者,在我看来,这些事件太全局化,导致我们的观察者被多次召唤.在您自己的模块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>
Run Code Online (Sandbox Code Playgroud)

使用以下文件

app/code/local/Myname/Catalogextended/Block/Adminhtml/Catalog/Product/Grid.php
Run Code Online (Sandbox Code Playgroud)

包含类似的东西:

<?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();
    }
}
Run Code Online (Sandbox Code Playgroud)

在此示例中,special_price在列之后添加名为的属性price.由于此属性具有存储范围,因此会添加商店检查.