/** * 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; }
/** * {@inheritDoc} * @SuppressWarnings(PHPMD.NPathComplexity) */ public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address) { /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); if ($quote->isVirtual()) { throw new NoSuchEntityException(__('Cart contains virtual product(s) only. Shipping address is not applicable.')); } $saveInAddressBook = $address->getSaveInAddressBook() ? 1 : 0; $sameAsBilling = $address->getSameAsBilling() ? 1 : 0; $customerAddressId = $address->getCustomerAddressId(); $this->addressValidator->validate($address); $quote->setShippingAddress($address); $address = $quote->getShippingAddress(); if ($customerAddressId) { $addressData = $this->addressRepository->getById($customerAddressId); $address = $quote->getShippingAddress()->importCustomerAddressData($addressData); } elseif ($quote->getCustomerId()) { $address->setEmail($quote->getCustomerEmail()); } $address->setSameAsBilling($sameAsBilling); $address->setSaveInAddressBook($saveInAddressBook); $address->setCollectShippingRates(true); try { $this->totalsCollector->collectAddressTotals($quote, $address); $address->save(); } catch (\Exception $e) { $this->logger->critical($e); throw new InputException(__('Unable to save address. Please, check input data.')); } if (!$quote->validateMinimumAmount($quote->getIsMultiShipping())) { throw new InputException($this->scopeConfig->getValue('sales/minimum_order/error_message', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $quote->getStoreId())); } return $quote->getShippingAddress()->getId(); }
/** * 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; }
/** * {@inheritDoc} */ public function savePaymentInformation($cartId, $email, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, \Magento\Quote\Api\Data\AddressInterface $billingAddress) { $billingAddress->setEmail($email); $this->billingAddressManagement->assign($cartId, $billingAddress); $this->paymentMethodManagement->set($cartId, $paymentMethod); return true; }
/** * @param \Magento\Quote\Api\Data\AddressInterface $shippingAddress * @return \Magento\Quote\Api\Data\ShippingInterface */ public function create(\Magento\Quote\Api\Data\AddressInterface $shippingAddress) { /** @var \Magento\Quote\Api\Data\ShippingInterface $shipping */ $shipping = $this->shippingFactory->create(); $shipping->setMethod($shippingAddress->getShippingMethod()); $shipping->setAddress($shippingAddress); return $shipping; }
/** * {@inheritDoc} */ public function getPaymentMethods($cartId, \Magento\Quote\Api\Data\AddressInterface $shippingAddress = null) { // if shippingAddress is provided use this country $country = null; if ($shippingAddress) { $country = $shippingAddress->getCountryId(); } return $this->_paymentMethodsHelper->getPaymentMethods($cartId, $country); }
/** * {@inheritDoc} */ public function getPaymentMethods($cartId, \Magento\Quote\Api\Data\AddressInterface $shippingAddress = null) { $quoteIdMask = $this->_quoteIdMaskFactory->create()->load($cartId, 'masked_id'); $quoteId = $quoteIdMask->getQuoteId(); // if shippingAddress is provided use this country $country = null; if ($shippingAddress) { $country = $shippingAddress->getCountryId(); } return $this->_paymentMethodsHelper->getPaymentMethods($quoteId, $country); }
/** * {@inheritDoc} */ public function savePaymentInformation($cartId, $email, \Magento\Quote\Api\Data\PaymentInterface $paymentMethod, \Magento\Quote\Api\Data\AddressInterface $billingAddress = null) { if ($billingAddress) { $billingAddress->setEmail($email); $this->billingAddressManagement->assign($cartId, $billingAddress); } else { $quoteIdMask = $this->quoteIdMaskFactory->create()->load($cartId, 'masked_id'); $this->cartRepository->getActive($quoteIdMask->getQuoteId())->getBillingAddress()->setEmail($email); } $this->paymentMethodManagement->set($cartId, $paymentMethod); return true; }
/** * Set shipping address * * @param \Magento\Quote\Api\Data\AddressInterface $address * @return $this */ public function setShippingAddress(\Magento\Quote\Api\Data\AddressInterface $address = null) { if ($this->getIsMultiShipping()) { $this->addAddress($address->setAddressType(Address::TYPE_SHIPPING)); } else { $old = $this->getShippingAddress(); if (!empty($old)) { $old->addData($address->getData()); } else { $this->addAddress($address->setAddressType(Address::TYPE_SHIPPING)); } } return $this; }
/** * Convert ValidAddress to QuoteAddressInterface * * @param \AvaTax\ValidAddress $address * @param QuoteAddressInterface $originalAddress * @return QuoteAddressInterface */ public function convertAvaTaxValidAddressToQuoteAddress(\AvaTax\ValidAddress $address, \Magento\Quote\Api\Data\AddressInterface $originalAddress) { $street = []; if ($address->getLine1()) { $street[] = $address->getLine1(); } if ($address->getLine2()) { $street[] = $address->getLine2(); } if ($address->getLine3()) { $street[] = $address->getLine3(); } // Not using line 4, as it returns a concatenation of city, state, and zip (e.g., BAINBRIDGE IS WA 98110-2450) // Get data from original address so that information like name and telephone will be preserved $data = array_merge($originalAddress->getData(), [QuoteAddressInterface::KEY_COUNTRY_ID => $address->getCountry(), QuoteAddressInterface::KEY_REGION_CODE => $address->getRegion(), QuoteAddressInterface::KEY_STREET => $street, QuoteAddressInterface::KEY_POSTCODE => $address->getPostalCode(), QuoteAddressInterface::KEY_CITY => $address->getCity()]); $region = $this->getRegionByCodeAndCountry($address->getRegion(), $address->getCountry()); if (!is_null($region)) { $data[QuoteAddressInterface::KEY_REGION_ID] = $region->getId(); $data[QuoteAddressInterface::KEY_REGION] = $region; } return $this->quoteAddressFactory->create(['data' => $data]); }
public function testGetEmail() { $expected = '*****@*****.**'; $this->quoteAddressMock->expects($this->once())->method('getEmail')->willReturn($expected); $this->assertEquals($expected, $this->model->getEmail()); }
/** * Get billing/shipping email * * @return string */ public function getEmail() { return $this->address->getEmail(); }
/** * Get company * * @return string */ public function getCompany() { return $this->address->getCompany(); }
/** * Returns name suffix * * @return string */ public function getSuffix() { return $this->address->getSuffix(); }
/** * Convert the address into an array of key-value-pairs. * * @param AddressInterface $address * @return string[]|string[][] */ protected function convertAddressToSuggestion(AddressInterface $address) { return array_filter([AddressInterface::KEY_CITY => $address->getCity(), AddressInterface::KEY_COMPANY => $address->getCompany(), AddressInterface::KEY_POSTCODE => $address->getPostcode(), AddressInterface::KEY_STREET => $address->getStreet(), AddressInterface::KEY_COUNTRY_ID => $address->getCountryId(), AddressInterface::KEY_REGION_ID => $address->getRegionId(), AddressInterface::KEY_REGION => $address->getRegion(), AddressInterface::KEY_REGION_CODE => $address->getRegionCode()]); }
/** * @inheritdoc */ public function estimateByExtendedAddress($cartId, AddressInterface $address) { /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); // no methods applicable for empty carts or carts with virtual products if ($quote->isVirtual() || 0 == $quote->getItemsCount()) { return []; } return $this->getShippingMethods($quote, $address->getData()); }