将列添加到 Magento 管理目录 >管理产品

发布于 2024-11-07 00:56:18 字数 134 浏览 0 评论 0原文

您好,我想在目录中添加一列> >管理产品部分(不是产品,而是产品列表),此列需要列出该产品已识别的任何相关产品 - 可能按 SKU 或名称 - 没有偏好。

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半衾梦 2024-11-14 00:56:18

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

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

}

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

}
誰認得朕 2024-11-14 00:56:18

为了改进clockworkgeek的答案https://stackoverflow.com/a/5994209/1025437

我决定不使用观察员们,在我看来,这些事件太过全球性,导致我们的观察员被多次召集。在您自己的模块 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();
    }
}

在此示例中,在列 price 之后添加名为 special_price 的属性。由于该属性具有 store 范围,因此添加了对 store 的检查。

To improve on answer https://stackoverflow.com/a/5994209/1025437 by clockworkgeek:

I decided not to use the observers, in my opinion those events are too global and lead to our observer being called many times. Using the following rewrite in your own module 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>

With the following file in

app/code/local/Myname/Catalogextended/Block/Adminhtml/Catalog/Product/Grid.php

containing something like:

<?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();
    }
}

In this example, an attribute named special_price is added after column price. Since this attribute has a store scope, the check for store is added.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文