/** * 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; }
/** * Return Sales Quote Address model (shipping address) * * @return \Magento\Quote\Model\Quote\Address */ public function getAddress() { if ($this->_address === null) { if ($this->isCustomerLoggedIn()) { $this->_address = $this->getQuote()->getShippingAddress(); } else { $this->_address = $this->_addressFactory->create(); } } return $this->_address; }
/** * Update Customer name in Quote */ protected function _updateQuoteCustomerName() { /** @var $emptyAddress \Magento\Quote\Model\Quote\Address */ $emptyAddress = $this->_quoteAddressFactory->create(); $emptyAddress->setFirstname(null); $emptyAddress->setLastname(null); $this->_block->getQuote()->setBillingAddress($emptyAddress); $customer = $this->_customerRepository->getById(self::FIXTURE_CUSTOMER_ID); $customer->setFirstname(self::SAMPLE_FIRST_NAME)->setLastname(self::SAMPLE_LAST_NAME); $this->_block->getQuote()->setCustomer($customer); $this->_block->getQuote()->save(); $this->assertEquals(self::SAMPLE_FIRST_NAME, $this->_block->getFirstname()); $this->assertEquals(self::SAMPLE_LAST_NAME, $this->_block->getLastname()); }
/** * Return Sales Quote Address model * * @return \Magento\Quote\Model\Quote\Address */ public function getAddress() { if ($this->_address === null) { if ($this->isCustomerLoggedIn()) { $this->_address = $this->getQuote()->getBillingAddress(); if (!$this->_address->getFirstname()) { $this->_address->setFirstname($this->getQuote()->getCustomer()->getFirstname()); } if (!$this->_address->getLastname()) { $this->_address->setLastname($this->getQuote()->getCustomer()->getLastname()); } } else { $this->_address = $this->_addressFactory->create(); } } return $this->_address; }
/** * Add quote item to specific shipping address based on customer address id * * @param int $quoteItemId * @param array $data array('qty'=>$qty, 'address'=>$customerAddressId) * @throws \Magento\Framework\Exception\LocalizedException * @return \Magento\Multishipping\Model\Checkout\Type\Multishipping * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function _addShippingItem($quoteItemId, $data) { $qty = isset($data['qty']) ? (int) $data['qty'] : 1; //$qty = $qty > 0 ? $qty : 1; $addressId = isset($data['address']) ? $data['address'] : false; $quoteItem = $this->getQuote()->getItemById($quoteItemId); if ($addressId && $quoteItem) { if (!$this->isAddressIdApplicable($addressId)) { throw new LocalizedException(__('Please check shipping address information.')); } /** * Skip item processing if qty 0 */ if ($qty === 0) { return $this; } $quoteItem->setMultishippingQty((int) $quoteItem->getMultishippingQty() + $qty); $quoteItem->setQty($quoteItem->getMultishippingQty()); try { $address = $this->addressRepository->getById($addressId); } catch (\Exception $e) { } if (isset($address)) { if (!($quoteAddress = $this->getQuote()->getShippingAddressByCustomerAddressId($address->getId()))) { $quoteAddress = $this->_addressFactory->create()->importCustomerAddressData($address); $this->getQuote()->addShippingAddress($quoteAddress); } $quoteAddress = $this->getQuote()->getShippingAddressByCustomerAddressId($address->getId()); $quoteAddress->setCustomerAddressId($addressId); $quoteAddressItem = $quoteAddress->getItemByQuoteItemId($quoteItemId); if ($quoteAddressItem) { $quoteAddressItem->setQty((int) ($quoteAddressItem->getQty() + $qty)); } else { $quoteAddress->addItem($quoteItem, $qty); } /** * Require shipping rate recollect */ $quoteAddress->setCollectShippingRates((bool) $this->getCollectRatesFlag()); } } return $this; }
/** * Retrieve quote address by type * * @param string $type * @return Address */ protected function _getAddressByType($type) { foreach ($this->getAddressesCollection() as $address) { if ($address->getAddressType() == $type && !$address->isDeleted()) { return $address; } } $address = $this->_quoteAddressFactory->create()->setAddressType($type); $this->addAddress($address); return $address; }
/** * {@inheritdoc} */ public function createEmptyCart() { $storeId = $this->storeManager->getStore()->getStoreId(); $quote = $this->createAnonymousCart($storeId); $quote->setBillingAddress($this->quoteAddressFactory->create()); $quote->setShippingAddress($this->quoteAddressFactory->create()); try { $this->quoteRepository->save($quote); } catch (\Exception $e) { throw new CouldNotSaveException(__('Cannot create quote')); } return $quote->getId(); }