本文整理汇总了PHP中Magento\Customer\Api\CustomerRepositoryInterface::save方法的典型用法代码示例。如果您正苦于以下问题:PHP CustomerRepositoryInterface::save方法的具体用法?PHP CustomerRepositoryInterface::save怎么用?PHP CustomerRepositoryInterface::save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Magento\Customer\Api\CustomerRepositoryInterface
的用法示例。
在下文中一共展示了CustomerRepositoryInterface::save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: execute
protected function execute(\Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output)
{
$def = $this->manTrans->begin();
try {
foreach ($this->DEFAULT_DWNL_TREE as $custId => $parentId) {
$first = 'User' . $custId;
$last = 'Last';
$email = "customer_{$custId}@test.com";
if ($custId != $parentId) {
/* save parent ID to registry */
$referralCode = $this->mapCustomerMageIdByIndex[$parentId];
$this->toolReferral->replaceCodeInRegistry($referralCode);
}
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
$customer = $this->_manObj->create(\Magento\Customer\Api\Data\CustomerInterface::class);
$customer->setEmail($email);
$customer->setFirstname($first);
$customer->setLastname($last);
/* MOBI-427: change group ID for retail customers */
if (in_array($custId, $this->GROUP_RETAIL)) {
$customer->setGroupId(BusinessCodesManager::M_CUST_GROUP_RETAIL);
}
/** @var \Magento\Customer\Api\Data\CustomerInterface $saved */
$saved = $this->repoCustomer->save($customer, $this->DEFAULT_PASSWORD_HASH);
$this->mapCustomerMageIdByIndex[$custId] = $saved->getId();
$this->mapCustomerIndexByMageId[$saved->getId()] = $custId;
}
/* MOBI-426 : rename customer groups according to Generic App scheme. */
$this->subCustomerGroups->renameGroups();
$this->manTrans->commit($def);
} finally {
// transaction will be rolled back if commit is not done (otherwise - do nothing)
$this->manTrans->end($def);
}
}
示例2: executeInternal
/**
* Save newsletter subscription preference action
*
* @return void|null
*/
public function executeInternal()
{
if (!$this->formKeyValidator->validate($this->getRequest())) {
return $this->_redirect('customer/account/');
}
$customerId = $this->_customerSession->getCustomerId();
if ($customerId === null) {
$this->messageManager->addError(__('Something went wrong while saving your subscription.'));
} else {
try {
$customer = $this->customerRepository->getById($customerId);
$storeId = $this->storeManager->getStore()->getId();
$customer->setStoreId($storeId);
$this->customerRepository->save($customer);
if ((bool) $this->getRequest()->getParam('is_subscribed', false)) {
$this->subscriberFactory->create()->subscribeCustomerById($customerId);
$this->messageManager->addSuccess(__('We saved the subscription.'));
} else {
$this->subscriberFactory->create()->unsubscribeCustomerById($customerId);
$this->messageManager->addSuccess(__('We removed the subscription.'));
}
} catch (\Exception $e) {
$this->messageManager->addError(__('Something went wrong while saving your subscription.'));
}
}
$this->_redirect('customer/account/');
}
示例3: execute
/**
* Unlock customer on success login attempt.
* @param \Magento\Framework\Event\Observer $observer
* @return $this
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
/** @var \Magento\Customer\Model\Customer $model */
$customerModel = $observer->getEvent()->getData('model');
$customer = $this->customerRepository->getById($customerModel->getId());
$this->accountManagementHelper->processUnlockData($customer->getId());
$this->customerRepository->save($customer);
return $this;
}
示例4: execute
/**
* Customer locking implementation
*
* @param \Magento\Framework\Event\Observer $observer
* @return $this
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$username = $observer->getEvent()->getData('username');
$customer = $this->customerRepository->get($username);
if ($customer && $customer->getId()) {
$this->accountManagementHelper->processCustomerLockoutData($customer->getId());
$this->customerRepository->save($customer);
}
return $this;
}
示例5: execute
/**
* Upgrade customer password hash when customer has logged in
*
* @param \Magento\Framework\Event\Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$password = $observer->getEvent()->getData('password');
/** @var \Magento\Customer\Model\Customer $model */
$model = $observer->getEvent()->getData('model');
$customer = $this->customerRepository->getById($model->getId());
$customerSecure = $this->customerRegistry->retrieveSecureData($model->getId());
if (!$this->encryptor->validateHashVersion($customerSecure->getPasswordHash(), true)) {
$customerSecure->setPasswordHash($this->encryptor->getHash($password, true));
$this->customerRepository->save($customer);
}
}
示例6: testCustomerUpdatedEmail
/**
* @magentoAppArea adminhtml
* @magentoDataFixture Magento/Newsletter/_files/subscribers.php
*/
public function testCustomerUpdatedEmail()
{
$objectManager = Bootstrap::getObjectManager();
/** @var \Magento\Newsletter\Model\Subscriber $subscriber */
$subscriber = $objectManager->create('Magento\\Newsletter\\Model\\Subscriber');
$subscriber->loadByEmail('customer@example.com');
$this->assertTrue($subscriber->isSubscribed());
$this->assertEquals(1, (int) $subscriber->getCustomerId());
$customer = $this->customerRepository->getById(1);
$customer->setEmail('new@example.com');
$this->customerRepository->save($customer);
$subscriber->loadByEmail('new@example.com');
$this->assertTrue($subscriber->isSubscribed());
$this->assertEquals(1, (int) $subscriber->getCustomerId());
}
示例7: execute
/**
* Check Captcha On Forgot Password Page
*
* @param \Magento\Framework\Event\Observer $observer
* @return $this
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$captchaModel = $this->helper->getCaptcha(self::FORM_ID);
if ($captchaModel->isRequired()) {
/** @var \Magento\Framework\App\Action\Action $controller */
$controller = $observer->getControllerAction();
if (!$captchaModel->isCorrect($this->captchaStringResolver->resolve($controller->getRequest(), self::FORM_ID))) {
try {
$customer = $this->customerRepository->getById($this->customerSession->getCustomerId());
$this->accountManagementHelper->processCustomerLockoutData($customer->getId());
$this->customerRepository->save($customer);
} catch (NoSuchEntityException $e) {
//do nothing as customer existance is validated later in authenticate method
}
$this->workWithLock();
$this->messageManager->addError(__('Incorrect CAPTCHA'));
$this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
$this->redirect->redirect($controller->getResponse(), '*/*/edit');
}
}
$customer = $this->customerSession->getCustomer();
$login = $customer->getEmail();
$captchaModel->logAttempt($login);
return $this;
}
示例8: unlock
/**
* {@inheritdoc}
*/
public function unlock($customerId)
{
$customerSecure = $this->customerRegistry->retrieveSecureData($customerId);
$customerSecure->setFailuresNum(0);
$customerSecure->setFirstFailure(null);
$customerSecure->setLockExpires(null);
$this->customerRepository->save($this->customerRepository->getById($customerId));
}
示例9: populateCustomerInfo
/**
* Populate customer model
*
* @param Quote $quote
* @return void
*/
public function populateCustomerInfo(QuoteEntity $quote)
{
$customer = $quote->getCustomer();
if (!$customer->getId()) {
$customer = $this->accountManagement->createAccountWithPasswordHash($customer, $quote->getPasswordHash());
$quote->setCustomer($customer);
} else {
$this->customerRepository->save($customer);
}
if (!$quote->getBillingAddress()->getId() && $customer->getDefaultBilling()) {
$quote->getBillingAddress()->importCustomerAddressData($this->customerAddressRepository->getById($customer->getDefaultBilling()));
$quote->getBillingAddress()->setCustomerAddressId($customer->getDefaultBilling());
}
if (!$quote->getShippingAddress()->getSameAsBilling() && !$quote->getBillingAddress()->getId() && $customer->getDefaultShipping()) {
$quote->getShippingAddress()->importCustomerAddressData($this->customerAddressRepository->getById($customer->getDefaultShipping()));
$quote->getShippingAddress()->setCustomerAddressId($customer->getDefaultShipping());
}
}
示例10: testIsConfirmedStatusConfirmationIsNotRequired
/**
* @magentoDbIsolation enabled
*/
public function testIsConfirmedStatusConfirmationIsNotRequired()
{
$password = 'password';
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
$customer = $this->_customerFactory->create()->setConfirmation(true)->setFirstname('firstname')->setLastname('lastname')->setEmail('email@email.com');
$customer = $this->_customerRepository->save($customer, $password);
$this->_coreRegistry->register(RegistryConstants::CURRENT_CUSTOMER_ID, $customer->getId());
$this->assertEquals('Confirmation Not Required', $this->_block->getIsConfirmedStatus());
}
示例11: massAction
/**
* Customer mass assign group action
*
* @param AbstractCollection $collection
* @return \Magento\Backend\Model\View\Result\Redirect
*/
protected function massAction(AbstractCollection $collection)
{
$customersUpdated = 0;
foreach ($collection->getAllIds() as $customerId) {
// Verify customer exists
$customer = $this->customerRepository->getById($customerId);
$customer->setGroupId($this->getRequest()->getParam('group'));
$this->customerRepository->save($customer);
$customersUpdated++;
}
if ($customersUpdated) {
$this->messageManager->addSuccess(__('A total of %1 record(s) were updated.', $customersUpdated));
}
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setPath($this->getComponentRefererUrl());
return $resultRedirect;
}
示例12: execute
/**
* Change customer password action
*
* @return \Magento\Framework\Controller\Result\Redirect
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function execute()
{
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
if (!$this->formKeyValidator->validate($this->getRequest())) {
$resultRedirect->setPath('*/*/edit');
return $resultRedirect;
}
if ($this->getRequest()->isPost()) {
$customerId = $this->_getSession()->getCustomerId();
$customer = $this->customerExtractor->extract('customer_account_edit', $this->_request);
$customer->setId($customerId);
if ($customer->getAddresses() == null) {
$customer->setAddresses($this->customerRepository->getById($customerId)->getAddresses());
}
if ($this->getRequest()->getParam('change_password')) {
$currPass = $this->getRequest()->getPost('current_password');
$newPass = $this->getRequest()->getPost('password');
$confPass = $this->getRequest()->getPost('password_confirmation');
if (strlen($newPass)) {
if ($newPass == $confPass) {
try {
$customerEmail = $this->customerRepository->getById($customerId)->getEmail();
$this->customerAccountManagement->changePassword($customerEmail, $currPass, $newPass);
} catch (AuthenticationException $e) {
$this->messageManager->addError($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addException($e, __('Something went wrong while changing the password.'));
}
} else {
$this->messageManager->addError(__('Confirm your new password.'));
}
} else {
$this->messageManager->addError(__('Please enter new password.'));
}
}
try {
$this->customerRepository->save($customer);
} catch (AuthenticationException $e) {
$this->messageManager->addError($e->getMessage());
} catch (InputException $e) {
$this->messageManager->addException($e, __('Invalid input'));
} catch (\Exception $e) {
$this->messageManager->addException($e, __('We can\'t save the customer.') . $e->getMessage() . '<pre>' . $e->getTraceAsString() . '</pre>');
}
if ($this->messageManager->getMessages()->getCount() > 0) {
$this->_getSession()->setCustomerFormData($this->getRequest()->getPostValue());
$resultRedirect->setPath('*/*/edit');
return $resultRedirect;
}
$this->messageManager->addSuccess(__('You saved the account information.'));
$resultRedirect->setPath('customer/account');
return $resultRedirect;
}
$resultRedirect->setPath('*/*/edit');
return $resultRedirect;
}
示例13: execute
/**
* Unlock specified customer
*
* @return \Magento\Backend\Model\View\Result\Redirect
*/
public function execute()
{
$customerId = $this->getRequest()->getParam('customer_id');
try {
// unlock customer
if ($customerId) {
$customer = $this->customerRepository->getById($customerId);
$this->accountManagementHelper->processUnlockData($customerId);
$this->customerRepository->save($customer);
$this->getMessageManager()->addSuccess(__('Customer has been unlocked successfully.'));
}
} catch (\Exception $e) {
$this->messageManager->addError($e->getMessage());
}
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $resultRedirect->setPath('customer/index/edit', ['id' => $customerId]);
}
示例14: testUpdateCustomerDeleteAllAddressesWithEmptyArray
/**
* @magentoAppArea frontend
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Customer/_files/customer_two_addresses.php
*/
public function testUpdateCustomerDeleteAllAddressesWithEmptyArray()
{
$customerId = 1;
$customer = $this->customerRepository->getById($customerId);
$customerDetails = $customer->__toArray();
$newCustomerEntity = $this->customerFactory->create();
$this->dataObjectHelper->populateWithArray($newCustomerEntity, $customerDetails, '\\Magento\\Customer\\Api\\Data\\CustomerInterface');
$newCustomerEntity->setId($customer->getId())->setAddresses([]);
$this->customerRepository->save($newCustomerEntity);
$newCustomerDetails = $this->customerRepository->getById($customerId);
//Verify that old addresses are removed
$this->assertEquals(0, count($newCustomerDetails->getAddresses()));
}
示例15: executeInternal
/**
* Change customer password action
*
* @return \Magento\Framework\Controller\Result\Redirect
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function executeInternal()
{
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
if (!$this->formKeyValidator->validate($this->getRequest())) {
return $resultRedirect->setPath('*/*/edit');
}
if ($this->getRequest()->isPost()) {
$customerId = $this->session->getCustomerId();
$currentCustomer = $this->customerRepository->getById($customerId);
// Prepare new customer data
$customer = $this->customerExtractor->extract('customer_account_edit', $this->_request);
$customer->setId($customerId);
if ($customer->getAddresses() == null) {
$customer->setAddresses($currentCustomer->getAddresses());
}
// Change customer password
if ($this->getRequest()->getParam('change_password')) {
$this->changeCustomerPassword($currentCustomer->getEmail());
}
try {
$this->customerRepository->save($customer);
} catch (AuthenticationException $e) {
$this->messageManager->addError($e->getMessage());
} catch (InputException $e) {
$this->messageManager->addException($e, __('Invalid input'));
} catch (\Exception $e) {
$message = __('We can\'t save the customer.') . $e->getMessage() . '<pre>' . $e->getTraceAsString() . '</pre>';
$this->messageManager->addException($e, $message);
}
if ($this->messageManager->getMessages()->getCount() > 0) {
$this->session->setCustomerFormData($this->getRequest()->getPostValue());
return $resultRedirect->setPath('*/*/edit');
}
$this->messageManager->addSuccess(__('You saved the account information.'));
return $resultRedirect->setPath('customer/account');
}
return $resultRedirect->setPath('*/*/edit');
}