Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento: Observer for order state is complete

Tags:

magento

On Magento, I am developing a commission module which I need to pay my agents when the sales is completed and not when the order is placed.

Ideally I would want to capture it when the order state turns into 'complete' but I have not found an observer to do this ?

I could add this to the shipment observer but a order can have multiple shipment and hence not right.

Alternatively I could always fire a cron job to calculate the order for last hour but again does not seem the right way.

Any suggestions on what is the right way to do this.

like image 910
TheVyom Avatar asked Oct 28 '11 09:10

TheVyom


1 Answers

Use the observer "sales_order_save_commit_after" or "sales_order_invoice_pay" then you can get the order status and depending on the status you do what you want. Here is an example:

// for event sales_order_save_commit_after
public function commissionCalculationOnComplete($observer)
{
    $order = $observer->getOrder();
    if($order->getState() == Mage_Sales_Model_Order::STATE_COMPLETE){
    // do your order complete stuff
    }
}

or

// Event sales_order_invoice_pay
public function triggerProvisionCalculation ($observer)
{
    $invoice = $observer->getEvent()->getInvoice();
    switch ($invoice->getState()) {
        case Mage_Sales_Model_Order_Invoice::STATE_PAID :
            //do your stuff
            break;
    }
    return $this;
}

You will have to check that you don't do the calculation twice, because this method is triggered each time an order is saved.

like image 110
Sylvain Rayé Avatar answered Nov 09 '22 23:11

Sylvain Rayé