/** * @param CustomerInterface $entity * @param array $arguments * @return CustomerInterface * @throws \Exception * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute($entity, $arguments = []) { $extensionAttribute = $entity->getExtensionAttributes()->getExtensionAttribute(); $extensionAttribute->setCustomerId($entity->getId()); $extensionAttribute = $this->entityManager->save($extensionAttribute); $entity->getExtensionAttributes()->setExtensionAttribute($extensionAttribute); return $entity; }
/** * Creates a collection item that represents a customer for the customer Grid. * * @param CustomerInterface $customer Input data for creating the item. * @return \Magento\Framework\Object Collection item that represents a customer */ protected function createCustomerItem(CustomerInterface $customer) { $customerNameParts = [$customer->getPrefix(), $customer->getFirstname(), $customer->getMiddlename(), $customer->getLastname(), $customer->getSuffix()]; $customerItem = new \Magento\Framework\Object(); $customerItem->setId($customer->getId()); $customerItem->setEntityId($customer->getId()); // All parts of the customer name must be displayed in the name column of the grid $customerItem->setName(implode(' ', array_filter($customerNameParts))); $customerItem->setEmail($customer->getEmail()); $customerItem->setWebsiteId($customer->getWebsiteId()); $customerItem->setCreatedAt($customer->getCreatedAt()); $customerItem->setGroupId($customer->getGroupId()); $billingAddress = null; foreach ($customer->getAddresses() as $address) { if ($address->isDefaultBilling()) { $billingAddress = $address; break; } } if ($billingAddress !== null) { $customerItem->setBillingTelephone($billingAddress->getTelephone()); $customerItem->setBillingPostcode($billingAddress->getPostcode()); $customerItem->setBillingCountryId($billingAddress->getCountryId()); $region = $billingAddress->getRegion() === null ? '' : $billingAddress->getRegion()->getRegion(); $customerItem->setBillingRegion($region); } return $customerItem; }
/** * @Given I have a new address with :street :city :region :zip :country */ public function iHaveANewAddressWith($street, $city, $region, $zip, $country) { $countryId = $this->getCountryId($country); $region = $this->loadRegionByName($region, $countryId); $address = $this->addressFactory->create()->setStreet((array) $street)->setCity($city)->setPostcode($zip)->setCountryId($countryId)->setRegion($region)->setRegionId($region->getRegionId())->setFirstname($this->customer->getFirstname())->setLastname($this->customer->getLastname())->setTelephone('555-555-5555')->setCustomerId($this->customer->getId()); $this->customerAddress = $address; }
/** * @param CustomerInterface $entity * @param array $arguments * @return CustomerInterface * @throws \Exception * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute($entity, $arguments = []) { $newAddresses = []; foreach ($entity->getAddresses() as $address) { $address->setCustomerId($entity->getId()); $newAddresses[] = $this->entityManager->save($address); } $entity->setAddresses($newAddresses); return $entity; }
/** * @param CustomerInterface $customer * @return \Openpay\Client\Type\OpenpayCustomerType * @throws LocalizedException */ public function save(CustomerInterface $customer) { $params = ['name' => $customer->getFirstname(), 'last_name' => $customer->getLastname(), 'email' => $customer->getEmail(), 'requires_account' => self::CUSTOMER_REQUIRES_ACCOUNT, 'external_id' => $customer->getId()]; try { $openpayCustomer = $this->customerAdapter->store($params); } catch (OpenpayException $e) { throw new LocalizedException(__($e->getDescription()), $e); } return $openpayCustomer; }
/** * @param CustomerInterface $entity * @param array $arguments * @return CustomerInterface * @throws \Exception * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute($entity, $arguments = []) { $metadata = $this->metadataPool->getMetadata(ExtensionAttributeInterface::class); $connection = $this->resourceConnection->getConnectionByName($metadata->getEntityConnectionName()); $id = $connection->fetchOne($connection->select()->from($metadata->getEntityTable(), [$metadata->getIdentifierField()])->where('customer_id = ?', $entity->getId())->limit(1)); $extensionAttribute = $this->extensionAttributeFactory->create(); $extensionAttribute = $this->entityManager->load($extensionAttribute, $id); $customerExtension = $this->customerExtensionFactory->create(['data' => ['extension_attribute' => $extensionAttribute]]); $entity->setExtensionAttributes($customerExtension); return $entity; }
/** * Create or update user by using data from facebook and login to store * * @param GraphUser $facebookUser * @param AccessToken $accessToken * * @return CustomerInterface */ private function createOrUpdateAndLogin(GraphUser $facebookUser, AccessToken $accessToken) { if (!$this->customer->getId()) { $this->customer->setEmail($facebookUser->getEmail()); $this->customer->setFirstname($facebookUser->getFirstName()); $this->customer->setLastname($facebookUser->getLastName()); $this->customer->setMiddlename($facebookUser->getMiddleName()); $this->customer->setGender((int) ($facebookUser->getGender() == 'male')); } $this->customer->setCustomAttribute('sf_id', $facebookUser->getId()); $this->customer->setCustomAttribute('sf_access_token', serialize($accessToken)); if ($this->customer->getId()) { $customer = $this->customerRepository->save($this->customer); } else { $customer = $this->accountManagement->createAccount($this->customer); } $this->login($customer->getId()); return $customer; }
/** * Create an object with data merged from Customer and CustomerSecure * * @param CustomerInterface $customer * @return Data\CustomerSecure */ protected function getFullCustomerObject($customer) { // No need to flatten the custom attributes or nested objects since the only usage is for email templates and // object passed for events $mergedCustomerData = $this->customerRegistry->retrieveSecureData($customer->getId()); $customerData = $this->dataProcessor->buildOutputDataArray($customer, '\\Magento\\Customer\\Api\\Data\\CustomerInterface'); $mergedCustomerData->addData($customerData); $mergedCustomerData->setData('name', $this->customerViewHelper->getCustomerName($customer)); return $mergedCustomerData; }
/** * Create customer address and save it in the quote so that it can be used to persist later. * * @param \Magento\Customer\Api\Data\CustomerInterface $customer * @param \Magento\Quote\Model\Quote\Address $quoteCustomerAddress * @return void * @throws \InvalidArgumentException * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function _prepareCustomerAddress($customer, $quoteCustomerAddress) { // Possible that customerId is null for new customers $quoteCustomerAddress->setCustomerId($customer->getId()); $customerAddress = $quoteCustomerAddress->exportCustomerAddress(); $quoteAddressId = $quoteCustomerAddress->getCustomerAddressId(); $addressType = $quoteCustomerAddress->getAddressType(); if ($quoteAddressId) { /** Update existing address */ $existingAddressDataObject = $this->addressRepository->getById($quoteAddressId); /** Update customer address data */ $this->dataObjectHelper->mergeDataObjects(get_class($existingAddressDataObject), $existingAddressDataObject, $customerAddress); $customerAddress = $existingAddressDataObject; } elseif ($addressType == \Magento\Quote\Model\Quote\Address::ADDRESS_TYPE_SHIPPING) { try { $billingAddressDataObject = $this->accountManagement->getDefaultBillingAddress($customer->getId()); } catch (\Exception $e) { /** Billing address does not exist. */ } $isShippingAsBilling = $quoteCustomerAddress->getSameAsBilling(); if (isset($billingAddressDataObject) && $isShippingAsBilling) { /** Set existing billing address as default shipping */ $customerAddress = $billingAddressDataObject; $customerAddress->setIsDefaultShipping(true); } } switch ($addressType) { case \Magento\Quote\Model\Quote\Address::ADDRESS_TYPE_BILLING: if (is_null($customer->getDefaultBilling())) { $customerAddress->setIsDefaultBilling(true); } break; case \Magento\Quote\Model\Quote\Address::ADDRESS_TYPE_SHIPPING: if (is_null($customer->getDefaultShipping())) { $customerAddress->setIsDefaultShipping(true); } break; default: throw new \InvalidArgumentException('Customer address type is invalid.'); } $this->getQuote()->setCustomer($customer); $this->getQuote()->addCustomerAddress($customerAddress); }
/** * Define customer object * * @param \Magento\Customer\Api\Data\CustomerInterface $customer * @return $this */ public function setCustomer(\Magento\Customer\Api\Data\CustomerInterface $customer = null) { /* @TODO: Remove the method after all external usages are refactored in MAGETWO-19930 */ $this->_customer = $customer; $this->setCustomerId($customer->getId()); $origAddresses = $customer->getAddresses(); $customer->setAddresses([]); $customerDataFlatArray = $this->objectFactory->create($this->extensibleDataObjectConverter->toFlatArray($customer, [], '\\Magento\\Customer\\Api\\Data\\CustomerInterface')); $customer->setAddresses($origAddresses); $this->_objectCopyService->copyFieldsetToTarget('customer_account', 'to_quote', $customerDataFlatArray, $this); return $this; }
/** * {@inheritdoc} */ public function delete(\Magento\Customer\Api\Data\CustomerInterface $customer) { return $this->deleteById($customer->getId()); }
/** * Load subscriber by customer * * @param \Magento\Customer\Api\Data\CustomerInterface $customer * @return array */ public function loadByCustomerData(\Magento\Customer\Api\Data\CustomerInterface $customer) { $select = $this->connection->select()->from($this->getMainTable())->where('customer_id=:customer_id'); $result = $this->connection->fetchRow($select, ['customer_id' => $customer->getId()]); if ($result) { return $result; } $select = $this->connection->select()->from($this->getMainTable())->where('subscriber_email=:subscriber_email'); $result = $this->connection->fetchRow($select, ['subscriber_email' => $customer->getEmail()]); if ($result) { return $result; } return []; }
/** * Create a quote object with customer * * @param array $quoteData * @param \Magento\Customer\Api\Data\CustomerInterface $customer * @return \Magento\Quote\Model\Quote */ protected function createQuote($quoteData, $customer) { /** @var \Magento\Customer\Api\AddressRepositoryInterface $addressService */ $addressService = $this->objectManager->create('Magento\\Customer\\Api\\AddressRepositoryInterface'); /** @var array $shippingAddressOverride */ $shippingAddressOverride = empty($quoteData['shipping_address']) ? [] : $quoteData['shipping_address']; /** @var \Magento\Customer\Model\Address $shippingAddress */ $shippingAddress = $this->createCustomerAddress($shippingAddressOverride, $customer->getId()); /** @var \Magento\Quote\Model\Quote\Address $quoteShippingAddress */ $quoteShippingAddress = $this->objectManager->create('Magento\\Quote\\Model\\Quote\\Address'); $quoteShippingAddress->importCustomerAddressData($addressService->getById($shippingAddress->getId())); /** @var array $billingAddressOverride */ $billingAddressOverride = empty($quoteData['billing_address']) ? [] : $quoteData['billing_address']; /** @var \Magento\Customer\Model\Address $billingAddress */ $billingAddress = $this->createCustomerAddress($billingAddressOverride, $customer->getId()); /** @var \Magento\Quote\Model\Quote\Address $quoteBillingAddress */ $quoteBillingAddress = $this->objectManager->create('Magento\\Quote\\Model\\Quote\\Address'); $quoteBillingAddress->importCustomerAddressData($addressService->getById($billingAddress->getId())); /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->objectManager->create('Magento\\Quote\\Model\\Quote'); $quote->setStoreId(1)->setIsActive(true)->setIsMultiShipping(false)->assignCustomerWithAddressChange($customer, $quoteBillingAddress, $quoteShippingAddress)->setCheckoutMethod('register')->setPasswordHash($this->accountManagement->getPasswordHash(static::CUSTOMER_PASSWORD)); return $quote; }
/** * Get checkout quote instance by current session * * @return Quote * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function getQuote() { $this->_eventManager->dispatch('custom_quote_process', ['checkout_session' => $this]); if ($this->_quote === null) { $quote = $this->quoteFactory->create(); if ($this->getQuoteId()) { try { if ($this->_loadInactive) { $quote = $this->quoteRepository->get($this->getQuoteId()); } else { $quote = $this->quoteRepository->getActive($this->getQuoteId()); } /** * 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()); $this->quoteRepository->save($quote->collectTotals()); /* * We mast to create new quote object, because collectTotals() * can to create links with other objects. */ $quote = $this->quoteRepository->get($this->getQuoteId()); } } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { $this->setQuoteId(null); } } if (!$this->getQuoteId()) { if ($this->_customerSession->isLoggedIn() || $this->_customer) { $customerId = $this->_customer ? $this->_customer->getId() : $this->_customerSession->getCustomerId(); try { $quote = $this->quoteRepository->getActiveForCustomer($customerId); $this->setQuoteId($quote->getId()); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { } } else { $quote->setIsCheckoutCart(true); $this->_eventManager->dispatch('checkout_quote_init', ['quote' => $quote]); } } if ($this->_customer) { $quote->setCustomer($this->_customer); } elseif ($this->_customerSession->isLoggedIn()) { $quote->setCustomer($this->customerRepository->getById($this->_customerSession->getCustomerId())); } $quote->setStore($this->_storeManager->getStore()); $this->_quote = $quote; } if (!$this->isQuoteMasked() && !$this->_customerSession->isLoggedIn() && $this->getQuoteId()) { $quoteId = $this->getQuoteId(); /** @var $quoteIdMask \Magento\Quote\Model\QuoteIdMask */ $quoteIdMask = $this->quoteIdMaskFactory->create()->load($quoteId, 'quote_id'); if ($quoteIdMask->getMaskedId() === null) { $quoteIdMask->setQuoteId($quoteId)->save(); } $this->setIsQuoteMasked(true); } $remoteAddress = $this->_remoteAddress->getRemoteAddress(); if ($remoteAddress) { $this->_quote->setRemoteIp($remoteAddress); $xForwardIp = $this->request->getServer('HTTP_X_FORWARDED_FOR'); $this->_quote->setXForwardedFor($xForwardIp); } return $this->_quote; }
/** * Process change email request * * @param \Magento\Customer\Api\Data\CustomerInterface $currentCustomerDataObject * @return void * @throws InvalidEmailOrPasswordException * @throws UserLockedException */ private function processChangeEmailRequest(\Magento\Customer\Api\Data\CustomerInterface $currentCustomerDataObject) { if ($this->getRequest()->getParam('change_email')) { // authenticate user for changing email try { $this->getAuthentication()->authenticate($currentCustomerDataObject->getId(), $this->getRequest()->getPost('current_password')); } catch (InvalidEmailOrPasswordException $e) { throw new InvalidEmailOrPasswordException(__('The password doesn\'t match this account.')); } } }
/** * Set Customer data object with the customer information * * @param CustomerData $customerData * @return $this */ public function setCustomerDataObject(CustomerData $customerData) { $this->setId($customerData->getId()); $this->getCustomer()->updateData($customerData); return $this; }
/** * Create Data Transfer Object of customer candidate * * @param \Magento\Framework\App\RequestInterface $inputData * @param \Magento\Customer\Api\Data\CustomerInterface $currentCustomerData * @return \Magento\Customer\Api\Data\CustomerInterface */ protected function populateNewCustomerDataObject(\Magento\Framework\App\RequestInterface $inputData, \Magento\Customer\Api\Data\CustomerInterface $currentCustomerData) { $customerDto = $this->customerExtractor->extract(self::FORM_DATA_EXTRACTOR_CODE, $inputData); $customerDto->setId($currentCustomerData->getId()); if (!$customerDto->getAddresses()) { $customerDto->setAddresses($currentCustomerData->getAddresses()); } if (!$inputData->getParam('change_email')) { $customerDto->setEmail($currentCustomerData->getEmail()); } return $customerDto; }
/** * Plugin after create customer that updates any newsletter subscription that may have existed. * * @param CustomerRepository $subject * @param CustomerInterface $customer * @return CustomerInterface * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterSave(CustomerRepository $subject, CustomerInterface $customer) { $this->subscriberFactory->create()->updateSubscription($customer->getId()); return $customer; }
/** * Check if customer is locked and throw exception. * * @api * @param \Magento\Customer\Api\Data\CustomerInterface $customer * @throws \Magento\Framework\Exception\State\UserLockedException * @return void */ public function checkIfLocked(\Magento\Customer\Api\Data\CustomerInterface $customer) { $currentCustomer = $this->customerRegistry->retrieve($customer->getId()); if ($currentCustomer->isCustomerLocked()) { throw new UserLockedException(__('The account is locked. Please wait and try again or contact %1.', $this->scopeConfig->getValue('contact/email/recipient_email'))); } }
/** * @param CustomerInterface $entity * @return CustomerInterface * @throws \Exception * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute($entity, $arguments = []) { $searchCriteria = $this->searchCriteriaBuilder->addFilter('parent_id', $entity->getId())->create(); $addressesResult = $this->addressRepository->getList($searchCriteria); $entity->setAddresses($addressesResult->getItems()); return $entity; }