/** * {@inheritdoc} */ public function searchGroups(SearchCriteria $searchCriteria) { $this->_searchResultsBuilder->setSearchCriteria($searchCriteria); $groups = array(); /** @var Collection $collection */ $collection = $this->_groupFactory->create()->getCollection()->addTaxClass(); //Add filters from root filter group to the collection foreach ($searchCriteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $collection); } $this->_searchResultsBuilder->setTotalCount($collection->getSize()); $sortOrders = $searchCriteria->getSortOrders(); if ($sortOrders) { foreach ($searchCriteria->getSortOrders() as $field => $direction) { $field = $this->translateField($field); $collection->addOrder($field, $direction == SearchCriteria::SORT_ASC ? 'ASC' : 'DESC'); } } $collection->setCurPage($searchCriteria->getCurrentPage()); $collection->setPageSize($searchCriteria->getPageSize()); /** @var CustomerGroupModel $group */ foreach ($collection as $group) { $this->_customerGroupBuilder->setId($group->getId())->setCode($group->getCode())->setTaxClassId($group->getTaxClassId())->setTaxClassName($group->getClassName()); $groups[] = $this->_customerGroupBuilder->create(); } $this->_searchResultsBuilder->setItems($groups); return $this->_searchResultsBuilder->create(); }
/** * {@inheritdoc} */ public function searchCustomers(SearchCriteria $searchCriteria) { $this->searchResultsBuilder->setSearchCriteria($searchCriteria); /** @var Collection $collection */ $collection = $this->customerFactory->create()->getCollection(); // This is needed to make sure all the attributes are properly loaded foreach ($this->customerMetadataService->getAllAttributesMetadata() as $metadata) { $collection->addAttributeToSelect($metadata->getAttributeCode()); } // Needed to enable filtering on name as a whole $collection->addNameToSelect(); // Needed to enable filtering based on billing address attributes $collection->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left')->joinAttribute('company', 'customer_address/company', 'default_billing', null, 'left'); //Add filters from root filter group to the collection foreach ($searchCriteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $collection); } $this->searchResultsBuilder->setTotalCount($collection->getSize()); $sortOrders = $searchCriteria->getSortOrders(); /** @var SortOrder $sortOrder */ if ($sortOrders) { foreach ($searchCriteria->getSortOrders() as $sortOrder) { $collection->addOrder($sortOrder->getField(), $sortOrder->getDirection() == SearchCriteria::SORT_ASC ? 'ASC' : 'DESC'); } } $collection->setCurPage($searchCriteria->getCurrentPage()); $collection->setPageSize($searchCriteria->getPageSize()); $customersDetails = []; /** @var CustomerModel $customerModel */ foreach ($collection as $customerModel) { $customer = $this->converter->createCustomerFromModel($customerModel); $addresses = $this->customerAddressService->getAddresses($customer->getId()); $customerDetails = $this->customerDetailsBuilder->setCustomer($customer)->setAddresses($addresses)->create(); $customersDetails[] = $customerDetails; } $this->searchResultsBuilder->setItems($customersDetails); return $this->searchResultsBuilder->create(); }