Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locate customer by custom attribute in Magento

Tags:

magento

I have a custom attribute called rms_id. It's a unique account number that's associated with our customers in another database. I need a way to lookup the customer by this id, without hitting the database manually.

As of now, I can load the customer by email address like so:

$customer->loadByEmail($data['email']);

This is great, however the email is not always available. The RMS id is. Is there any way to load a user by the custom attribute?

In theory, the following would work:

$customer->loadByRmsId($data['account_id']);

However it errors out. Any help would be much appreciated.

like image 619
Nick Parsons Avatar asked Nov 30 '22 22:11

Nick Parsons


2 Answers

but in practice you have to come up with your own method similar to loadByEmail() in app/code/core/Mage/Customer/Model/Resource/Customer.php

or get the e-mail from collection

Mage::getModel('customer/customer')->getCollection()->addFieldToFilter('rms_id', 'youremailhere')->load();
like image 37
Anton S Avatar answered Dec 05 '22 21:12

Anton S


There is a good example of this in the core; see Mage_Catalog_Model_Abstract::loadByAttribute()[link]. It involves using a data model to retrieve a collection, joining the attribute in, and filtering by that attribute. This is necessary if the attribute is not static i.e. not a part of the entity table.

$result = Mage::getModel('customer/customer')
              ->getCollection()
              ->addAttributeToSelect('rms_‌​id')
              ->addAttributeToFilter('rms_id',{Val})->load();

if (is_object($result)) {
    /* Logic */
}
like image 120
benmarks Avatar answered Dec 05 '22 20:12

benmarks