/**
  * 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()));
         }
     }
     // validate address id
     if ($addressData->getId()) {
         try {
             $address = $this->addressRepository->getById($addressData->getId());
         } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
             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\NoSuchEntityException(__('Invalid address id %1', $addressData->getId()));
             }
         }
     }
     if ($addressData->getCustomerAddressId()) {
         $applicableAddressIds = array_map(function ($address) {
             /** @var \Magento\Customer\Api\Data\AddressInterface $address */
             return $address->getId();
         }, $this->customerSession->getCustomerDataObject()->getAddresses());
         if (!in_array($addressData->getCustomerAddressId(), $applicableAddressIds)) {
             throw new \Magento\Framework\Exception\NoSuchEntityException(__('Invalid address id %1', $addressData->getCustomerAddressId()));
         }
     }
     return true;
 }
Ejemplo n.º 3
0
 /**
  * @param \Magento\Quote\Api\Data\AddressInterface $address
  * @return $this
  */
 public function addAddress(\Magento\Quote\Api\Data\AddressInterface $address)
 {
     $address->setQuote($this);
     if (!$address->getId()) {
         $this->getAddressesCollection()->addItem($address);
     }
     return $this;
 }