/** * Validate data object fields * * @param \Magento\Checkout\Service\V1\Data\Cart\Address $addressData * @return bool * @throws \Magento\Framework\Exception\InputException * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function validate($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 ' . $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 ' . $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 ' . $addressData->getId() . ' belongs to another customer'); } } } return true; }
/** * Convert address data object to quote address model * * @param \Magento\Checkout\Service\V1\Data\Cart\Address $dataObject * @param \Magento\Sales\Model\Quote\Address $address * @return \Magento\Sales\Model\Quote\Address */ public function convertDataObjectToModel($dataObject, $address) { $address->setData($dataObject->__toArray()); //set custom attributes $customAttributes = $dataObject->getCustomAttributes(); /** @var \Magento\Framework\Service\Data\AttributeValue $attributeData */ foreach ($customAttributes as $attributeData) { $address->setData($attributeData->getAttributeCode(), $attributeData->getValue()); } //set fields with custom logic $address->setStreet($dataObject->getStreet()); $address->setRegionId($dataObject->getRegion()->getRegionId()); $address->setRegion($dataObject->getRegion()->getRegion()); return $address; }