0
votes

I want to create an event to export inventory when status of order is changed to completed.

I created module to overwrite Mage_Sales_Model_Order and tried to generated an event

(sales_order_status_change).

When from magento admin panel I view and order and change order state to complete, it works mean print something in my extended class, it shows, its fine. Here I generate event.(Problem could be here, may be event not generated)

I created an other module to observe this event. But I am unable to get into my Observer class. I cannot print anything and exit, means code does not run.

But in the same observer class if change config and observe some other event (From Magento built in events) it works. Like I tested catalog_controller_product_view

Here Is my Event Generator Module.

app/etc/modules/Gol_Eventgenerator.xml

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

app\code\local\Gol\Eventgenerator\etc\config.xml

<?xml version="1.0" encoding="utf-8"?>
<config>
    <modules>
        <Gol_Eventgenerator>
            <version>0.1.0</version>
        </Gol_Eventgenerator>
    </modules>
    <global>
        <models>
            <sales>
                <rewrite>
                    <order>Gol_Eventgenerator_Model_Order</order>
                </rewrite>
            </sales>
        </models>
    </global>
</config>

app\code\local\Gol\Eventgenerator\Model\Order.php

<?php
class Gol_Eventgenerator_Model_Order extends Mage_Sales_Model_Order
{
    public function setState($state, $status = false, $comment = '', $isCustomerNotified = null)
    {   
        //if I print something here and exit, it does will.
        Mage::dispatchEvent('sales_order_status_change', array('order' => $this, 'state' => $state, 'status' => $status, 'comment' => $comment, 'isCustomerNotified' => $isCustomerNotified));
        return parent::setState($state, $status, $comment, $isCustomerNotified);
    }
}

Here are the details of observer module

app/etc/modules/Gol_Inventory.xml

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

app\code\local\Gol\Inventory\etc\config.xml

<?xml version="1.0" encoding="utf-8"?>
<config>
    <modules>
        <Gol_Inventory>
            <version>0.1.0</version>
        </Gol_Inventory>
    </modules>        
    <global>        
        <events>
            <sales_order_status_change>
                <observers>
                    <Gol_Inventory_observer>
                        <type>singleton</type>
                        <class>Gol_Inventory_Model_Observer</class>
                        <method>exportEnventory</method>
                    </Gol_Inventory_observer>
                </observers>
            </sales_order_status_change>
        </events>
    </global>
</config>

app\code\local\Gol\Inventory\Model\Observer.php

<?php
class Gol_Inventory_Model_Observer
{
  public function exportEnventory($observer)
  {   
        echo "Inside exportEvnentory method";
        exit;
      //$order = $observer->getEvent()->getOrder();
      //$state = $observer->getEvent()->getState();
      //$status = $observer->getEvent()->getStatus();
  }
}

Is there any difference in the way for generating events for frontend and admin??? except

, in config file.

Sorry in advance if I have not followed some very common thing. New to magento.

I have tried to follow .

THis

1
you don't need to create an event for that, observe the sales_order_save_before and check the new status with the previous oneOSdave
Thanks a lot! what attributes will I get from the $observer ?? and when this event will be fired?? when order is placed from front end?? or when order status changed form admin??? or both??sohail042414
I tried, sales_order_save_before gets fired from both sides. but I want an event that should be fired only when order status changes to completed. Any Idea??sohail042414

1 Answers

0
votes

there is no event for just when the order changes status, but you can easily check if the order is changing status and what status it is now:

    public function exportEnventory(Varien_Event_Observer $observer)
    {
        $status = $observer->getEvent()->getOrder()->getStatus();
        $originalData = $observer->getEvent()->getOrder()->getOrigData();
        $previousStatus = $originalData['status'];

        if (($status !== $previousStatus) && ($status == Mage_Sales_Model_Order::STATE_COMPLETE)) {
            //do something when the order changes to status complete
        }
    }

edit: and in your config.xml

<?xml version="1.0" encoding="utf-8"?>
<config>
    <modules>
        <Gol_Inventory>
            <version>0.1.0</version>
        </Gol_Inventory>
    </modules>        
    <global>        
        <events>
            <sales_order_save_after>
                <observers>
                    <Gol_Inventory_observer>
                        <type>singleton</type>
                        <class>Gol_Inventory_Model_Observer</class>
                        <method>exportEnventory</method>
                    </Gol_Inventory_observer>
                </observers>
            </sales_order_save_after>
        </events>
    </global>
</config>