Change Customer Group Upon Buying a Specific Product

customer-group

This question has been asked a few times before but none of the answers have worked for my specific case.

What I want:
If someone purchases a product off of my store (We will call it Prime Membership) then their customer group will automatically be changed to whichever group I specify (We will call this Special Group). So When a customer purchases Prime Membership then they will be placed in the customer group called Special Group.

I have followed the instructions here: https://stackoverflow.com/a/27057781/4043494

The observer code is giving me some trouble. I have made all 3 files mentioned in that post and implemented my own module/package where necessary in the code. I have changed the category ID to my own, and even went as far as changing the Sku of the product to be the name of the Group. None of this seems to have made this work correctly. The group of the customer just will not change.

This code I actually used just because it seemed to be the closest thing to getting what I want to work.

I would rather just define one specific product as opposed to a category that the product is in. I feel like this should be rather simple but I may be wrong.

To go over it again and make a long story short what I want is this: Someone buys a product that I specify, and they automatically get put into a customer group that I specify.

Any and all help is much appreciated.

UPDATE:

<?xml version="1.0"?>
<config>
  <modules>
    <Package_Module>
      <version>0.1.0</version>
    </Package_Module>
  </modules>
  <global>
    <models>
      <package_module>
        <class>Package_Module_Model</class>
        <resourceModel>module_mysql4</resourceModel>
      </package_module>
    </models>
    <events>
    <sales_order_place_after>
        <observers>
            <package_module_sales_place_order_after>
                <class>package_module/observer</class>
                <method>changeGroup</method>
            </package_module_sales_place_order_after>
        </observers>
    </sales_order_place_after>
    </events>
  </global>
</config> 

Observer:

 <?php
    class Package_Module_Model_Observer
    {

public function changeGroup(Varien_Event_Observer $observer)
{
    $order = $observer->getEvent()->getOrder();
    $customer = Mage::getModel('customer/customer')->load($order->getCustomerId());

    // ensure it's not guest checkout
    if ($customer->getId()) {
        $items = $order->getAllVisibleItems();
        foreach ($items as $item) {
            $sku = $item->getSku();
            if ($sku == "Special") {
                $customer->setGroupId(5);
                $customer->save();
                break;
            }
        }
    }
}
}

Got this to work.

Best Answer

You can do the following in your observer, using the sales_order_place_after event (will also work with multiple shipping):

public function changeGroup(Varien_Event_Observer $observer)
{
    $order = $observer->getEvent()->getOrder();
    $customer = Mage::getModel('customer/customer')->load($order->getCustomerId());

    // ensure it's not guest checkout
    if ($customer->getId()) {
        $items = $order->getAllVisibleItems();
        foreach ($items as $item) {
            $sku = $item->getSku();
            if ($sku == [whatever your sku is]) {
                $customer->setGroupId([whatever your new group ID is]);
                $customer->save();
                break;
            }
        }
    }
}

Then change your config to this (replacing 'modulename' with your module name):

<events>
    <sales_order_place_after>
        <observers>
            <modulename_sales_place_order_after>
                <class>modulename/observer</class>
                <method>changeGroup</method>
            </modulename_scratch_sales_place_order_after>
        </observers>
    </sales_order_place_after>
</events>