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?
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.