Magento – How to change the default group id when certain condition occurs

checkoutcustomer-groupmagento-1.8onestepcheckout

How can I change the default group id when certain condition occurs?

For instance, the default group id is 1 which is general. But I have a new group which is 4 (memebership).

And I want to set the customer to this group when they are purchasing a club item, I have this code in onepage/billing.phtml

<?php if($orderHasWineClub === true):?>
    <?php Mage::getSingleton('customer/session')->setCustomerGroupId(4); //4 being the customer group id ?>
<?php endif;?>

after this,

<?php if(!$this->isCustomerLoggedIn()): ?>
                <li class="control">
                    <div class="form-group checkbox">
                        <label for="billing:create_account" class="required">
                            <input type="checkbox" name="billing[create_account]" id="billing:create_account" value="1" title="<?php echo  $this->__('Create your account for later use') ?>"<?php if($orderHasWineClub === true):?> class="required-entry"<?php endif;?>/>
                            <a href="#" class="button-cart-simple"><?php echo  $this->__('Create your account for later use') ?></a>
                        </label>
                    </div> 
                </li>

<!-- the code of setCustomerGroupId(4) -->
....
....

But I still get general as my customer group. so I think the code is not working.

Any idea?

I am suing IWD_OPC onestep checkout by they way.

Best Answer

You need set the customer group and save the user:

$customer = Mage::getSingleton('customer/session')->getCustomer();
$customer->setGroupId(4);
$customer->save();