Magento – Get customer by Tax/VAT number

customergetmodelmagento-1.9

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.

Best Answer

Try using this approach

$result = Mage::getModel('customer/customer')
              ->getCollection()
              ->addAttributeToSelect('vat_‌​id')
              ->addAttributeToFilter('vat_id',$ss_5_last)->load()->getFirstItem();

if (is_object($result)) {
    /* Logic */
}

Keep in mind vat_id is not unique and the code below can load multiple customers without ->getFirstItem().

Related Topic