I'm trying to build a search script to retrieve entries from customer collection. The trick is that the normal "OR" condition works only for the first entry.
This is the code I have so far:
$customers = Mage::getResourceModel('customer/customer_collection')
->addAttributeToSelect('*');
if($_searchTerm = $this->getRequest()->getParam('q')){
$customers->addAttributeToFilter(
array(
array('attribute' => 'firstname', 'like' => $_searchTerm),
array('attribute' => 'lastname', 'like' => $_searchTerm),
array('attribute' => 'email', 'like' => '%' . $_searchTerm . '%'),
array('attribute' => 'phone', 'like' => '%' . $_searchTerm . '%'), /* valid field in my collection*/
)
);
}
I've tried with wildcard "%" also but still no proper result. Most likely i'm missing something here.
Thanks.
Try this, Its 100% working for me
public function customerSearch($data) {
$customerFactory = $this->_objectManager->get('\Magento\Customer\Model\CustomerFactory');
$collection = $customerFactory->create()->getCollection()
->addAttributeToSelect("*")
->addAttributeToFilter('sponsor_id', array('eq' => $this->_customerSession->getCustomer()->getId()))
->addAttributeToFilter(
array(
array('attribute' => 'firstname', 'like' => '%' . $data . '%'),
array('attribute' => 'lastname', 'like' => '%' . $data . '%')
))->load();
$customer = $collection->getData();
echo json_encode($customer);
exit;
}
Where sponsor_id is my custom attribute for customer and $data is simple string to find customer by firstname or lastname. I hope this will resolve your issue. Thanks