Engine23

Temporarily and Programmatically Set Group Id in Session

 

I found a solution for anyone who needs it.

Because Magento has a fallback hierarchy, you can easily override the customer session class without modifying core code.

1. Locate and copy this file

 

app/code/core/Mage/Customer/Model/Session.php

 

and insert it into this directory structure

 

app/code/local/Mage/Customer/Model/Session.php

 

2. Open Session.php and locate line 137, function getCustomerGroupId().  Modify the code to suit your needs:

public function getCustomerGroupId()
    
{
        
if ($this->isLoggedIn()) {
            
return $this->getCustomer()->getGroupId();
        
elseif( YOUR CONDITION HERE ){
            
return 4//Where 4 is the group id number
        
else {
            
return Mage_Customer_Model_Group::NOT_LOGGED_IN_ID;
        
}
    }

 

3. You will also need to override Quote.php.  Copy this file:

app/code/core/Mage/Sales/Model/Quote.php

 

And paste it into this directory structure:

app/code/local/Mage/Sales/Model/Quote.php

 

Then modify line 356, function getCustomerGroupId():

 

public function getCustomerGroupId()
    
{
        
if ($this->getCustomerId()) {
            
return ($this->getData('customer_group_id')) ? $this->getData('customer_group_id')
                : 
$this->getCustomer()->getGroupId();
        
elseif( YOUR CONDITION HERE {
            
return 4//Where 4 is the group id number
        
else {
            
return Mage_Customer_Model_Group::NOT_LOGGED_IN_ID;
        
}
    }

Share: