Question

I try to change the customers group id in the observer sales_order_place_after but it seems to be impossible. I successfully managed to change the firstname of the customer just for test, but groupId ? No chance...

Can someone explain why it is not possible to change the groupId ?

This is my attempt:

class EaDesign_CustomerGroup_Model_SalesOrderPlaceAfterObserver
{
    ...

    public function changeCustomerGroup(Varien_Event_Observer $observer)
    {    
        try
        {
            $customer = Mage::getSingleton('customer/session')->getCustomer();
            $newGroupId = '7';

            $customer->setGroupId($newGroupId);  // does not work
            //$customer->setData('group_id', $newGroupId);  // second attempt, not working too


                    $customer->setFirstname('hans');  // this works

                    $customer->save();
                    if ($customer->save())
                    {
                        Mage::Log('customer saved', 7, 'autochange_customer_group.log', true);  // this is logged
                    }


        } catch (Exception $e) {

            Mage::log('customer session moving ' . $e->getMessage());
        }
    }

But the customer group is still the same!

I made sure that the customer group id is correct by inspecting the HTML code in the backend:

enter image description here

Was it helpful?

Solution

I noticed that it is working with the same code in another magento installation where the same git branch is checked out. So it was clear that the error was caused by difference in the database, or in other words: by magento settings.

So I compared the settings from both installations and after alot of comparing I found out that it was due to the setting in (following is translated from german...) System -> Configuration -> Customers -> Customerconfiguration -> Create Customer Options -> Activate automatic customer group. After setting the value to No it suddenly works.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top