Change customer group upon buying a specific product

All we need is an easy explanation of the problem, so here it is.

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.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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>

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply