BenB
BenB

Reputation: 2907

Magento get customer by Tax/VAT number field

I am looking to something similar to this one (thats works with email), But instead of get the customer by email, i want to get him by the Tax/VAT number.

$customer = Mage::getModel('customer/customer')->setWebsiteId($website->getWebsiteId())->loadByEmail($customerEmail);

I found this example but its doesn't work.

$customer = Mage::getModel('customer/customer')->getCollection()->addAttributeToSelect('*')->addFieldToFilter('taxvat',  $ss_5_last)->load();

Thank you.

Upvotes: 1

Views: 2384

Answers (1)

webster
webster

Reputation: 99

If the attribute taxvat contains unique values, your approach was almost right. But the result of your second try returns a collection. I think you need a customer object, so try this one:

$customer = Mage::getModel('customer/customer')
            ->getCollection()
            ->addAttributeToSelect('*')
            ->addFieldToFilter('taxvat',  $ss_5_last)
            ->getFirstItem();

This returns a customer object you can work with. Caution: If taxvat does NOT contain unique values, this method could return the wrong customer, because it always returns the first item of a collection with more than one item.

Upvotes: 3

Related Questions