/** * Returns shipping address * * @return AddressAdapterInterface|null */ public function getShippingAddress() { if ($this->quote->getShippingAddress()) { return $this->addressAdapterFactory->create(['address' => $this->quote->getShippingAddress()]); } return null; }
/** * @param CartInterface $quote * @param AddressInterface $address * @param bool $useForShipping * @return void * @throws NoSuchEntityException * @throws InputException */ public function save(CartInterface $quote, AddressInterface $address, $useForShipping = false) { /** @var \Magento\Quote\Model\Quote $quote */ $this->addressValidator->validate($address); $customerAddressId = $address->getCustomerAddressId(); $shippingAddress = null; $addressData = []; if ($useForShipping) { $shippingAddress = $address; } $saveInAddressBook = $address->getSaveInAddressBook() ? 1 : 0; if ($customerAddressId) { try { $addressData = $this->addressRepository->getById($customerAddressId); } catch (NoSuchEntityException $e) { // do nothing if customer is not found by id } $address = $quote->getBillingAddress()->importCustomerAddressData($addressData); if ($useForShipping) { $shippingAddress = $quote->getShippingAddress()->importCustomerAddressData($addressData); $shippingAddress->setSaveInAddressBook($saveInAddressBook); } } elseif ($quote->getCustomerId()) { $address->setEmail($quote->getCustomerEmail()); } $address->setSaveInAddressBook($saveInAddressBook); $quote->setBillingAddress($address); if ($useForShipping) { $shippingAddress->setSameAsBilling(1); $shippingAddress->setCollectShippingRates(true); $quote->setShippingAddress($shippingAddress); } }
/** * @param CartInterface $quote * @return \Magento\Quote\Api\Data\ShippingAssignmentInterface */ public function create(CartInterface $quote) { /** @var \Magento\Quote\Model\Quote $quote */ $shippingAddress = $quote->getShippingAddress(); /** @var \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment */ $shippingAssignment = $this->shippingAssignmentFactory->create(); $shippingAssignment->setItems($quote->getItems()); $shippingAssignment->setShipping($this->shippingProcessor->create($shippingAddress)); return $shippingAssignment; }
/** * Return customer code according to the admin configured format * * @param \Magento\Quote\Api\Data\CartInterface|\Magento\Sales\Api\Data\OrderInterface $data * @return string */ protected function getCustomerCode($data) { switch ($this->config->getCustomerCodeFormat($data->getStoreId())) { case Config::CUSTOMER_FORMAT_OPTION_EMAIL: $email = $data->getCustomerEmail(); return $email ?: Config::CUSTOMER_MISSING_EMAIL; break; case Config::CUSTOMER_FORMAT_OPTION_NAME_ID: $customer = $this->getCustomerById($data->getCustomerId()); if ($customer && $customer->getId()) { $name = $customer->getFirstname() . ' ' . $customer->getLastname(); $id = $customer->getId(); } else { if (!$data->getIsVirtual()) { $address = $data->getShippingAddress(); } else { $address = $data->getBillingAddress(); } $name = $address->getFirstname() . ' ' . $address->getLastname(); if (!trim($name)) { $name = Config::CUSTOMER_MISSING_NAME; } $id = Config::CUSTOMER_GUEST_ID; } return sprintf(Config::CUSTOMER_FORMAT_NAME_ID, $name, $id); break; case Config::CUSTOMER_FORMAT_OPTION_ID: default: return $data->getCustomerId() ?: strtolower(Config::CUSTOMER_GUEST_ID) . '-' . $data->getId(); break; } }