addressFactory = $addressFactory; $this->addressRegistry = $addressRegistry; $this->customerRegistry = $customerRegistry; $this->addressResourceModel = $addressResourceModel; $this->directoryData = $directoryData; $this->addressSearchResultsFactory = $addressSearchResultsFactory; $this->addressCollectionFactory = $addressCollectionFactory; $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor; $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor(); } /** * Save customer address. * * @param \Magento\Customer\Api\Data\AddressInterface $address * @return \Magento\Customer\Api\Data\AddressInterface * @throws \Magento\Framework\Exception\LocalizedException */ public function save(\Magento\Customer\Api\Data\AddressInterface $address) { $addressModel = null; $customerModel = $this->customerRegistry->retrieve($address->getCustomerId()); if ($address->getId()) { $addressModel = $this->addressRegistry->retrieve($address->getId()); } if ($addressModel === null) { /** @var \Magento\Customer\Model\Address $addressModel */ $addressModel = $this->addressFactory->create(); $addressModel->updateData($address); $addressModel->setCustomer($customerModel); } else { $addressModel->updateData($address); } $addressModel->setStoreId($customerModel->getStoreId()); $errors = $addressModel->validate(); if ($errors !== true) { $inputException = new InputException(); foreach ($errors as $error) { $inputException->addError($error); } throw $inputException; } $addressModel->save(); $address->setId($addressModel->getId()); // Clean up the customer registry since the Address save has a // side effect on customer : \Magento\Customer\Model\ResourceModel\Address::_afterSave $this->addressRegistry->push($addressModel); $this->updateAddressCollection($customerModel, $addressModel); return $addressModel->getDataModel(); } /** * Update address collection. * * @param Customer $customer * @param Address $address * @throws \Magento\Framework\Exception\LocalizedException * @return void */ private function updateAddressCollection(CustomerModel $customer, CustomerAddressModel $address) { $customer->getAddressesCollection()->removeItemByKey($address->getId()); $customer->getAddressesCollection()->addItem($address); } /** * Retrieve customer address. * * @param int $addressId * @return \Magento\Customer\Api\Data\AddressInterface * @throws \Magento\Framework\Exception\LocalizedException */ public function getById($addressId) { $address = $this->addressRegistry->retrieve($addressId); return $address->getDataModel(); } /** * Retrieve customers addresses matching the specified criteria. * * @param SearchCriteriaInterface $searchCriteria * @return \Magento\Customer\Api\Data\AddressSearchResultsInterface * @throws \Magento\Framework\Exception\LocalizedException */ public function getList(SearchCriteriaInterface $searchCriteria) { /** @var Collection $collection */ $collection = $this->addressCollectionFactory->create(); $this->extensionAttributesJoinProcessor->process( $collection, \Magento\Customer\Api\Data\AddressInterface::class ); $this->collectionProcessor->process($searchCriteria, $collection); /** @var \Magento\Customer\Api\Data\AddressInterface[] $addresses */ $addresses = []; /** @var \Magento\Customer\Model\Address $address */ foreach ($collection->getItems() as $address) { $addresses[] = $this->getById($address->getId()); } /** @var \Magento\Customer\Api\Data\AddressSearchResultsInterface $searchResults */ $searchResults = $this->addressSearchResultsFactory->create(); $searchResults->setItems($addresses); $searchResults->setSearchCriteria($searchCriteria); $searchResults->setTotalCount($collection->getSize()); return $searchResults; } /** * Helper function that adds a FilterGroup to the collection. * * @deprecated 101.0.0 * @param FilterGroup $filterGroup * @param Collection $collection * @return void * @throws \Magento\Framework\Exception\InputException */ protected function addFilterGroupToCollection(FilterGroup $filterGroup, Collection $collection) { $fields = []; $conditions = []; foreach ($filterGroup->getFilters() as $filter) { $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq'; $fields[] = ['attribute' => $filter->getField(), $condition => $filter->getValue()]; $conditions[] = [$condition => $filter->getValue()]; } if ($fields) { $collection->addFieldToFilter($fields, $conditions); } } /** * Delete customer address. * * @param \Magento\Customer\Api\Data\AddressInterface $address * @return bool true on success * @throws \Magento\Framework\Exception\LocalizedException */ public function delete(\Magento\Customer\Api\Data\AddressInterface $address) { $addressId = $address->getId(); $address = $this->addressRegistry->retrieve($addressId); $customerModel = $this->customerRegistry->retrieve($address->getCustomerId()); $customerModel->getAddressesCollection()->clear(); $this->addressResourceModel->delete($address); $this->addressRegistry->remove($addressId); return true; } /** * Delete customer address by ID. * * @param int $addressId * @return bool true on success * @throws \Magento\Framework\Exception\NoSuchEntityException * @throws \Magento\Framework\Exception\LocalizedException */ public function deleteById($addressId) { $address = $this->addressRegistry->retrieve($addressId); $customerModel = $this->customerRegistry->retrieve($address->getCustomerId()); $customerModel->getAddressesCollection()->removeItemByKey($addressId); $this->addressResourceModel->delete($address); $this->addressRegistry->remove($addressId); return true; } /** * Retrieve collection processor * * @deprecated 101.0.0 * @return CollectionProcessorInterface */ private function getCollectionProcessor() { if (!$this->collectionProcessor) { $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get( 'Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor' ); } return $this->collectionProcessor; } }