3

I'm trying to get customer(login)or guest user email address with sales order model, but it reflects blank result . I have been used this code..

$orderData = $observer->getEvent()->getOrder();
            $oincid = $orderData->getIncrementId();
            $order = Mage::getModel('sales/order')->loadByIncrementId($oincid);
            $order->getCustomerEmail();
            echo $order;exit;  
2

Try this:

In your config.xml file:

<config>
<global>
.........
   <events>
            <sales_order_place_after>
                <observers>
                    <any_name>
                        <type>singleton</type>
                        <class>Namespace_ModuleName_Model_Observer</class>
                        <method>getEmail</method>
                    </any_name>
                </observers>
            </sales_order_place_after>
   </events>
..........
</global>
</config>

Then in your Model/Observer.php:

public function getEmail($observer){
   $order= $observer->getEvent()->getOrder();
   if($order->getCustomerId()){
       $email = $order->getCustomerEmail(); //logged in customer
   }
   else{
       $email = $order->getBillingAddress()->getEmail(); //not logged in customer
   }

   //your other logic

   //lets test if it is grabbing email
   Mage::log($order->getId(). " - ". $email, null, 'your_name.log');
}

This will create a file named your_name.log in var/log folder when anyone places order.

If everything is ok, then you will see your customer's email in that file.

Good luck.

  • this is working fine for guest customer but if i want to check that customer register first time then what about that ? – Sarfaraj Sipai Oct 2 '18 at 6:14
  • Above code works in both conditions. Since this sales_order_place_after fires after order is placed, meaning it has also registered customer. – Adarsh Khatri Oct 2 '18 at 6:41
  • but for what about new customer register and order product – Sarfaraj Sipai Oct 2 '18 at 6:45
  • I think, when customer register and order product, system has already saved customer. Which is why it will pass $order->getCustomerId() condition. – Adarsh Khatri Oct 2 '18 at 7:00
  • actually i have to thing 1) customer register and check they have order any product or not 2) new customer order for product – Sarfaraj Sipai Oct 2 '18 at 7:09
0

Step 1:- Define sales_order_place_after event in your module's config.xml

<config>
    <global>
       <events>
            <sales_order_place_after>
                <observers>
                    <get_emailaddress_order>
                        <type>singleton</type>
                        <class>YourNamespace_YourModuleName_Model_Observer</class>
                        <method>getEmailAddress</method>
                    </get_emailaddress_order>
                </observers>
            </sales_order_place_after>
       </events>
    </global>
</config>

Step 2:- Define getEmailAddress() method in your module's model's Observer.php

public function getEmailAddress($observer){
   $order= $observer->getEvent()->getOrder();

   if($order->getCustomerId()){
    // if order is customer order
    $email = $order->getCustomerEmail();
   }else{
    // if order is guest order
    $email = $order->getShippingAddress()->getEmail();
   }

   Mage::log("Email id for order no:- ".$order->getId()." is :- ".$email, null, 'email_address.log');
}
0

Customer emails are also stored with shipping and billing address of customer. Use below code to get email from billing address -

change $order->getCustomerEmail();

to

$order->getBillingAddress()->getEmail(); 

in your code

0

If the code in your question is what you are actually using, it looks like you are trying to print the $order variable rather than the Customer Email

Your Code:

$orderData = $observer->getEvent()->getOrder();
$oincid = $orderData->getIncrementId();
$order = Mage::getModel('sales/order')->loadByIncrementId($oincid);
$order->getCustomerEmail();
echo $order;exit;

Updated Code:

$orderData = $observer->getEvent()->getOrder();
$oincid = $orderData->getIncrementId();
$order = Mage::getModel('sales/order')->loadByIncrementId($oincid);

// This should contain the customer email
echo $order->getCustomerEmail();
exit;  

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.