0

I am trying to create a handler for the Order place event using which I need to send the order data to some other table. I have created a module with config.xml and observer.php file. The handler function is called when order is placed, but the $observer objects does not return any order data.

My Sm/Sx/etc/config.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Sm_Sx>
            <version>0.1.0</version>
        </Sm_Sx>
    </modules>
    <global>
        <helpers>
            <Sx>
                <class>Sm_Sx_Helper</class>
            </Sx>
        </helpers>
        <events>
            <sales_order_place_after>
                <observers>
                    <Sm_Sx_Model_Observer>
                        <class>Sm_Sx_Model_Observer</class>
                        <method>create_sx_order</method>
                    </Sm_Sx_Model_Observer>
                </observers>
            </sales_order_place_after>
        </events>
    </global>
</config>

My Sm/Sx/Model/Observer.php is:

class Sm_Sx_Model_Observer
{
    public function __construct()
    {
        //Just constrcutor for fun :-)
    }

    public function create_sx_order(Varien_Event_Observer $observer)
    {

        $data = $observer->getData('Order');

        echo "<pre>";
        print_r($data);
        echo "</pre>";      
        exit;

        return $this;
    }   
}

I have tried using:

$data = $observer->getOrder();

And

$data = $observer->getEvent()->getOrder();

But nothing works. Any help would be greatly appreciated.

5
  • did you try output in Magento log Mage::log('orderData'); Mage::log($data->debug()); you will see output in log.. if you are using exit then you will see the output in console. Mar 17, 2016 at 11:56
  • I tried the log option also, but there is nothing in the log for the order data. I am using the exit. When outputting static text only, it shows in console. But fetching the order it fails, or shows nothing.
    – Rajendra
    Mar 17, 2016 at 12:23
  • if you are using this try $data = $observer->getData('order'); Mar 17, 2016 at 12:34
  • I am using this: $data = $observer->getData('Order'); I am not sure if small and caps "O" in Order makes any difference!
    – Rajendra
    Mar 17, 2016 at 12:35
  • some time it make a difference. I don't know what is problem in you situation but i always get the data with this code $data = $observer->getOrder(); Mar 17, 2016 at 12:39

2 Answers 2

1

There are few reasons, the event does nor fires this

  • Maybe you do not create this module configuration file Sm_Sx.xml at app/etc/modules
  • You used event sales_order_place_after does not fire always as per as**,my thought** it is only fire when Order object (Mage_Sales_Model_Order) is called $orderObject->place().

I don't think it is always called, So suggest to use another event which is called during order place

like

  • checkout_submit_all_after
<?php
Class [ModuleNameSpace]_[ModuleName]_Model_Observer{

  public function checkoutOrderDetails($observer){
  $order=$observer->getEvent()->getOrder();

      if($order->getId()){
          $ProdustIds=array();
          foreach ($order->getAllVisibleItems() as $item)
          {
              $item->getProductId();
          }        
      }
  }
}

config.xml code

<global>
    <events>
        <checkout_submit_all_after>
            <observers>
                <order_data_at_place>
                    <type>singleton</type>
                    <class>[ModuleNameSpace]_[ModuleName]_Model_Observer</class>
                    <method>checkoutOrderDetails</method>
                </order_data_at_place>
            </observers>
        </checkout_submit_all_after>
    </events>
</global>
0

I know it is too late, but just to post the solution. It worked using the code below:

$order=$observer->getEvent()->getOrder();
$magento_order_id = $order->getId();

And in my config.XML:

<events>
    <sales_order_place_after>
        <observers>
            <Sm_Sx_Model_Observer>
                <class>Sm_Sx_Model_Observer</class>
                <method>create_sx_order</method>
            </Sm_Sx_Model_Observer>
        </observers>
    </sales_order_place_after>
    <catalog_product_load_after>
        <observers>
            <Sm_Sx_Model_Observer>
                <class>Sm_Sx_Model_Observer</class>
                <method>update_product_availability</method>
            </Sm_Sx_Model_Observer>
        </observers>
    </catalog_product_load_after>
</events>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.