I call the observer i.e. customerRegisterSuccess function after successfull registration of customer.In this function I want to get email as well as telephone number of customer.Email successfully fetch by using function
$observer->getEvent()->getCustomer()->getEmail();
But, While fetching telephone number I face too much problems.I use below some function but all are not in use.
function are :
1.$observer->getEvent()->getCustomer()->getTelephone();
2.$observer->getEvent()->getCustomer()->getBillingAddress()->getTelephone();
3.$observer->getEvent()->getCustomer()->getMobileNumber();
4.$observer->getEvent()->getCustomer()->getPrimaryBillingAddress()->getTelephone();
All above function return NULL.Some function get error.
So, please let me know the perfect answer.The telephone no. field present in the address information section of registration form.
3 Answers
Try this in your (Observer) file.
$customer = $observer->getEvent()->getCustomer();
$customer->getAddressesCollection()->getFirstitem()->getTelephone();
Because telephone is an address attribute.
-
Sorry, but i can't figure out how can you get an address attribute value after registration... unless change the registration form in order to add an address form. Have you the customer address form in registration? Commented Dec 24, 2015 at 7:37
-
Check With the Below implantation that you are doing the Rightway
Implementation Steps :
Step 1 : Create Required folders.
Create below folders in
app/code/local/Wli/Registrationdemo/etc, app/code/local/Wli/Registrationdemo/Model.
Wli is company name & Registration demo is module name. You can use your own company name & module name instead of above specified.
Step 2 : Activate Module through module xml file.
Magento requires xml file that tells to magento to look into it & call your custom module based on the xml. So lets create module xml file Wli_Registrationdemo.xml & place the file into the app/etc/modules
directory.
<?xml version="1.0"?>
<config>
<modules>
<Wli_Registrationdemo>
<active>true</active>
<codePool>local</codePool>
</Wli_Registrationdemo>
</modules>
</config>
Step 3 : Create Configuration XML file.
Next step is to create module configuration file config.xml & place it in app/code/local/Wli/Registrationdemo/etc
folder which we have created earlier.
<config>
<modules>
<Wli_Registrationdemo>
<version>0.1.0</version>
</Wli_Registrationdemo>
</modules>
<global>
<events>
<customer_register_success>
<observers>
<Wli_Registrationdemo_customer_register_success>
<type>singleton</type>
<class>Wli_Registrationdemo_Model_Observer</class>
<method>customerRegisterSuccess</method>
</Wli_Registrationdemo_customer_register_success>
</observers>
</customer_register_success>
</events>
</global>
</config>
Step 4 : Create Model Class (Observer) file.
<?php
class Wli_Registrationdemo_Model_Observer {
public function customerRegisterSuccess(Varien_Event_Observer $observer) {
$event = $observer->getEvent();
$customer = $event->getCustomer();
$email = $customer->getEmail();
if($email) {
//Create custom code over here as per your requirements !
}
}
}
?>
-
I use the same procedure you have given.Now my problem is that I want telephone no. as well in customerRegisterSuccess function. I use $customer->getTelephone(); but its not fetch telephone no. here Commented Dec 24, 2015 at 6:16
-
Do you have phone number set as customer attribute ? Commented Dec 24, 2015 at 6:26
"Out of the box" Magento Customer Entity hasn't Telephone Attribute, you need to look for this attribute in the customer address entity, but at this point (customerRegisterSuccess) you don't have any address data because the customer must fill the telephone when set an address (not in registration) so you have two options:
Create a Customer Attribute for telephone and set it as required, then you will get it in your observer.
Change the observer to some other event like "customer_address_save_after" in order to get the telephone from the address object.
-
This is great solution.But it requires to add attribute or create new observer function.anyway thanks for the reply sir.. Commented Dec 24, 2015 at 7:18