Magento - How to trigger event (change user group) after purchasing a specific product

1.8k views Asked by At

My question is how to trigger an action after a successful order (with ID 11)?

I read from previous questions that I need to create an observer for checkout_onepage_controller_success_action but not much is explained after that.

Below is the code that (from the same previous question) I used to change the product:

$special_cat = 11; // Special product category
$customerId = Mage::getSingleton('customer/session')->getCustomerId();
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getSingleton('sales/order'); 
$order->load($lastOrderId);
$allitems = $order->getAllItems();
foreach($allitems as $item)
{
    $product = Mage::getModel('catalog/product')->load($item->getProductId());
    $categoryIds = $product->getCategoryIds();
    if (in_array($special_cat, $categoryIds)) {
        $mem_group_id = $item->getSku();  // $item->getSku() is customer group name
        $customer_detail = Mage::getSingleton('customer/session')->getCustomer();
        $customer_detail->setGroupId($mem_group_id);
        $customer_detail->save();
    }
}

Do I need to create an extension for this or do I need to edit the core files? Where should I create the Observer?

3

There are 3 answers

0
Harit On

It's never a good practise to edit the core files. If you want to change the functionality of default Magento, you should override specific files. Here is a good tutorial on overriding

A much better way is to create an extension and observe specific events. In your case, observe sales_order_place_after event and check whether the order is containing product with ID 11 or not? If yes, then change the customer group.

For More info on observer check this out.

Hope it helps.

0
Elavarasan On

Yes. You need create extension for this. It's not a big deal. So you want change user group for a customer after order is success. Right ?. For this you need observe the checkout process and put your code in that. And you were right, checkout_onepage_controller_success_action is the correct observer for this.

Here you go..

app/code/local/Packagename/Modulename/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Packagename_ModuleName>
      <version>0.1.0</version>
    </Packagename_ModuleName>
  </modules>
  <global>
    <models>
      <modulename>
        <class>Packagename_ModuleName_Model</class>
        <resourceModel>modulename_mysql4</resourceModel>
      </modulename>
    </models>
    <events>
      <checkout_onepage_controller_success_action> <!-- identifier of the event we want to catch -->
        <observers>
          <checkout_onepage_controller_success_action_handler> <!-- identifier of the event handler -->
            <type>model</type> <!-- class method call type; valid are model, object and singleton -->
            <class>modulename/observer</class> <!-- observers class alias -->
            <method>changeUserGroup</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </checkout_onepage_controller_success_action_handler>
        </observers>
      </checkout_onepage_controller_success_action>
    </events>
  </global>
</config> 

Here Packagename_ModuleName_Model is your new class. and changeUserGroup is your new method. Here only we will put our code for user group change things. so,

app/code/local/Packagename/Modulename/Model/Observer.php

 <?php
    class Packagename_ModuleName_Model_Observer
    {

                public function changeUserGroup(Varien_Event_Observer $observer)
                {
                    //$customer = $observer->getCustomer();

$special_cat = 11; // Special product category
$customerId = Mage::getSingleton('customer/session')->getCustomerId();
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getSingleton('sales/order'); 
$order->load($lastOrderId);
$allitems = $order->getAllItems();
foreach($allitems as $item)
{
    $product = Mage::getModel('catalog/product')->load($item->getProductId());
    $categoryIds = $product->getCategoryIds();
    if (in_array($special_cat, $categoryIds)) {
        $mem_group_id = $item->getSku();  // $item->getSku() is customer group name
        $customer_detail = Mage::getSingleton('customer/session')->getCustomer();
        $customer_detail->setGroupId($mem_group_id);
        $customer_detail->save();
    }
}
                }

    }

And finally enable your module in,

app/etc/modules/Packagename_ModuleName.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Packagename_ModuleName>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.1.0</version>
    </Packagename_ModuleName>
  </modules>
</config>

Normally we get the details from $observer when we observe something. But in your case the customer_id and order-id is available in session. So we can get those things from session. That's it.

If you have any doubt please comment here.

0
Prignesh Patel On

no need to create extension for it.

Just go to in your onepage_controller_action and add following code.

 $mem_catid = 982; //CATEGORY ID products to filter
$_customerId = Mage::getSingleton('customer/session')->getCustomerId();
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
$order = Mage::getSingleton('sales/order'); 
$order->load($lastOrderId);
$allitems = $order->getAllItems();
foreach($allitems as $item)
{
    $product = Mage::getModel('catalog/product')->load($item->getProductId());
    $categoryIds = $product->getCategoryIds();
    if (in_array($mem_catid, $categoryIds)) {
        $mem_group_id = $item->getSku();  // $item->getSku() is customer group name
        $customer_detail = Mage::getSingleton('customer/session')->getCustomer();
        $customer_detail->setGroupId(6);//add customer group id here which you want to set.
        $customer_detail->save();
    }
}