/** * Set Customer data object with the customer information * * @param CustomerData $customerData * @return $this */ public function setCustomerDataObject(CustomerData $customerData) { $this->setId($customerData->getId()); $this->_converter->updateCustomerModel($this->getCustomer(), $customerData); return $this; }
/** * Create customerAddressDataObject and save it in the Model\Quote so that it can be used to persist later. * * @param CustomerDataObject $customerDataObject * @param \Magento\Sales\Model\Quote\Address $quoteCustomerAddress * @return void * @throws \InvalidArgumentException */ protected function _prepareCustomerAddress($customerDataObject, $quoteCustomerAddress) { // Possible that customerId is null for new customers $customerId = $customerDataObject->getId(); $quoteCustomerAddress->setCustomerId($customerId); $customerAddressDataObject = $quoteCustomerAddress->exportCustomerAddressData(); $quoteAddressId = $quoteCustomerAddress->getCustomerAddressId(); $addressType = $quoteCustomerAddress->getAddressType(); if ($quoteAddressId) { /** Update existing address */ $existingAddressDataObject = $this->_customerAddressService->getAddress($quoteAddressId); /** Update customer address data */ $customerAddressDataObject = $this->_customerAddressBuilder->mergeDataObjects($existingAddressDataObject, $customerAddressDataObject); } elseif ($addressType == CustomerAddressDataObject::ADDRESS_TYPE_SHIPPING) { try { $billingAddressDataObject = $this->_customerAddressService->getDefaultBillingAddress($customerId); } catch (\Exception $e) { /** Billing address does not exist. */ } $isShippingAsBilling = $quoteCustomerAddress->getSameAsBilling(); if (isset($billingAddressDataObject) && $isShippingAsBilling) { /** Set existing billing address as default shipping */ $customerAddressDataObject = $this->_customerAddressBuilder->populate($billingAddressDataObject)->setDefaultShipping(true)->create(); } } switch ($addressType) { case CustomerAddressDataObject::ADDRESS_TYPE_BILLING: if (is_null($customerDataObject->getDefaultBilling())) { $customerAddressDataObject = $this->_customerAddressBuilder->populate($customerAddressDataObject)->setDefaultBilling(true)->create(); } break; case CustomerAddressDataObject::ADDRESS_TYPE_SHIPPING: if (is_null($customerDataObject->getDefaultShipping())) { $customerAddressDataObject = $this->_customerAddressBuilder->populate($customerAddressDataObject)->setDefaultShipping(true)->create(); } break; default: throw new \InvalidArgumentException('Customer address type is invalid.'); } $this->getQuote()->addCustomerAddressData($customerAddressDataObject); }
/** * Get checkout quote instance by current session * * @return Quote */ public function getQuote() { $this->_eventManager->dispatch('custom_quote_process', array('checkout_session' => $this)); if ($this->_quote === null) { /** @var $quote Quote */ $quote = $this->_quoteFactory->create()->setStoreId($this->_storeManager->getStore()->getId()); if ($this->getQuoteId()) { if ($this->_loadInactive) { $quote->load($this->getQuoteId()); } else { $quote->loadActive($this->getQuoteId()); } if ($quote->getId()) { /** * If current currency code of quote is not equal current currency code of store, * need recalculate totals of quote. It is possible if customer use currency switcher or * store switcher. */ if ($quote->getQuoteCurrencyCode() != $this->_storeManager->getStore()->getCurrentCurrencyCode()) { $quote->setStore($this->_storeManager->getStore()); $quote->collectTotals()->save(); /* * We mast to create new quote object, because collectTotals() * can to create links with other objects. */ $quote = $this->_quoteFactory->create()->setStoreId($this->_storeManager->getStore()->getId()); $quote->load($this->getQuoteId()); } } else { $this->setQuoteId(null); } } if (!$this->getQuoteId()) { if ($this->_customerSession->isLoggedIn() || $this->_customer) { $customerId = $this->_customer ? $this->_customer->getId() : $this->_customerSession->getCustomerId(); $quote->loadByCustomer($customerId); $this->setQuoteId($quote->getId()); } else { $quote->setIsCheckoutCart(true); $this->_eventManager->dispatch('checkout_quote_init', array('quote' => $quote)); } } if ($this->getQuoteId()) { if ($this->_customer) { $quote->setCustomerData($this->_customer); } else { if ($this->_customerSession->isLoggedIn()) { $quote->setCustomerData($this->_customerSession->getCustomerDataObject()); } } } $quote->setStore($this->_storeManager->getStore()); $this->_quote = $quote; } if ($remoteAddr = $this->_remoteAddress->getRemoteAddress()) { $this->_quote->setRemoteIp($remoteAddr); $xForwardIp = $this->request->getServer('HTTP_X_FORWARDED_FOR'); $this->_quote->setXForwardedFor($xForwardIp); } return $this->_quote; }
/** * Plugin after create customer that updates any newsletter subscription that may have existed. * * @param CustomerAccountServiceInterface $subject * @param Customer $customer * @return Customer * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterCreateCustomer(CustomerAccountServiceInterface $subject, Customer $customer) { $this->subscriberFactory->create()->updateSubscription($customer->getId()); return $customer; }
/** * Load subscriber by customer * * @param \Magento\Customer\Service\V1\Data\Customer $customer * @return array */ public function loadByCustomerData(\Magento\Customer\Service\V1\Data\Customer $customer) { $select = $this->_read->select()->from($this->getMainTable())->where('customer_id=:customer_id'); $result = $this->_read->fetchRow($select, array('customer_id' => $customer->getId())); if ($result) { return $result; } $select = $this->_read->select()->from($this->getMainTable())->where('subscriber_email=:subscriber_email'); $result = $this->_read->fetchRow($select, array('subscriber_email' => $customer->getEmail())); if ($result) { return $result; } return array(); }