4

What is the best way to load Customer by custom attribute? I create custom module. What should I overwrite?

  • Model/Customer
  • Model/ResourceModel/Customer
  • Model/ResourceModel/CustomerRepository

What is the best way?

1 Answer 1

7

We should try with Service Contracts Layer.

Take a look: vendor/magento/module-backend/Model/Search/Customer::load()

    $searchFields = ['firstname', 'lastname', 'company'];
    $filters = [];
    foreach ($searchFields as $field) {
        $filters[] = $this->filterBuilder
            ->setField($field)
            ->setConditionType('like')
            ->setValue($this->getQuery() . '%')
            ->create();
    }
    $this->searchCriteriaBuilder->addFilters($filters);
    $searchCriteria = $this->searchCriteriaBuilder->create();
    $searchResults = $this->customerRepository->getList($searchCriteria);

We can use Magento\Customer\Api\CustomerRepositoryInterface::getList() to retrieve customers which match a specified criteria.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.