将订单网格后端中的SKU更改为DPN属性Magento

时间:2016-08-10 12:46:57

标签: php magento magento-1.9

我目前使用扩展程序在网格中显示邮政编码和SKU。

我想更改此设置,以便使用代码为“dpn”的新属性替换SKU。

我应该如何扩展我的代码?

CODE:

应用程序/代码/本地/ Atwix / ExtendedGrid的/ etc / config.xml中

<?xml version="1.0"?>
<config>
    <modules>
        <Atwix_ExtendedGrid>
            <version>1.0.1</version>
        </Atwix_ExtendedGrid>
    </modules>
    <global>
        <blocks>
            <atwix_extendedgrid>
                <class>Atwix_ExtendedGrid_Block</class>
            </atwix_extendedgrid>
        </blocks>
        <helpers>
            <atwix_extendedgrid>
                <class>Atwix_ExtendedGrid_Helper</class>
            </atwix_extendedgrid>
        </helpers>
        <models>
            <atwix_extendedgrid>
                <class>Atwix_ExtendedGrid_Model</class>
            </atwix_extendedgrid>
            <sales_resource>
                <rewrite>
                    <order_grid_collection>Atwix_ExtendedGrid_Model_Resource_Sales_Order_Grid_Collection</order_grid_collection>
                </rewrite>
            </sales_resource>
        </models>
    </global>
    <adminhtml>
        <events>
            <sales_order_grid_collection_load_before>
                <observers>
                    <atwix_exgrid>
                        <model>atwix_extendedgrid/observer</model>
                        <method>salesOrderGridCollectionLoadBefore</method>
                    </atwix_exgrid>
                </observers>
            </sales_order_grid_collection_load_before>
        </events>
        <layout>
            <updates>
                <atwix_extendedgrid>
                    <file>atwix/extendedgrid.xml</file>
                </atwix_extendedgrid>
            </updates>
        </layout>
    </adminhtml>
</config>

应用程序/代码/本地/ Atwix / ExtendedGrid /助手/ Data.php

    class Atwix_ExtendedGrid_Helper_Data extends Mage_Core_Helper_Abstract
{
    /**
     * parameters for addColumnAfter method
     * @return array
     */
    public function getSkusColumnParams()
    {
        return array(
            'header' => 'SKU',
            'index' => 'skus',
            'type' => 'text',
            'filter_condition_callback' => array('Atwix_ExtendedGrid_Model_Observer', 'filterSkus'),
        );
    }

     public function getPostcodeColumnParams()
    {
        return array(
            'header' => 'Postcode',
            'index' => 'postcode',
            'type' => 'text',               
        );
    } 
}

应用程序/代码/本地/ Atwix / ExtendedGrid /型号/ Observer.php

    class Atwix_ExtendedGrid_Model_Observer
{
    /**
     * Joins extra tables for adding custom columns to Mage_Adminhtml_Block_Sales_Order_Grid
     * @param Varien_Object $observer
     * @return Atwix_Exgrid_Model_Observer
     */
    public function salesOrderGridCollectionLoadBefore($observer)
    {
        $collection = $observer->getOrderGridCollection();
        $select = $collection->getSelect();
        $select->joinLeft(array('payment' => $collection->getTable('sales/order_payment')), 'payment.parent_id=main_table.entity_id', array('payment_method' => 'method'));

        $select->join(
                array('address' => $collection->getTable("sales/order_address")),
                'main_table.entity_id = address.parent_id AND address.address_type = "shipping"',
                array('postcode')
            );

        $select->join('sales_flat_order_item', '`sales_flat_order_item`.order_id=`main_table`.entity_id', array('skus' => new Zend_Db_Expr('group_concat(`sales_flat_order_item`.sku SEPARATOR ", ")')));
        $select->group('main_table.entity_id'); 
    }

    /**
     * callback function used to filter collection
     * @param $collection
     * @param $column
     * @return $this
     */
    public function filterSkus($collection, $column)
    {
        if (!$value = $column->getFilter()->getValue()) {
            return $this;
        }

        $collection->getSelect()->having(
            "group_concat(`sales_flat_order_item`.sku SEPARATOR ', ') like ?", "%$value%");

        return $this;
    }
}

应用程序/代码/本地/ Atwix / ExtendedGrid /型号/资源/销售/订购/网格/ Collection.php

    class Atwix_ExtendedGrid_Model_Resource_Sales_Order_Grid_Collection extends Mage_Sales_Model_Resource_Order_Grid_Collection
{

    /**
     * Get SQL for get record count
     *
     * @return Varien_Db_Select
     */
    public function getSelectCountSql()
    {
        $countSelect = parent::getSelectCountSql();

        if (Mage::app()->getRequest()->getControllerName() == 'sales_order') {
            $countSelect->reset(Zend_Db_Select::GROUP);
            $countSelect->reset(Zend_Db_Select::COLUMNS);
            $countSelect->columns("COUNT(DISTINCT main_table.entity_id)");

            $havingCondition = $countSelect->getPart(Zend_Db_Select::HAVING);
            if (count($havingCondition)) {
                $countSelect->where(
                    str_replace("group_concat(`sales_flat_order_item`.sku SEPARATOR ', ')", 'sales_flat_order_item.sku', $havingCondition[0])
                );
                $countSelect->reset(Zend_Db_Select::HAVING);
            }
        }

        return $countSelect;
    }

    /**
     * Init select
     * @return Mage_Core_Model_Resource_Db_Collection_Abstract
     */
    protected function _initSelect()
    {
        $this->addFilterToMap('store_id', 'main_table.store_id')
            ->addFilterToMap('created_at', 'main_table.created_at')
            ->addFilterToMap('updated_at', 'main_table.updated_at');
        return parent::_initSelect();
    }
}

应用程序/设计/ adminhtml /默认/默认/布局/ atwix / extendedgrid.xml

<layout>
 <sales_order_grid_update_handle>
        <reference name="sales_order.grid">
            <action method="addColumnAfter">
                <columnId>payment_method</columnId>
                <arguments>
                    <header>Betaalmethode</header>
                    <index>payment_method</index>
                    <filter_index>payment.method</filter_index>
                    <type>text</type>
                </arguments>
                <after>shipping_name</after>
            </action>
            <action method="addColumnAfter">
                <columnId>skus</columnId>
                <arguments helper="atwix_extendedgrid/getSkusColumnParams" />
                <after>payment_method</after>
            </action>
            <action method="addColumnAfter">
                <columnId>postcode</columnId>
                <arguments helper="atwix_extendedgrid/getPostcodeColumnParams" />
                <after>skus</after>
            </action>
        </reference>
    </sales_order_grid_update_handle>
    <adminhtml_sales_order_grid>
        <!-- apply layout handle defined above -->
        <update handle="sales_order_grid_update_handle" />
    </adminhtml_sales_order_grid>
    <adminhtml_sales_order_index>
        <!-- apply layout handle defined above -->
        <update handle="sales_order_grid_update_handle" />
    </adminhtml_sales_order_index>
</layout>

0 个答案:

没有答案