Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Sort Orders by Customer Groups in magento 1.7
 
Max134
Member
 
Total Posts:  37
Joined:  2010-05-29
 

Hello

I have to Sort order by Customer Groups in magento 1.7.0.2, I try to follow magento wiki:

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

But it doesn’t works.

I copy app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php to app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php to not touch magento core.

I add this code to this function

protected function _prepareColumns(){

 $this
->addColumn('customer_group_id', array(
                
'header'=> Mage::helper('customer')->__('Customer Group'),
                
'width' => '80px',
                
'index' => 'customer_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

Second, in the same file I add this override function

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;
    
}

third, in the Grid.php I modify this function:

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

Now I create this file in app/code/local/Mage/Adminhtml/Block/Sales/Order/Renderer/CustomerGroup.php with this code:

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;
    
}

}

And I have this error:

**:5:{i:0;s:92:"SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘customer_group_id’ in ‘where clause‘“;i:1;s:5645:"#0 MyProjectFolder\lib\Varien\Db\Statement\Pdo\Mysql.php(111): Zend_Db_Statement_Pdo->_execute(Array)**

Thanks in advance !

 
Magento Community Magento Community
Magento Community
Magento Community
 
Denys Babenko
Sr. Member
 
Avatar
Total Posts:  130
Joined:  2010-03-03
Fritz-Elsas-Str. 60
 

Hi Max,

this should fix the problem

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

Best Regards,
Denys

 
Magento Community Magento Community
Magento Community
Magento Community
 
Max134
Member
 
Total Posts:  37
Joined:  2010-05-29
 

Hello

Yes it works !

Thanks a lot.

 
Magento Community Magento Community
Magento Community
Magento Community
 
laraz
Jr. Member
 
Total Posts:  4
Joined:  2012-04-30
 

It works perfect on Magento 1.7.0.2 . Thanks a lot!!

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top