Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
895 views
in Technique[技术] by (71.8m points)

php - Add column to Magento admin catolog > manage products

Hi I want to add a column to the catolg > manage products section (not the product but the list of products), this column needs to list any related products the product has identified with it - maybe by sku or name - no preferance there.

I added a column for manufacturer but forgot where i obtained the code from.

thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I recently (yesterday in fact) had to add a column to the same grid. Partly because it is poor practice and mostly because another module had already used it's own override, I didn't want to replace or override the class completely. Instead here is a clean way to modify the product's grid via events.

app/code/local/My/Module/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>

app/code/local/My/Module/Model/Adminhtml/Observer

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...');
        }
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...