0

How to register more than 1 event observer in config.xml If called methods are in same class & file.

  • When using both observer seprately(registering them seprately) both are working. But when both are enabled can't complete checkout process. After selecting payment information continue buton not working in rwd theme.

  • first observer is doing something when payment made successfully. And second observer is printing some text before page load. When using both seprately both are working fine. But when using both at once. Second observer is printing text before body load as it should do. But checkout process can't complete so i don't know second will do work as it should do or not.

  • when i disable my module checkout is working. Even 1 remove 1 observer registry from config.xml then checkout is also working.

      <events>            
          <checkout_onepage_controller_success_action>
                <observers>
                    <Barcatsia_Smsntf_Model_Observer_customFunction>
                        <type>singleton</type>
                        <class>Barcatsia_Smsntf_Model_Observer</class>
                        <method>customFunction</method>
                    </Barcatsia_Smsntf_Model_Observer_customFunction>
                </observers>
           </checkout_onepage_controller_success_action>
    
    
           <controller_action_layout_load_before>
                <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>
           </controller_action_layout_load_before>
        </events>
    

Observer.php

<?php
class Barcatsia_Smsntf_Model_Observer
{
    public function customFunction(Varien_Event_Observer $observer)
    {
        if(Mage::getStoreConfig('testsection/smsntf/Smsntf_module_activation')){
      //  $order = $observer->getEvent()->getOrder();
         //here you can add your custom code
        $layout = Mage::app()->getLayout();

    //IF{ //its working now if is working now

        $messageBlock = $layout->createBlock('core/template', 'smsntf_test');
        $messageBlock->setTemplate('smsntf/red/event_observer/event_observer.phtml');
        $layout->getBlock('before_body_end')->append($messageBlock);

    //}ELSE{
//      $layout->getUpdate()->addHandle('custom_layout_handle'); //refrencing here layout.xml handle
    //}
//      $footer = $layout->getBlock('before_body_end');
//      $footer->setCacheKey(sha1($footer->getCacheKey() . '-payment-message'));


        //NOW GETTING ORDER ID AND OTHER ORDER DETAIL
            $orderIds = $observer->getData('order_ids');

        //  $order     = Mage::getModel('sales/order')->load($_orderId);
         // $customer  = Mage::getModel('customer/customer')->load($order->getData('customer_id'));
            $orderIds = $observer->getData('order_ids');
        //  foreach($orderIds as $_orderId){
            $order     = Mage::getModel('sales/order')->load($orderIds);

    //      print_r($order ->getdata('grand_total'));
    //      print_r($order ->getdata());
        //   }
        //NOW GETTING ORDER ID AND OTHER ORDER DETAIL
        $message_for_send = Mage::getStoreConfig('testsection/smsntf/Smsntf_text_field_msg');
        $http_url = Mage::getStoreConfig('testsection/smsntf/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/smsntf/Smsntf_text_field_Msg_prm').'='.urlencode($message_for_send);
        for($a=0; $a<sizeof($arr_params); $a++){
            if(trim(Mage::getStoreConfig('testsection/smsntf/'.$arr_params[$a]))!=''){
                if(trim($arr_params[$a])!='Smsntf_text_field_mobile')
                    $http_url .= '&'.Mage::getStoreConfig('testsection/smsntf/'.$arr_params[$a]);
                else
                    $http_url .= '&'.Mage::getStoreConfig('testsection/smsntf/'.$arr_params[$a]).'='.$order->getBillingAddress()->getData('telephone');

            }       
        }
    //  echo $http_url;
        //printing system->configuration inputs value
        //      echo Mage::getStoreConfig('testsection/smsntf/active');
                $this->send_sms($http_url);

        }else{
            return false;
        }
    }

    public function send_sms($feed_url/*, $rauth_key, $amobile, $hmsg, $uid, $lid, $content_type*/){
        $mageFilename = 'app/Mage.php';
        require_once $mageFilename;
        umask(0);
        Mage::app();

        $curl = new Varien_Http_Adapter_Curl();
        $curl->setConfig(array(
               'timeout'   => 30 
        ));
    //  $feed_url = 'http://sms.vtechnologies4u.in/rest/services/sendSMS/sendGroupSms?AUTH_KEY=48cdfa49d561561cb65dc5175c3aea&mobileNos=7080557519&message=now+its+working_ninee_step&senderId=Vtechn&routeId=1&smsContentType=english';
    //  $feed_url = 'http://sms.vtechnologies4u.in/rest/services/sendSMS/sendGroupSms?AUTH_KEY='.$rauth_key.'&mobileNos='.$amobile.'&message='.$hmsg.'&senderId='.$uid.'&routeId='.$lid.'&smsContentType='.$content_type;

        $curl->write(Zend_Http_Client::GET, $feed_url, '1.0');
        $data = $curl->read();
        if ($data === false) {
           return false;
        }
        $data = preg_split('/^r?$/m', $data, 2);
        $data = trim($data[1]);
        $curl->close();

        try {
            //  $xml  = new SimpleXMLElement($data);
            //  Parse the XML FEED and output the data
        //  echo "Connection done successfully";
        }
        catch (Exception $e) {
        //   echo $e->getMessage();
        }
    }

    public function smsntf_cancel_order(){
        echo "i will visile every time befoe page load";
    }
}
  • Is it not working? – Rohit Kundale Feb 2 '16 at 12:17
  • when i try same. checout not working properly – Rahul Chaurasia Feb 2 '16 at 12:17
  • Check i have posted an answer – Rohit Kundale Feb 2 '16 at 12:20
  • Please explain, what "not working properly" means. If you can't explain it, this debugging guide should help you: magento.stackexchange.com/questions/428/… - also try to narrow it down, i.e. remove the working observer and maybe more, until you have a minimal reproducible example. Your problem is not that you have defined two observers. – Fabian Schmengler Feb 2 '16 at 12:59
  • i update the question again it was not showing fully its was just showing 1 observer in question. even i was write both thier i think any format problem. please take a look and tell me. – Rahul Chaurasia Feb 3 '16 at 1:29
2

Try removing echo statement from your smsntf_cancel_order function and use some kind of logging instead that doesn't affect the output being sent via the Magento application.

Reason will be, at checkout, controllers send ajax response, but your echo statements are messing up with the response being sent.

  • Thanks prateek. few people can't understand what i want to say. And give a downvote to my question. – Rahul Chaurasia Feb 3 '16 at 7:38
1

The observer identifier, in your case it's the tag Barcatsia_Smsntf_Model_Observer right after the observers tag must be different for each observer you want to create, that's basically it.

  • other things are right? – Rahul Chaurasia Feb 2 '16 at 12:18
  • yes the rest is ok – Raphael at Digital Pianism Feb 2 '16 at 12:18
  • you means second time i should not use observers tag? can i use any tag ? – Rahul Chaurasia Feb 2 '16 at 12:19
  • no you should use a different identifier. Example the first time you can use <Barcatsia_Smsntf_Model_Observer> and the second time it must be different, for example: <Barcatsia_Smsntf_Model_Observer_2> – Raphael at Digital Pianism Feb 2 '16 at 12:20
  • 3
    it only has to be different for each event, because the XML structure gets merged. I don't see any errors in he shown code – Fabian Schmengler Feb 2 '16 at 12:33

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.