2

I would like to send an sms on every successful registration. Below is my code. But I am not sure whether the method is triggered after successful registration.

app\etc\modules\Php_Sms.xml

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

Inside local code pool, I have created namespace as Php and module as Sms.

code\local\Php\Sms\etc\config.xml as below

    <config>
    <modules>
        <Php_Sms>
            <version>0.0.1</version>
        </Php_Sms>
    </modules>
    <global>
        <events>
            <customer_register_success>
                <observers>
                    <Php_Sms_customer_register_success>
                        <type>singleton</type>
                        <class>Php_Sms_Model_Observer</class>
                        <method>customerRegisterSuccess</method>
                    </Php_Sms_customer_register_success>
                </observers>
            </customer_register_success>
        </events>
    </global>
</config>

code\local\Php\Sms\Model\Observer.php as below

 <?php
class Ucs_GlobalSms_Model_Observer {
  public function customerRegisterSuccess(Varien_Event_Observer $observer) {
      $event = $observer->getEvent();
      $customer = $event->getCustomer();
      $email = $customer->getEmail();
          Mage::log('Successfully logged in', null, 'mylogfile.log');



      if($email) {
      }
  }
}
?>

Mage\customer\controllers\AccountController.php

/**
 * Dispatch Event
 *
 * @param Mage_Customer_Model_Customer $customer
 */
protected function _dispatchRegisterSuccess($customer)
{
    Mage::log('Successfully logged in'.$flag, null, 'syncProduct.log');
    Mage::dispatchEvent('customer_register_success',
        array('account_controller' => $this, 'customer' => $customer)
    );
}

But mylogfile.log is not at all creating. I guess customerRegisterSuccess is not triggered, should I do something to enable _dispatchRegisterSuccess method?

My questions are,

  1. Will magento log work on Observer.php?
  2. How to know whether my custom method triggered?
  3. Why my sms is not sending? (No issues with SMS api. It works fine in core php file)
9
  • my you removed <models> defination from the config.xml May 26, 2015 at 13:00
  • as per technet.weblineindia.com/web/… I removed that
    – Butterfly
    May 26, 2015 at 13:03
  • no no you have to add defination of model since you are using the observer May 26, 2015 at 13:04
  • okay, I will try with it now. Sorry
    – Butterfly
    May 26, 2015 at 13:07
  • add model tag refer my answer May 26, 2015 at 13:08

4 Answers 4

2

you have to change <class>Php_Sms_Model_Observer</class> to <class>Php_Sms_Model</class>

<global>
    <models>
        <custommodule>
            <class>Php_Sms_Model</class>
        </custommodule>
    </models>
</global>
6
  • <events>...</events> tag should be in global tag or frontend tag?
    – Butterfly
    May 26, 2015 at 13:27
  • global is fine.. May 26, 2015 at 13:28
  • I added a Mage::log(...) inside _dispatchRegisterSuccess method in AccountController.php, but that log itself not created, should I do something to enable _dispatchRegisterSuccess method?
    – Butterfly
    May 26, 2015 at 13:30
  • instead of log try to put echo 'somestring'; exit; just to check wthr the function is executing or not May 26, 2015 at 13:31
  • 1
    log is not working may be because you haven't enabled login in the backend.go to system > configuration > developers their you need to enable the log May 26, 2015 at 13:51
2

update your config.xml with this one

<config>
    <modules>
        <Php_Sms>
            <version>0.0.1</version>
        </Php_Sms>
    </modules>
    <global>
        <models>
            <custommodule>
                <class>Php_Sms_Model</class>  <!--issue here-->
            </custommodule>
        </models>
    </global>
    <frontend>
        <events>
            <customer_register_success>
                <observers>
                    <Php_Sms_customer_register_success>
                        <type>singleton</type>
                        <class>Php_Sms_Model_Observer</class>
                        <method>customerRegisterSuccess</method>
                    </Php_Sms_customer_register_success>
                </observers>
            </customer_register_success>
        </events>
        <routers>
            <Php_Sms><!--issue here-->
                <use>standard</use>
                <args>
                    <module>Php_Sms</module>
                    <frontName>php_sms</frontName>
                </args>
            </Php_Sms>
        </routers>
    </frontend>
</config>

Update app\etc\modules\Php_Sms.xml

<config>
    <modules>
        <Php_Sms>
            <active>true</active>
            <codePool>local</codePool>
        </Php_Sms>
    </modules>
</config>
4
  • modified the class attribute, but I added a code to write a log inside customerRegisterSuccess method. But after successful registration Log itself not created.
    – Butterfly
    May 26, 2015 at 11:10
  • Please check the system.log error on my update question.
    – Butterfly
    May 26, 2015 at 11:52
  • update both files from my answer @version_0.1
    – Keyur Shah
    May 26, 2015 at 12:15
  • just now I noticed, if I put a log inside protected function _dispatchRegisterSuccess($customer) in Mage/Customer/contollers/AccountController.php, log is not printed. I guess customerRegisterSuccess is not triggered, should I do something to enable _dispatchRegisterSuccess method?
    – Butterfly
    May 26, 2015 at 12:33
1
  1. Will magento log work on Observer.php?

Yes

  1. How to know whether my custom method triggered?

Just try it out:

require 'Mage.php';
Mage::app();
Mage::dispatchEvent('whatever');
  1. Why my sms is not sending? (No issues with SMS api. It works fine in core php file)

if the code doesn't run, the code doesn't run.

Are you sure you cleaned the cache, so your config is up to date and magento knows about your observer?

2
  • I added a code to write a log inside customerRegisterSuccess method. But after successful registration Log itself not created.
    – Butterfly
    May 26, 2015 at 11:08
  • can you please look into my updated question
    – Butterfly
    May 26, 2015 at 12:56
1

How do you test this? The event won't be dispatched if you create new user account in admin area. Also check your system.log if there's no errors, especially with event dispatching.

What is that controller in your config.xml? If you use custom controller to perform user registration, make sure to let it dispatch this event (or some custom one) if you want to use it.

Edit: What is this controller used for?

    <routers>
        <userregistration>
            <use>standard</use>
            <args>
                <module>UCS_UserRegistration</module>
                <frontName>userregistration</frontName>
            </args>
        </userregistration>
    </routers>
6
  • Please check the system.log error on my update question.
    – Butterfly
    May 26, 2015 at 11:52
  • There's some error in your xml config. Validate xml files you have edited, because it's not even misconfiguration, but a problem with the files' structure - it may cause your config not being loaded at all.
    – Lanius
    May 26, 2015 at 11:56
  • Yes I understand that. But unable to find any defects on xml, i referred app/etc/Php_Sms.xml and Php/sms/etc/config.xml, anything else>
    – Butterfly
    May 26, 2015 at 11:59
  • Try to search your all xml files for &lt; and &gt;. I'm not sure if they're only logged this way or really some of your files got html_escaped (?). I ta may also encoding issue. What editor are you using?
    – Lanius
    May 26, 2015 at 12:04
  • I found the root cause of the issue. inside Php\Sms\etc\config.xml <?xml version="1.0" encoding="UTF-8"?> header should not be there, but after removing this also, method customerRegisterSuccess is not triggered
    – Butterfly
    May 26, 2015 at 12:07

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.