2

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

  • Customer is saved twice, remove this one $customer->save(); before the if and try. as well as print $customer->getData() and $customer->getOrigData() before and after save, which gives you an idea, why its not set. – Haijerome Feb 19 at 16:43
  • I removed the second save, but it still fails. I placed getOrigData before and after and there was no change in the groupId. getData shows the new ID. What is going on? – Black Feb 20 at 8:37
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.