/**
  * @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);
     }
 }
Example #2
0
 /**
  * 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;
     }
 }