/** * {@inheritdoc} */ public function setAddress($cartId, $addressData) { /** @var \Magento\Sales\Model\Quote $quote */ $quote = $this->quoteLoader->load($cartId, $this->storeManager->getStore()->getId()); if ($quote->isVirtual()) { throw new NoSuchEntityException('Cart contains virtual product(s) only. Shipping address is not applicable'); } /** @var \Magento\Sales\Model\Quote\Address $address */ $address = $this->quoteAddressFactory->create(); $this->addressValidator->validate($addressData); if ($addressData->getId()) { $address->load($addressData->getId()); } $address = $this->addressConverter->convertDataObjectToModel($addressData, $address); $address->setSameAsBilling(0); $quote->setShippingAddress($address); $quote->setDataChanges(true); try { $quote->save(); } catch (\Exception $e) { $this->logger->logException($e); throw new InputException('Unable to save address. Please, check input data.'); } return $quote->getShippingAddress()->getId(); }
/** * {@inheritdoc} */ public function setAddress($cartId, $addressData) { /** @var \Magento\Sales\Model\Quote $quote */ $quote = $this->quoteRepository->get($cartId); /** @var \Magento\Sales\Model\Quote\Address $address */ $address = $this->quoteAddressFactory->create(); $this->addressValidator->validate($addressData); if ($addressData->getId()) { $address->load($addressData->getId()); } $address = $this->addressConverter->convertDataObjectToModel($addressData, $address); $quote->setBillingAddress($address); $quote->setDataChanges(true); try { $quote->save(); } catch (\Exception $e) { $this->logger->logException($e); throw new InputException('Unable to save address. Please, check input data.'); } return $quote->getBillingAddress()->getId(); }
public function testValidateWithValidAddress() { $addressCustomer = 100; $addressId = 100; /** Address data object */ $addressData = $this->addressDataBuilder->setId($addressId)->setCompany('eBay Inc')->setCustomerId($addressCustomer)->create(); /** Customer mock */ $this->customerFactoryMock->expects($this->once())->method('create')->will($this->returnValue($this->customerMock)); $this->customerMock->expects($this->once())->method('load')->with($addressCustomer); $this->customerMock->expects($this->once())->method('getId')->will($this->returnValue($addressCustomer)); /** Quote address mock */ $this->addressFactoryMock->expects($this->once())->method('create')->will($this->returnValue($this->quoteAddressMock)); $this->quoteAddressMock->expects($this->once())->method('load')->with($addressId); $this->quoteAddressMock->expects($this->once())->method('getId')->will($this->returnValue($addressId)); $this->quoteAddressMock->expects($this->any())->method('getCustomerId')->will($this->returnValue($addressCustomer)); /** Validate */ $this->model->validate($addressData); }