/** * @param \Magento\Quote\Model\Quote $quote * @return \Magento\Quote\Model\Quote */ public function prepareQuoteForNewCustomer(\Magento\Quote\Model\Quote $quote) { $billing = $quote->getBillingAddress(); $shipping = $quote->isVirtual() ? null : $quote->getShippingAddress(); $billing->setDefaultBilling(true); if ($shipping && !$shipping->getSameAsBilling()) { $shipping->setDefaultShipping(true); $address = $shipping->exportCustomerAddress(); $shipping->setCustomerAddressData($address); } elseif ($shipping) { $billing->setDefaultShipping(true); } $address = $shipping->exportCustomerAddress(); $billing->setCustomerAddressData($address); foreach (['customer_dob', 'customer_taxvat', 'customer_gender'] as $attribute) { if ($quote->getData($attribute) && !$billing->getData($attribute)) { $billing->setData($attribute, $quote->getData($attribute)); } } $customer = $this->customerFactory->create(); $this->dataObjectHelper->populateWithArray($customer, $this->copyObject->getDataFromFieldset('checkout_onepage_billing', 'to_customer', $billing), '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $customer->setEmail($quote->getCustomerEmail()); $customer->setPrefix($quote->getCustomerPrefix()); $customer->setFirstname($quote->getCustomerFirstname()); $customer->setMiddlename($quote->getCustomerMiddlename()); $customer->setLastname($quote->getCustomerLastname()); $customer->setSuffix($quote->getCustomerSuffix()); $quote->setCustomer($customer); $quote->addCustomerAddress($billing->exportCustomerAddress()); if ($shipping->hasCustomerAddress()) { $quote->addCustomerAddress($shipping->exportCustomerAddress()); } return $quote; }
/** * @param \Magento\Quote\Model\Quote $quote * @return $this */ protected function _validateCouponCode(\Magento\Quote\Model\Quote $quote) { $code = $quote->getData('coupon_code'); if (strlen($code)) { $addressHasCoupon = false; $addresses = $quote->getAllAddresses(); if (count($addresses) > 0) { foreach ($addresses as $address) { if ($address->hasCouponCode()) { $addressHasCoupon = true; } } if (!$addressHasCoupon) { $quote->setCouponCode(''); } } } return $this; }