7

Get event before place order or save new order. I have tried the sales_order_save_before event but I didn't get any data. Below is my code, In config.xml:

<sales_order_save_before>
    <observers>
        <custom_observerwww>
            <type>singleton</type>
            <class>Custom_Module_Model_Observer</class>
            <method>removeItems</method>
        </custom_observerwww>
    </observers>
</sales_order_save_before>

In Observer.php:

class Custom_Module_Model_Observer {
    public function removeItems($observer){
        Mage::log($observer->getEvent()->getOrder(),null,"order.log",true);
    }

There is no data received in log file.

6
  • In your mage::log if you replace $observer->getEvent()->getOrder() with any other text does that appear in your log?
    – rob3000
    Commented Jun 6, 2015 at 12:58
  • yes.It gets printed Commented Jun 6, 2015 at 13:05
  • so then the event actually works, its just that the order might not be available
    – rob3000
    Commented Jun 6, 2015 at 13:08
  • yes.I don't know how to get order data before save or place Commented Jun 6, 2015 at 13:09
  • are you want to fire always whenever there are some chnage will be done. As per as magento system, whenever any change done at order then sales_order_save_before.
    – Amit Bera
    Commented Jun 7, 2015 at 6:38

3 Answers 3

11

Try this da. It worked for me.

config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Always_Nallavan>
            <version>0.0.1</version>
        </Always_Nallavan>
    </modules>
    <global>
        <models>
            <alwaysnallavan>
                <class>Nallavan_Model</class>
            </alwaysnallavan>
        </models>
        <events>
            <sales_order_place_before>
                <observers>
                    <always_nallavan_model_observer>
                        <type>singleton</type>
                        <class>Always_Nallavan_Model_Observer</class>
                        <method>dandanaka</method>
                    </always_nallavan_model_observer>
                </observers>
            </sales_order_place_before>
        </events>
    </global>
</config>

Observer.php

<?php

class Always_Nallavan_Model_Observer
{

    public function dandanaka($observer)
    {
        $data = $observer->getEvent()->getOrder();
        Mage::log($data->debug(), null, "order.log", true);
    }
}

Note the debug() method, that returns a string representation of any Varien_Object instance.

5
  • @vinothavan what does dandanaka ah danakkunakka mean? lol :-)
    – Anto S
    Commented Jun 27, 2016 at 10:11
  • @invissible Its just a method name man.
    – vinothavn
    Commented Jun 28, 2016 at 9:53
  • I understood but when I read that method name I understood that you are TR follower :-)
    – Anto S
    Commented Jun 28, 2016 at 10:02
  • Ok invissible. Nandri :)
    – vinothavn
    Commented Jun 28, 2016 at 13:34
  • 1
    நன்றி நன்றி நன்றி
    – Anto S
    Commented Jun 28, 2016 at 13:56
0

Now that you know your event is working you should be able to get the order by the following:

$observer->getOrder() or $observer->getEvent()->getData('order')

1
  • Already I tried like this not working Commented Jun 6, 2015 at 13:24
-1

Please check this code it will definitely work.

1st STEP:

app/code/local/Demo/Observerexample/Model/Observer.php

class Demo_Observerexample_Model_Observer
{

    public function send_email($observer)
    {

        $orderIds = $observer->getData('order_ids');

        foreach ($orderIds as $_orderId) {
            $order = Mage::getModel('sales/order')->load($_orderId);
            $store = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
            //$customer  = Mage::getModel('customer/customer')->load($order->getData('customer_id'));
            //$customer->getDefaultBillingAddress()->getLastname();
            $billingaddress = $order->getBillingAddress();
            $Ordercreated = $order->getCreatedAt();
            $OrderId = $order->getIncrementId();
            $Getstaus = $order->getStatusLabel();
            $shipping = $order->getShippingDescription(); // Shipping address for perticular product part.
            $country = $billingaddress->getCountry();
            $countryName = Mage::getModel('directory/country')->load($country)->getName();

            try {
                $params = array('customerName' => $order->getData('customer_firstname'),
                    'customerlastname' => $order->getData('customer_lastname'),
                    'telephone' => $billingaddress->getData('telephone'),
                    'email' => $billingaddress->getData('email'),
                    'street' => $billingaddress->getData('street'),
                    'city' => $billingaddress->getData('city'),
                    'region' => $billingaddress->getData('region'),
                    'postcode' => $billingaddress->getData('postcode'),
                    'total' => $order->getGrandTotal());

                //var_dump($Getstaus);
                //die();

                /* $url= 'http://localhost/allcat/getorders.php';         // the url on which the data will be send through curl
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch,CURLOPT_HEADER);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
                $response = curl_exec($ch); 
                curl_close($ch);
                Mage::log('Order has been sent to exmaple.com'); */

                $dbhost = '';
                $dbuser = '';
                $dbpass = '';
                $conn = mysql_connect($dbhost, $dbuser, $dbpass);

                if (!$conn) {
                    die('Could not connect: ' . mysql_error());
                }

                if ($store) {

                    $short_code = 'web10';

                    $orderItemsCollection = $order->getItemsCollection();
                    foreach ($orderItemsCollection as $item) {

                        $productName = $item->getName();
                        $productQty = $item->getQtyOrdered();
                        $productPrice = $item->getPrice();
                        $payment_method = $order->getPayment()->getMethodInstance()->getTitle();

                        $sql = "INSERT INTO crm_order_management (email,product,comment,lname,qty,orderamt,pname,paymode,orderfro,despdate,orderid,status,storename,fname,short_code)
                                                   VALUES ('" . $params['email'] . "','" . $productName . "','" . $params['customerName'] . "','" . $params['customerlastname'] . "','" . $productQty . "','" . $params['total'] . "','" . $params['telephone'] . "','" . $payment_method . "','" . $params['city'] . "','" . $Ordercreated . "','" . $OrderId . "','" . $Getstaus . "','" . $store . "','" . $countryName . "','" . $short_code . "')";
                        mysql_select_db('abortion_word');
                        $retval = mysql_query($sql, $conn);

                        if (!$retval) {
                            die('Could not enter data: ' . mysql_error());
                        }
                    }
                }

                mysql_close($conn);

            } catch (Exception $e) {
                Mage::logException($e);
            }
        }

        return $this;
    }
}
3
  • I'm downvoting this because this code shows an Observer being a Model which is not needed, variables are named in weird ways (starting with capitals, with underscores), everything is combined in a single function (violating SOLID) and a database connection is created using deprecated mysql functions instead of using the DBO system of Magento. Commented Sep 30, 2017 at 16:02
  • 1
    a prime example of why outsourcing to cheap developers is a bad idea Commented Nov 1, 2017 at 11:58
  • Very dirty code. And not answered to start question.
    – newman
    Commented Mar 15, 2022 at 20:24

Your Answer

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

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