繁体   English   中英

在magento 1.7中按客户组对订单进行排序

[英]Sort Orders by Customer Groups in magento 1.7

我必须在magento 1.7.0.2中按客户组对订单进行排序,我尝试遵循magento Wiki:

http://www.magentocommerce.com/wiki/5_-_modules_and_development/admin/sort_order_by_customer_groups

但这是行不通的。

我将app / code / core / Mage / Adminhtml / Block / Sales / Order / Grid.php复制到app / code / local / Mage / Adminhtml / Block / Sales / Order / Grid.php以不接触magento核心。

我将此代码添加到此功能

protected function _prepareColumns(){

 $this->addColumn('customer_group_id', array(
                'header'=> Mage::helper('customer')->__('Customer Group'),
                'width' => '80px',
                'index' => 'group_id',
                'renderer' => new   Mage_Adminhtml_Block_Sales_Order_Renderer_CustomerGroup(),
                'type' => 'options',
                'options' => Mage_Adminhtml_Block_Sales_Order_Renderer_CustomerGroup::getCustomerGroupsArray(),
        ));

// now the code original
}

其次,在同一文件中添加此覆盖功能

protected function _addColumnFilterToCollection($column) {

        if ($this->getCollection()) {
            $field = ( $column->getFilterIndex() ) ? $column->getFilterIndex() : $column->getIndex();
            if ($column->getFilterConditionCallback()) {
                call_user_func($column->getFilterConditionCallback(), $this->getCollection(), $column);
            }
            else {
                $cond = $column->getFilter()->getCondition();
                if ($field && isset($cond)) {
                    if (in_array('NULL', array_values($cond))) {
                        $this->getCollection()->addFieldToFilter($field, array('null' => true));
                    }
                    else {                      
                        $this->getCollection()->addFieldToFilter($field, $cond);
                    }
                }
            }
        }
        return $this;
    }

第三,在Grid.php中,我修改此功能:

protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel($this->_getCollectionClass());

        $collection->getSelect()->joinLeft(
          array('ce'=>'customer_entity'),
          'ce.entity_id=main_table.customer_id',
          array('ce.group_id')
        );
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

现在,我使用以下代码在app / code / local / Mage / Adminhtml / Block / Sales / Order / Renderer / CustomerGroup.php中创建此文件:

class Mage_Adminhtml_Block_Sales_Order_Renderer_CustomerGroup
extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {

    // Holds an associative array with customer_group_id and the associated label
    private static $_customerGroups = array(); // "singleton"

    public static function getCustomerGroupsArray() {
        // Make sure the static property is only populated once
        if (count(self::$_customerGroups) == 0) {
            $customer_group = new Mage_Customer_Model_Group();
            $customer_groups = $customer_group->getCollection()->toOptionHash();
            self::$_customerGroups = $customer_groups;
        }

        return self::$_customerGroups;
    }

    // Transforms the customer_group_id into corresponding label
    public function render(Varien_Object $row)
    {
        $val = $this->_getValue($row);
        $customer_groups = self::getCustomerGroupsArray();
        return isset($customer_groups[$val]) ? $customer_groups[$val] : false;
    }

}

我有这个错误:

:5:{i:0; s:92:“ SQLSTATE [42S22]:找不到列:1054'where子句'中的未知列'customer_group_id';; i:1; s:5645:”#0 MyProjectFolder \\ lib \\ Varien \\ Db \\ Statement \\ Pdo \\ Mysql.php(111):Zend_Db_Statement_Pdo-> _ execute(Array)

提前致谢 !

我在magento论坛中找到了解决方案,有人给了我正确的解决方案:

protected function _prepareCollection()
{
        $collection = Mage::getResourceModel($this->_getCollectionClass());

        $collection->getSelect()->join(
          array('oe'=>'sales_flat_order'),
          'oe.entity_id=main_table.entity_id',
          array('oe.customer_group_id')
        );
        $this->setCollection($collection);
        return parent::_prepareCollection();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM