1

How to get order id and other details of order for sales_order_payment_cancel event observer. i know how to get them for checkout_onepage_controller_success_action. But that method is not working in it. And showing this error.

Fatal error: Call to a member function getId() on a non-object in /home/orderitonline/public_html/includes/src/Barcatsia_Smsntf_Model_Observer.php on line 108

<sales_order_payment_cancel>
                <observers>
                    <Barcatsia_Smsntf_Model_Observer_cancelOrder>
                        <type>singleton</type>
                        <class>Barcatsia_Smsntf_Model_Observer</class>
                        <method>smsntf_cancel_order</method>
                    </Barcatsia_Smsntf_Model_Observer_cancelOrder>
                </observers>
           </sales_order_payment_cancel>

-

public function smsntf_cancel_order($observer){


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




        if(Mage::getStoreConfig

('testsection/cancel_order/Smsntf_module_activation')){

        $layout = Mage::app()->getLayout();


            $order     = Mage::getModel('sales/order')->load($orderIds);            

        $message_for_send = Mage::getStoreConfig

('testsection/cancel_order/Smsntf_text_field_msg');
        $http_url = Mage::getStoreConfig

('testsection/cancel_order/Smsntf_text_field_api_calling_url');
        $arr_params = array('Smsntf_text_field_authkey', 'Smsntf_text_field_mobile', 

'Smsntf_text_field_senderid', 'Smsntf_text_field_routeid', 'Smsntf_text_field_additional', 

'Smsntf_text_field_additional', 'Smsntf_text_field_additional1', 

'Smsntf_text_field_additional2', 'Smsntf_text_field_additional3', 

'Smsntf_text_field_additional4');

        //replaceing %O% with Order Number
        $message_for_send = str_replace('%O%',100000000+intval($orderIds[0]),

$message_for_send);

        //replaceing %N% with Mobile Number
        $message_for_send = str_replace('%N%',$order->getdata

('customer_firstname'),$message_for_send);

        //replaceing %A% with Grand Total
        $message_for_send = str_replace('%A%',$order->getdata('grand_total'),

$message_for_send);

        $http_url .='?'. Mage::getStoreConfig

('testsection/cancel_order/Smsntf_text_field_Msg_prm').'='.urlencode($message_for_send);
        for($a=0; $a<sizeof($arr_params); $a++){
            if(trim(Mage::getStoreConfig('testsection/cancel_order/'.

$arr_params[$a]))!=''){
                if(trim($arr_params[$a])!='Smsntf_text_field_mobile')
                    $http_url .= '&'.Mage::getStoreConfig

('testsection/cancel_order/'.$arr_params[$a]);
                else
                    $http_url .= '&'.Mage::getStoreConfig

('testsection/cancel_order/'.$arr_params[$a]).'='.$order->getBillingAddress()->getData

('telephone');
            }       
        }

        //printing system->configuration inputs value
        //      echo Mage::getStoreConfig

('testsection/cancel_order/active');
                $this->send_sms($http_url);

        }else{
            return false;
        }
    } 
  • Can please show code of Barcatsia_Smsntf_Model_Observer – Amit Bera Feb 9 '16 at 9:50
  • try $Observer->getPayment()->getOrder()->getId() – Qaisar Satti Feb 9 '16 at 10:35
2

If you want to get order object from sales_order_payment_cancel at observer then you can try:

$OrderObject=$observer->getEvent()->getPayment()->getOrder()

And from order object $OrderObject,you can get all order related details

Edit:

just change $observer->getEvent()->getOrder() to $observer->getEvent()->getPayment()->getOrder()

  • when i am printing whole object data is too much long and page get hanged. that is why i am asking how to get order id – Rahul Chaurasia Feb 9 '16 at 9:57
  • for getting id try $order->getId() and Increment id $order->getIncrementId() – Amit Bera Feb 9 '16 at 9:58
  • why its give same error in third line? $payment = $observer->getEvent()->getPayment(); $order = $payment->getOrder(); $orderIds = $order->getId(); – Rahul Chaurasia Feb 9 '16 at 10:07
  • i changed this code $observer->getEvent()->getOrder() to $Observer->getEvent()->getPayment()->getOrder() Now its showing this error Fatal error: Call to a member function getEvent() on a non-object in – Rahul Chaurasia Feb 9 '16 at 10:41
  • just variable issue $Observer should be $observer – Amit Bera Feb 9 '16 at 10:55
2

Observers will give you different variables, so be careful when you use them. In your case, this observer only passes the payment information Mage::dispatchEvent('sales_order_payment_cancel', array('payment' => $this));. So what you are after is:

$payment = $observer->getEvent()->getPayment();
$order = $payment->getOrder();
  • i know its return order object. but how to get order id from order object – Rahul Chaurasia Feb 9 '16 at 9:56
  • Just do $orderId = $order->getId(); – Mayers Feb 9 '16 at 9:57
  • why its give same error in third line? $payment = $observer->getEvent()->getPayment(); $order = $payment->getOrder(); $orderIds = $order->getId(); – Rahul Chaurasia Feb 9 '16 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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