Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Customer.php
Go to the documentation of this file.
1 <?php
7 
26 {
32  protected $_adminhtmlData = null;
33 
38 
43 
47  protected $filterBuilder;
48 
53 
63  public function __construct(
64  \Magento\Backend\Helper\Data $adminhtmlData,
65  \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
66  \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
67  \Magento\Framework\Api\FilterBuilder $filterBuilder,
68  \Magento\Customer\Helper\View $customerViewHelper
69  ) {
70  $this->_adminhtmlData = $adminhtmlData;
71  $this->customerRepository = $customerRepository;
72  $this->searchCriteriaBuilder = $searchCriteriaBuilder;
73  $this->filterBuilder = $filterBuilder;
74  $this->_customerViewHelper = $customerViewHelper;
75  }
76 
82  public function load()
83  {
84  $result = [];
85  if (!$this->hasStart() || !$this->hasLimit() || !$this->hasQuery()) {
86  $this->setResults($result);
87  return $this;
88  }
89 
90  $this->searchCriteriaBuilder->setCurrentPage($this->getStart());
91  $this->searchCriteriaBuilder->setPageSize($this->getLimit());
92  $searchFields = ['firstname', 'lastname', 'company'];
93  $filters = [];
94  foreach ($searchFields as $field) {
95  $filters[] = $this->filterBuilder
96  ->setField($field)
97  ->setConditionType('like')
98  ->setValue($this->getQuery() . '%')
99  ->create();
100  }
101  $this->searchCriteriaBuilder->addFilters($filters);
102  $searchCriteria = $this->searchCriteriaBuilder->create();
103  $searchResults = $this->customerRepository->getList($searchCriteria);
104 
105  foreach ($searchResults->getItems() as $customer) {
106  $customerAddresses = $customer->getAddresses();
108  $company = null;
109  foreach ($customerAddresses as $customerAddress) {
110  if ($customerAddress->getId() == $customer->getDefaultBilling()) {
111  $company = $customerAddress->getCompany();
112  break;
113  }
114  }
115  $result[] = [
116  'id' => 'customer/1/' . $customer->getId(),
117  'type' => __('Customer'),
118  'name' => $this->_customerViewHelper->getCustomerName($customer),
119  'description' => $company,
120  'url' => $this->_adminhtmlData->getUrl('customer/index/edit', ['id' => $customer->getId()]),
121  ];
122  }
123  $this->setResults($result);
124  return $this;
125  }
126 }
$customer
Definition: customers.php:11
__()
Definition: __.php:13
__construct(\Magento\Backend\Helper\Data $adminhtmlData, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder, \Magento\Framework\Api\FilterBuilder $filterBuilder, \Magento\Customer\Helper\View $customerViewHelper)
Definition: Customer.php:63
$searchCriteria
$filters
Definition: uploader.phtml:11
$customerAddress