/** * same_as_billing must be equal 0 if billing address is being saved * * @param bool $unsetId * @dataProvider unsetAddressIdDataProvider */ public function testSameAsBillingForBillingAddress($unsetId) { $this->_quote->setCustomer($this->_customer); $address = $this->_quote->getBillingAddress(); if ($unsetId) { $address->setId(null); } /** @var \Magento\Customer\Service\V1\CustomerAddressServiceInterface $addressService */ $addressService = Bootstrap::getObjectManager()->create('Magento\\Customer\\Service\\V1\\CustomerAddressServiceInterface'); $customerAddressData = $addressService->getDefaultBillingAddress($this->_customer->getId()); $address->setSameAsBilling(0)->setCustomerAddressData($customerAddressData)->save(); $this->assertEquals(0, $this->_quote->getBillingAddress()->getSameAsBilling()); }
/** * Load customer user info * * @throws \Magento\Framework\Exception * @return \Inchoo\SocialConnect\Model\Facebook\Info\User */ public function loadSelf() { if (!$this->_customerSession->isLoggedIn()) { if (!$this->_customer->getId()) { throw new \Magento\Framework\Exception(__('Could not load self since customer isn\'t logged in')); } } $this->_customer = $this->_customerSession->getCustomer(); if (!$this->_customer->getId()) { throw new \Magento\Framework\Exception(__('Could not load by customer id')); } if (!($socialconnectFid = $this->_customer->getInchooSocialconnectFid()) || !($socialconnectFtoken = $this->_customer->getInchooSocialconnectFtoken())) { throw new \Magento\Framework\Exception(__('Could not retrieve token by customer id')); } $this->setAccessToken($socialconnectFtoken); $this->_load(); return $this; }
/** * Change customer password. * * @param CustomerModel $customer * @param string $currentPassword * @param string $newPassword * @return bool true on success * @throws InputException * @throws InvalidEmailOrPasswordException */ private function changePasswordForCustomer($customer, $currentPassword, $newPassword) { $customerSecure = $this->customerRegistry->retrieveSecureData($customer->getId()); $hash = $customerSecure->getPasswordHash(); if (!$this->encryptor->validateHash($currentPassword, $hash)) { throw new InvalidEmailOrPasswordException(__('The password doesn\'t match this account.')); } $customerSecure->setRpToken(null); $customerSecure->setRpTokenCreatedAt(null); $this->checkPasswordStrength($newPassword); $customerSecure->setPasswordHash($this->createPasswordHash($newPassword)); $this->customerRepository->save($customer); // FIXME: Are we using the proper template here? try { $this->sendPasswordResetNotificationEmail($customer); } catch (MailException $e) { $this->logger->critical($e); } return true; }
/** * Save/delete customer address * * @param \Magento\Customer\Model\Customer $customer * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function _saveAddresses(\Magento\Customer\Model\Customer $customer) { $defaultBillingId = $customer->getData('default_billing'); $defaultShippingId = $customer->getData('default_shipping'); /** @var \Magento\Customer\Model\Address $address */ foreach ($customer->getAddresses() as $address) { if ($address->getData('_deleted')) { if ($address->getId() == $defaultBillingId) { $customer->setData('default_billing', null); } if ($address->getId() == $defaultShippingId) { $customer->setData('default_shipping', null); } $removedAddressId = $address->getId(); $address->delete(); // Remove deleted address from customer address collection $customer->getAddressesCollection()->removeItemByKey($removedAddressId); } else { $address->setParentId($customer->getId())->setStoreId($customer->getStoreId())->setIsCustomerSaveTransaction(true)->save(); if (($address->getIsPrimaryBilling() || $address->getIsDefaultBilling()) && $address->getId() != $defaultBillingId) { $customer->setData('default_billing', $address->getId()); } if (($address->getIsPrimaryShipping() || $address->getIsDefaultShipping()) && $address->getId() != $defaultShippingId) { $customer->setData('default_shipping', $address->getId()); } } } $this->saveAttribute($customer, 'default_billing'); $this->saveAttribute($customer, 'default_shipping'); return $this; }
/** * Loading quote data by customer * * @param \Magento\Customer\Model\Customer|int $customer * @return $this */ public function loadByCustomer($customer) { /* @TODO: remove this if after external usages of loadByCustomerId are refactored in MAGETWO-19935 */ if ($customer instanceof \Magento\Customer\Model\Customer || $customer instanceof CustomerInterface) { $customerId = $customer->getId(); } else { $customerId = (int) $customer; } $this->_getResource()->loadByCustomerId($this, $customerId); $this->_afterLoad(); return $this; }
/** * Set customer model and the customer id in session * * @param Customer $customerModel * @return $this * @deprecated use setCustomerId() instead */ public function setCustomer(Customer $customerModel) { $this->_customerModel = $customerModel; $this->_httpContext->setValue(\Magento\Customer\Helper\Data::CONTEXT_GROUP, $customerModel->getGroupId(), \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID); $this->setCustomerId($customerModel->getId()); if (!$customerModel->isConfirmationRequired() && $customerModel->getConfirmation()) { $customerModel->setConfirmation(null)->save(); } /** * The next line is a workaround. * It is used to distinguish users that are logged in from user data set via methods similar to setCustomerId() */ $this->unsIsCustomerEmulated(); return $this; }
/** * Replace existing customer model with a new one. * * @param Customer $customer * @return $this */ public function push(Customer $customer) { $this->customerRegistryById[$customer->getId()] = $customer; $emailKey = $this->getEmailKey($customer->getEmail(), $customer->getWebsiteId()); $this->customerRegistryByEmail[$emailKey] = $customer; return $this; }
/** * Retrieve customer model object * * @return \Magento\Customer\Model\Customer */ public function getCustomer() { /** * @TODO: Remove the method after all external usages are refactored in MAGETWO-19930 * _customer and _customerFactory variables should be eliminated as well */ if (null === $this->_customer) { $this->_customer = $this->_customerFactory->create(); $customerId = $this->getCustomerId(); if ($customerId) { $this->_customer->load($customerId); if (!$this->_customer->getId()) { $this->_customer->setCustomerId(null); } } } return $this->_customer; }
/** * Specify address customer * * @param Customer $customer * @return $this */ public function setCustomer(Customer $customer) { $this->_customer = $customer; $this->setCustomerId($customer->getId()); return $this; }
/** * get customer last quote id * */ public function getCustomerLastQuoteId(\Magento\Customer\Model\Customer $customer) { $storeIds = $this->_storeManager->getWebsite($customer->getWebsiteId())->getStoreIds(); $collection = $this->_quoteCollection->create()->addFieldToFilter('customer_id', $customer->getId())->addFieldToFilter('store_id', array('in' => $storeIds))->setPageSize(1)->setOrder('entity_id'); if ($collection->count()) { return $collection->getFirstItem(); } else { return false; } }
/** * Save/delete customer address * * @param \Magento\Customer\Model\Customer $customer * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function _saveAddresses(\Magento\Customer\Model\Customer $customer) { $defaultBillingId = $customer->getData('default_billing'); $defaultShippingId = $customer->getData('default_shipping'); /** @var \Magento\Customer\Model\Address $address */ foreach ($customer->getAddresses() as $address) { if ($address->getData('_deleted')) { if ($address->getId() == $defaultBillingId) { $customer->setData('default_billing', null); } if ($address->getId() == $defaultShippingId) { $customer->setData('default_shipping', null); } $removedAddressId = $address->getId(); $address->delete(); // Remove deleted address from customer address collection $customer->getAddressesCollection()->removeItemByKey($removedAddressId); } else { $address->setParentId($customer->getId())->setStoreId($customer->getStoreId())->setIsCustomerSaveTransaction(true)->save(); if (($address->getIsPrimaryBilling() || $address->getIsDefaultBilling()) && $address->getId() != $defaultBillingId) { $customer->setData('default_billing', $address->getId()); } if (($address->getIsPrimaryShipping() || $address->getIsDefaultShipping()) && $address->getId() != $defaultShippingId) { $customer->setData('default_shipping', $address->getId()); } } } $changedAddresses = []; $changedAddresses['default_billing'] = $customer->getData('default_billing'); $changedAddresses['default_shipping'] = $customer->getData('default_shipping'); $this->_getWriteAdapter()->update($this->getTable('customer_entity'), $changedAddresses, $this->_getWriteAdapter()->quoteInto('entity_id = ?', $customer->getId())); return $this; }
/** * Get customer last quote id. * * @param \Magento\Customer\Model\Customer $customer * * @return bool|mixed */ public function getCustomerLastQuoteId(\Magento\Customer\Model\Customer $customer) { $storeIds = $this->storeManager->getWebsite($customer->getWebsiteId())->getStoreIds(); $collection = $this->quoteCollection->create()->addFieldToFilter('customer_id', $customer->getId())->addFieldToFilter('store_id', ['in' => $storeIds])->setPageSize(1)->setOrder('entity_id'); if ($collection->getSize()) { //@codingStandardsIgnoreStart return $collection->getFirstItem(); //@codingStandardsIgnoreEnd } else { return false; } }
/** * Create a quote object with customer * * @param array $quoteData * @param \Magento\Customer\Model\Customer $customer * @return \Magento\Sales\Model\Quote */ protected function createQuote($quoteData, $customer) { /** @var \Magento\Customer\Service\V1\CustomerAddressServiceInterface $addressService */ $addressService = $this->objectManager->create('Magento\\Customer\\Service\\V1\\CustomerAddressServiceInterface'); /** @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\Sales\Model\Quote\Address $quoteShippingAddress */ $quoteShippingAddress = $this->objectManager->create('Magento\\Sales\\Model\\Quote\\Address'); $quoteShippingAddress->importCustomerAddressData($addressService->getAddress($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\Sales\Model\Quote\Address $quoteBillingAddress */ $quoteBillingAddress = $this->objectManager->create('Magento\\Sales\\Model\\Quote\\Address'); $quoteBillingAddress->importCustomerAddressData($addressService->getAddress($billingAddress->getId())); /** @var \Magento\Sales\Model\Quote $quote */ $quote = $this->objectManager->create('Magento\\Sales\\Model\\Quote'); $quote->setStoreId(1)->setIsActive(true)->setIsMultiShipping(false)->assignCustomerWithAddressChange($customer, $quoteBillingAddress, $quoteShippingAddress)->setCheckoutMethod($customer->getMode())->setPasswordHash($customer->encryptPassword($customer->getPassword())); return $quote; }
/** * Set customer model and the customer id in session * * @param Customer $customerModel * @return $this * @deprecated use setCustomerId() instead */ public function setCustomer(Customer $customerModel) { $this->_customerModel = $customerModel; $this->_httpContext->setValue(\Magento\Customer\Helper\Data::CONTEXT_GROUP, $customerModel->getGroupId(), \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID); $this->setCustomerId($customerModel->getId()); if (!$customerModel->isConfirmationRequired() && $customerModel->getConfirmation()) { $customerModel->setConfirmation(null)->save(); } return $this; }