/** * Validates the fields in a specified address data object. * * @param \Magento\Quote\Api\Data\AddressInterface $addressData The address data object. * @return bool * @throws \Magento\Framework\Exception\InputException The specified address belongs to another customer. * @throws \Magento\Framework\Exception\NoSuchEntityException The specified customer ID or address ID is not valid. */ public function validate(\Magento\Quote\Api\Data\AddressInterface $addressData) { //validate customer id if ($addressData->getCustomerId()) { $customer = $this->customerFactory->create(); $customer->load($addressData->getCustomerId()); if (!$customer->getId()) { throw new \Magento\Framework\Exception\NoSuchEntityException(__('Invalid customer id %1', $addressData->getCustomerId())); } } // validate address id if ($addressData->getId()) { $address = $this->quoteAddressFactory->create(); $address->load($addressData->getId()); if (!$address->getId()) { throw new \Magento\Framework\Exception\NoSuchEntityException(__('Invalid address id %1', $addressData->getId())); } // check correspondence between customer id and address id if ($addressData->getCustomerId()) { if ($address->getCustomerId() != $addressData->getCustomerId()) { throw new \Magento\Framework\Exception\InputException(__('Address with id %1 belongs to another customer', $addressData->getId())); } } } return true; }
/** * Validates the fields in a specified address data object. * * @param \Magento\Quote\Api\Data\AddressInterface $addressData The address data object. * @return bool * @throws \Magento\Framework\Exception\InputException The specified address belongs to another customer. * @throws \Magento\Framework\Exception\NoSuchEntityException The specified customer ID or address ID is not valid. */ public function validate(\Magento\Quote\Api\Data\AddressInterface $addressData) { //validate customer id if ($addressData->getCustomerId()) { $customer = $this->customerRepository->getById($addressData->getCustomerId()); if (!$customer->getId()) { throw new \Magento\Framework\Exception\NoSuchEntityException(__('Invalid customer id %1', $addressData->getCustomerId())); } } if ($addressData->getCustomerAddressId()) { try { $this->addressRepository->getById($addressData->getCustomerAddressId()); } catch (NoSuchEntityException $e) { throw new \Magento\Framework\Exception\NoSuchEntityException(__('Invalid address id %1', $addressData->getId())); } $applicableAddressIds = array_map(function ($address) { /** @var \Magento\Customer\Api\Data\AddressInterface $address */ return $address->getId(); }, $this->customerRepository->getById($addressData->getCustomerId())->getAddresses()); if (!in_array($addressData->getCustomerAddressId(), $applicableAddressIds)) { throw new \Magento\Framework\Exception\NoSuchEntityException(__('Invalid address id %1', $addressData->getCustomerAddressId())); } } return true; }
/** * Get customer id * * @return int|null */ public function getCustomerId() { return $this->address->getCustomerId(); }