Get information about customer by email id in magento
Asked Answered
C

1

4

I want to get information of customer by email id, so i create a method in controller with content:

public function showAction(){
    $customer_email = "[email protected]";
    $customer = Mage::getModel("customer/customer");
    $customer->setWebsiteId(Mage::app()->getWebsite()->getId());
    $customer->loadByEmail($customer_email);
    echo $customer->getId();
    echo $customer->getFirstName();
    echo $customer->getEmail(); 
}

but when run it return null value, i don't know why?. please help me

Coreycorf answered 17/6, 2013 at 13:52 Comment(6)
Do you run multiple websites ? If so, are customer accounts shared globally or by website ?Largess
Not run multiple websitesCoreycorf
Mage::app()->getWebsite()->getId() always return value 0Coreycorf
Are you sure that a customer with this email address exists ? What does it give when you comment out the setWebsiteId() call ?Largess
@rocky: Did you try to fetch customer information through another email id ? As i can see there is no error in code except getFirstName() should be getFirstname().Rempe
when i using $customer->setWebsiteId(1), it's return correct email of customer but i using $customer->setWebsiteId(Mage::app()->getWebsite()->getId()), it's return null.Coreycorf
L
10

In your system configuration, customer accounts are shared by website, so the loadByEmail method needs to be used on a customer model that has a value for website_id, and this website ID must correspond to the website to which the customer is associated.

Or, as your controller seems to be an admin one, Mage::app()->getWebsite()->getId() returns 0, which does not correspond.

So, your solution is either to share customer accounts globally (System > Configuration > Customers > Customer Configuration > Account Sharing Options), as it won't change much things if you only run a single website, either to use a website ID that has to be specified by an user, or at least not retrieved by Mage::app()->getWebsite()->getId().

Largess answered 17/6, 2013 at 14:41 Comment(1)
Clearly answer. Thanks you so muchCoreycorf

© 2022 - 2024 — McMap. All rights reserved.