/** * Make sure customer is valid, if logged in * * By default will add error messages and redirect to customer edit form * * @param bool $redirect - stop dispatch and redirect? * @param bool $addErrors - add error messages? * @return bool */ protected function _preDispatchValidateCustomer($redirect = true, $addErrors = true) { try { $customerId = $this->_customerSession->getCustomerId(); $customer = $this->_customerAccountService->getCustomer($customerId); } catch (NoSuchEntityException $e) { return true; } if (isset($customer)) { $validationResult = $this->_customerAccountService->validateCustomerData($customer, $this->_customerMetadataService->getAllAttributesMetadata()); if (!$validationResult->isValid()) { if ($addErrors) { foreach ($validationResult->getMessages() as $error) { $this->messageManager->addError($error); } } if ($redirect) { $this->_redirect('customer/account/edit'); $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true); } return false; } } return true; }
/** * Return array of additional account data * Value is option style array * * @return array */ public function getCustomerAccountData() { $accountData = array(); $entityType = 'customer'; foreach ($this->_customerMetadataService->getAllAttributesMetadata($entityType) as $attribute) { /* @var $attribute \Magento\Customer\Service\V1\Data\Eav\AttributeMetadata */ if (!$attribute->isVisible() || $attribute->isSystem()) { continue; } $orderKey = sprintf('customer_%s', $attribute->getAttributeCode()); $orderValue = $this->getOrder()->getData($orderKey); if ($orderValue != '') { $metadataElement = $this->_metadataElementFactory->create($attribute, $orderValue, $entityType); $value = $metadataElement->outputValue(AttributeDataFactory::OUTPUT_FORMAT_HTML); $sortOrder = $attribute->getSortOrder() + $attribute->isUserDefined() ? 200 : 0; $sortOrder = $this->_prepareAccountDataSortOrder($accountData, $sortOrder); $accountData[$sortOrder] = array('label' => $attribute->getFrontendLabel(), 'value' => $this->escapeHtml($value, array('br'))); } } ksort($accountData, SORT_NUMERIC); return $accountData; }
/** * {@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(); }