Magento 2 : Programmatically Create Customer and Then Logincreate customer & login that customer...
Why is the underscore command _ useful?
How can I practically buy stocks?
What is the term for a person whose job is to place products on shelves in stores?
Combinatorics problem, right solution?
Help with my training data
How do I produce this symbol: Ϟ in pdfLaTeX?
Older movie/show about humans on derelict alien warship which refuels by passing through a star
Bayes factor vs P value
How do I deal with a coworker that keeps asking to make small superficial changes to a report, and it is seriously triggering my anxiety?
Was Dennis Ritchie being too modest in this quote about C and Pascal?
How exactly does Hawking radiation decrease the mass of black holes?
Multiple fireplaces in an apartment building?
Could moose/elk survive in the Amazon forest?
Philosophical question on logistic regression: why isn't the optimal threshold value trained?
How can I wire a 9-position switch so that each position turns on one more LED than the one before?
Double-nominative constructions and “von”
How important is it that $TERM is correct?
Retract an already submitted recommendation letter (written for an undergrad student)
What's the difference between using dependency injection with a container and using a service locator?
How bug prioritization works in agile projects vs non agile
Suing a Police Officer Instead of the Police Department
A strange hotel
Drawing a german abacus as in the books of Adam Ries
Co-worker works way more than he should
Magento 2 : Programmatically Create Customer and Then Login
create customer & login that customer Programmatically in custom controllerGetting strange result querying carts when using multiple search filter groupsArea code not setThe requested Payment Method is not available When creating an orderMagento2 add custom address attributeMagento 2: How to override newsletter Subscriber modelI can't grab region or region_id in Admin Controller - Magento 1.7Magento 2 Add new field to Magento_User admin formSQL query: getting customer attributes (M1) - This type of clause was previously parsedMagento 2 Error
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
I am trying to create customer and then login programmatically. Below is my code :
<?php
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
$websiteId = $storeManager->getStore()->getWebsiteId();
$firstName = 'Test';
$lastName = 'Test';
$email = 'test@example.com';
$password = 'Test@123';
$address = array(
'customer_address_id' => '',
'prefix' => '',
'firstname' => $firstName,
'middlename' => '',
'lastname' => $lastName,
'suffix' => '',
'company' => '',
'street' => array(
'0' => 'Customer Address 1', // this is mandatory
'1' => 'Customer Address 2' // this is optional
),
'city' => 'New York',
'country_id' => 'US', // two letters country code
'region' => 'New York', // can be empty '' if no region
'region_id' => '43', // can be empty '' if no region_id
'postcode' => '10450',
'telephone' => '123-456-7890',
'fax' => '',
'save_in_address_book' => 1
);
$customerFactory = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();
/**
* check whether the email address is already registered or not
*/
$customer = $customerFactory->setWebsiteId($websiteId)->loadByEmail($email);
/**
* if email address already registered, return the error message
* else, create new customer account
*/
if ($customer->getId()) {
echo 'Customer with email '.$email.' is already registered.';
} else {
try {
$customer = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();
$customer->setWebsiteId($websiteId);
$customer->setEmail($email);
$customer->setFirstname($firstName);
$customer->setLastname($lastName);
$customer->setPassword($password);
// save customer
$customer->save();
$customer->setConfirmation(null);
$customer->save();
$customAddress = $objectManager->get('MagentoCustomerModelAddressFactory')->create();
$customAddress->setData($address)
->setCustomerId($customer->getId())
->setIsDefaultBilling('1')
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
// save customer address
$customAddress->save();
// Create customer session
$customerSession = $objectManager->create('MagentoCustomerModelSession');
$customerSession->setCustomerAsLoggedIn($customer);
echo 'Customer with email '.$email.' is successfully created.';
} catch (Exception $e) {
echo $e->getMessage();
}
}
Above code create customer successfully as I can find them in database but they are not visible in admin customer grid. Also, after creating customer I am trying to login programmatically but no success. Please help.
magento2 customer login
bumped to the homepage by Community♦ 2 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
I am trying to create customer and then login programmatically. Below is my code :
<?php
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
$websiteId = $storeManager->getStore()->getWebsiteId();
$firstName = 'Test';
$lastName = 'Test';
$email = 'test@example.com';
$password = 'Test@123';
$address = array(
'customer_address_id' => '',
'prefix' => '',
'firstname' => $firstName,
'middlename' => '',
'lastname' => $lastName,
'suffix' => '',
'company' => '',
'street' => array(
'0' => 'Customer Address 1', // this is mandatory
'1' => 'Customer Address 2' // this is optional
),
'city' => 'New York',
'country_id' => 'US', // two letters country code
'region' => 'New York', // can be empty '' if no region
'region_id' => '43', // can be empty '' if no region_id
'postcode' => '10450',
'telephone' => '123-456-7890',
'fax' => '',
'save_in_address_book' => 1
);
$customerFactory = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();
/**
* check whether the email address is already registered or not
*/
$customer = $customerFactory->setWebsiteId($websiteId)->loadByEmail($email);
/**
* if email address already registered, return the error message
* else, create new customer account
*/
if ($customer->getId()) {
echo 'Customer with email '.$email.' is already registered.';
} else {
try {
$customer = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();
$customer->setWebsiteId($websiteId);
$customer->setEmail($email);
$customer->setFirstname($firstName);
$customer->setLastname($lastName);
$customer->setPassword($password);
// save customer
$customer->save();
$customer->setConfirmation(null);
$customer->save();
$customAddress = $objectManager->get('MagentoCustomerModelAddressFactory')->create();
$customAddress->setData($address)
->setCustomerId($customer->getId())
->setIsDefaultBilling('1')
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
// save customer address
$customAddress->save();
// Create customer session
$customerSession = $objectManager->create('MagentoCustomerModelSession');
$customerSession->setCustomerAsLoggedIn($customer);
echo 'Customer with email '.$email.' is successfully created.';
} catch (Exception $e) {
echo $e->getMessage();
}
}
Above code create customer successfully as I can find them in database but they are not visible in admin customer grid. Also, after creating customer I am trying to login programmatically but no success. Please help.
magento2 customer login
bumped to the homepage by Community♦ 2 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
Any error in log file when above script executed?
– Dhiren Vasoya
Jul 31 '18 at 11:01
No error in log files. I can log in from login page but it is not visible in admin customer's grid.
– Amatya Trivedi
Jul 31 '18 at 11:45
Try to run indexing one, then check.
– Dhiren Vasoya
Jul 31 '18 at 11:51
add a comment |
I am trying to create customer and then login programmatically. Below is my code :
<?php
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
$websiteId = $storeManager->getStore()->getWebsiteId();
$firstName = 'Test';
$lastName = 'Test';
$email = 'test@example.com';
$password = 'Test@123';
$address = array(
'customer_address_id' => '',
'prefix' => '',
'firstname' => $firstName,
'middlename' => '',
'lastname' => $lastName,
'suffix' => '',
'company' => '',
'street' => array(
'0' => 'Customer Address 1', // this is mandatory
'1' => 'Customer Address 2' // this is optional
),
'city' => 'New York',
'country_id' => 'US', // two letters country code
'region' => 'New York', // can be empty '' if no region
'region_id' => '43', // can be empty '' if no region_id
'postcode' => '10450',
'telephone' => '123-456-7890',
'fax' => '',
'save_in_address_book' => 1
);
$customerFactory = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();
/**
* check whether the email address is already registered or not
*/
$customer = $customerFactory->setWebsiteId($websiteId)->loadByEmail($email);
/**
* if email address already registered, return the error message
* else, create new customer account
*/
if ($customer->getId()) {
echo 'Customer with email '.$email.' is already registered.';
} else {
try {
$customer = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();
$customer->setWebsiteId($websiteId);
$customer->setEmail($email);
$customer->setFirstname($firstName);
$customer->setLastname($lastName);
$customer->setPassword($password);
// save customer
$customer->save();
$customer->setConfirmation(null);
$customer->save();
$customAddress = $objectManager->get('MagentoCustomerModelAddressFactory')->create();
$customAddress->setData($address)
->setCustomerId($customer->getId())
->setIsDefaultBilling('1')
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
// save customer address
$customAddress->save();
// Create customer session
$customerSession = $objectManager->create('MagentoCustomerModelSession');
$customerSession->setCustomerAsLoggedIn($customer);
echo 'Customer with email '.$email.' is successfully created.';
} catch (Exception $e) {
echo $e->getMessage();
}
}
Above code create customer successfully as I can find them in database but they are not visible in admin customer grid. Also, after creating customer I am trying to login programmatically but no success. Please help.
magento2 customer login
I am trying to create customer and then login programmatically. Below is my code :
<?php
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
$websiteId = $storeManager->getStore()->getWebsiteId();
$firstName = 'Test';
$lastName = 'Test';
$email = 'test@example.com';
$password = 'Test@123';
$address = array(
'customer_address_id' => '',
'prefix' => '',
'firstname' => $firstName,
'middlename' => '',
'lastname' => $lastName,
'suffix' => '',
'company' => '',
'street' => array(
'0' => 'Customer Address 1', // this is mandatory
'1' => 'Customer Address 2' // this is optional
),
'city' => 'New York',
'country_id' => 'US', // two letters country code
'region' => 'New York', // can be empty '' if no region
'region_id' => '43', // can be empty '' if no region_id
'postcode' => '10450',
'telephone' => '123-456-7890',
'fax' => '',
'save_in_address_book' => 1
);
$customerFactory = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();
/**
* check whether the email address is already registered or not
*/
$customer = $customerFactory->setWebsiteId($websiteId)->loadByEmail($email);
/**
* if email address already registered, return the error message
* else, create new customer account
*/
if ($customer->getId()) {
echo 'Customer with email '.$email.' is already registered.';
} else {
try {
$customer = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();
$customer->setWebsiteId($websiteId);
$customer->setEmail($email);
$customer->setFirstname($firstName);
$customer->setLastname($lastName);
$customer->setPassword($password);
// save customer
$customer->save();
$customer->setConfirmation(null);
$customer->save();
$customAddress = $objectManager->get('MagentoCustomerModelAddressFactory')->create();
$customAddress->setData($address)
->setCustomerId($customer->getId())
->setIsDefaultBilling('1')
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
// save customer address
$customAddress->save();
// Create customer session
$customerSession = $objectManager->create('MagentoCustomerModelSession');
$customerSession->setCustomerAsLoggedIn($customer);
echo 'Customer with email '.$email.' is successfully created.';
} catch (Exception $e) {
echo $e->getMessage();
}
}
Above code create customer successfully as I can find them in database but they are not visible in admin customer grid. Also, after creating customer I am trying to login programmatically but no success. Please help.
magento2 customer login
magento2 customer login
edited Jul 31 '18 at 11:00
Dhiren Vasoya
4,52751844
4,52751844
asked Jul 31 '18 at 9:34
Amatya TrivediAmatya Trivedi
539
539
bumped to the homepage by Community♦ 2 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 2 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
Any error in log file when above script executed?
– Dhiren Vasoya
Jul 31 '18 at 11:01
No error in log files. I can log in from login page but it is not visible in admin customer's grid.
– Amatya Trivedi
Jul 31 '18 at 11:45
Try to run indexing one, then check.
– Dhiren Vasoya
Jul 31 '18 at 11:51
add a comment |
Any error in log file when above script executed?
– Dhiren Vasoya
Jul 31 '18 at 11:01
No error in log files. I can log in from login page but it is not visible in admin customer's grid.
– Amatya Trivedi
Jul 31 '18 at 11:45
Try to run indexing one, then check.
– Dhiren Vasoya
Jul 31 '18 at 11:51
Any error in log file when above script executed?
– Dhiren Vasoya
Jul 31 '18 at 11:01
Any error in log file when above script executed?
– Dhiren Vasoya
Jul 31 '18 at 11:01
No error in log files. I can log in from login page but it is not visible in admin customer's grid.
– Amatya Trivedi
Jul 31 '18 at 11:45
No error in log files. I can log in from login page but it is not visible in admin customer's grid.
– Amatya Trivedi
Jul 31 '18 at 11:45
Try to run indexing one, then check.
– Dhiren Vasoya
Jul 31 '18 at 11:51
Try to run indexing one, then check.
– Dhiren Vasoya
Jul 31 '18 at 11:51
add a comment |
2 Answers
2
active
oldest
votes
The customer probablu isn't visible in the admin customer grid because that get's cached. I also had that problem.
You can log in the customer like this:
public function loginNewCustomer($customer){
if (!$this->customerSession->isLoggedIn()) {
try {
$this->customerSession->setCustomerAsLoggedIn($customer);
} catch (Exception $e) {
// Customer can't log in, this might be because the account needs to be confirmed.
}
}
}
If you don't have the customer object yet at that location you can get $customer like this:
$customer = $this->_customer->loadByEmail("test@m2s.com");
$this->customerSession->setCustomerAsLoggedIn($customer);
add a comment |
Here is the solution :
<?php
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
$websiteId = $storeManager->getStore()->getWebsiteId();
$firstName = 'Test';
$lastName = 'Test';
$email = 'aabb@example.com';
$password = 'Test@123';
$address = array(
'customer_address_id' => '',
'prefix' => '',
'firstname' => $firstName,
'middlename' => '',
'lastname' => $lastName,
'suffix' => '',
'company' => '',
'street' => array(
'0' => 'Customer Address 1', // this is mandatory
'1' => 'Customer Address 2' // this is optional
),
'city' => 'New York',
'country_id' => 'US', // two letters country code
'region' => 'New York', // can be empty '' if no region
'region_id' => '43', // can be empty '' if no region_id
'postcode' => '10450',
'telephone' => '123-456-7890',
'fax' => '',
'save_in_address_book' => 1
);
$customerFactory = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();
/**
* check whether the email address is already registered or not
*/
$customer = $customerFactory->setWebsiteId($websiteId)->loadByEmail($email);
/**
* if email address already registered, return the error message
* else, create new customer account
*/
if ($customer->getId()) {
echo 'Customer with email '.$email.' is already registered.';
} else {
try {
$customer = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();
$customer->setWebsiteId($websiteId);
$customer->setEmail($email);
$customer->setFirstname($firstName);
$customer->setLastname($lastName);
$customer->setPassword($password);
// save customer
$customer->save();
$customer->setConfirmation(null);
$customer->save();
$customAddress = $objectManager->get('MagentoCustomerModelAddressFactory')->create();
$customAddress->setData($address)
->setCustomerId($customer->getId())
->setIsDefaultBilling('1')
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
// save customer address
$customAddress->save();
//reindex customer grid index
$indexerFactory = $objectManager->get('MagentoIndexerModelIndexerFactory');
$indexerId = 'customer_grid';
$indexer = $indexerFactory->create();
$indexer->load($indexerId);
$indexer->reindexAll();
// Create customer session
$customerSession = $objectManager->create('MagentoCustomerModelSession');
$customerSession->setCustomerAsLoggedIn($customer);
echo 'Customer with email '.$email.' is successfully created.';
} catch (Exception $e) {
echo $e->getMessage();
}
}
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f236588%2fmagento-2-programmatically-create-customer-and-then-login%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The customer probablu isn't visible in the admin customer grid because that get's cached. I also had that problem.
You can log in the customer like this:
public function loginNewCustomer($customer){
if (!$this->customerSession->isLoggedIn()) {
try {
$this->customerSession->setCustomerAsLoggedIn($customer);
} catch (Exception $e) {
// Customer can't log in, this might be because the account needs to be confirmed.
}
}
}
If you don't have the customer object yet at that location you can get $customer like this:
$customer = $this->_customer->loadByEmail("test@m2s.com");
$this->customerSession->setCustomerAsLoggedIn($customer);
add a comment |
The customer probablu isn't visible in the admin customer grid because that get's cached. I also had that problem.
You can log in the customer like this:
public function loginNewCustomer($customer){
if (!$this->customerSession->isLoggedIn()) {
try {
$this->customerSession->setCustomerAsLoggedIn($customer);
} catch (Exception $e) {
// Customer can't log in, this might be because the account needs to be confirmed.
}
}
}
If you don't have the customer object yet at that location you can get $customer like this:
$customer = $this->_customer->loadByEmail("test@m2s.com");
$this->customerSession->setCustomerAsLoggedIn($customer);
add a comment |
The customer probablu isn't visible in the admin customer grid because that get's cached. I also had that problem.
You can log in the customer like this:
public function loginNewCustomer($customer){
if (!$this->customerSession->isLoggedIn()) {
try {
$this->customerSession->setCustomerAsLoggedIn($customer);
} catch (Exception $e) {
// Customer can't log in, this might be because the account needs to be confirmed.
}
}
}
If you don't have the customer object yet at that location you can get $customer like this:
$customer = $this->_customer->loadByEmail("test@m2s.com");
$this->customerSession->setCustomerAsLoggedIn($customer);
The customer probablu isn't visible in the admin customer grid because that get's cached. I also had that problem.
You can log in the customer like this:
public function loginNewCustomer($customer){
if (!$this->customerSession->isLoggedIn()) {
try {
$this->customerSession->setCustomerAsLoggedIn($customer);
} catch (Exception $e) {
// Customer can't log in, this might be because the account needs to be confirmed.
}
}
}
If you don't have the customer object yet at that location you can get $customer like this:
$customer = $this->_customer->loadByEmail("test@m2s.com");
$this->customerSession->setCustomerAsLoggedIn($customer);
answered Jul 31 '18 at 9:42
SanneSanne
844316
844316
add a comment |
add a comment |
Here is the solution :
<?php
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
$websiteId = $storeManager->getStore()->getWebsiteId();
$firstName = 'Test';
$lastName = 'Test';
$email = 'aabb@example.com';
$password = 'Test@123';
$address = array(
'customer_address_id' => '',
'prefix' => '',
'firstname' => $firstName,
'middlename' => '',
'lastname' => $lastName,
'suffix' => '',
'company' => '',
'street' => array(
'0' => 'Customer Address 1', // this is mandatory
'1' => 'Customer Address 2' // this is optional
),
'city' => 'New York',
'country_id' => 'US', // two letters country code
'region' => 'New York', // can be empty '' if no region
'region_id' => '43', // can be empty '' if no region_id
'postcode' => '10450',
'telephone' => '123-456-7890',
'fax' => '',
'save_in_address_book' => 1
);
$customerFactory = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();
/**
* check whether the email address is already registered or not
*/
$customer = $customerFactory->setWebsiteId($websiteId)->loadByEmail($email);
/**
* if email address already registered, return the error message
* else, create new customer account
*/
if ($customer->getId()) {
echo 'Customer with email '.$email.' is already registered.';
} else {
try {
$customer = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();
$customer->setWebsiteId($websiteId);
$customer->setEmail($email);
$customer->setFirstname($firstName);
$customer->setLastname($lastName);
$customer->setPassword($password);
// save customer
$customer->save();
$customer->setConfirmation(null);
$customer->save();
$customAddress = $objectManager->get('MagentoCustomerModelAddressFactory')->create();
$customAddress->setData($address)
->setCustomerId($customer->getId())
->setIsDefaultBilling('1')
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
// save customer address
$customAddress->save();
//reindex customer grid index
$indexerFactory = $objectManager->get('MagentoIndexerModelIndexerFactory');
$indexerId = 'customer_grid';
$indexer = $indexerFactory->create();
$indexer->load($indexerId);
$indexer->reindexAll();
// Create customer session
$customerSession = $objectManager->create('MagentoCustomerModelSession');
$customerSession->setCustomerAsLoggedIn($customer);
echo 'Customer with email '.$email.' is successfully created.';
} catch (Exception $e) {
echo $e->getMessage();
}
}
add a comment |
Here is the solution :
<?php
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
$websiteId = $storeManager->getStore()->getWebsiteId();
$firstName = 'Test';
$lastName = 'Test';
$email = 'aabb@example.com';
$password = 'Test@123';
$address = array(
'customer_address_id' => '',
'prefix' => '',
'firstname' => $firstName,
'middlename' => '',
'lastname' => $lastName,
'suffix' => '',
'company' => '',
'street' => array(
'0' => 'Customer Address 1', // this is mandatory
'1' => 'Customer Address 2' // this is optional
),
'city' => 'New York',
'country_id' => 'US', // two letters country code
'region' => 'New York', // can be empty '' if no region
'region_id' => '43', // can be empty '' if no region_id
'postcode' => '10450',
'telephone' => '123-456-7890',
'fax' => '',
'save_in_address_book' => 1
);
$customerFactory = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();
/**
* check whether the email address is already registered or not
*/
$customer = $customerFactory->setWebsiteId($websiteId)->loadByEmail($email);
/**
* if email address already registered, return the error message
* else, create new customer account
*/
if ($customer->getId()) {
echo 'Customer with email '.$email.' is already registered.';
} else {
try {
$customer = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();
$customer->setWebsiteId($websiteId);
$customer->setEmail($email);
$customer->setFirstname($firstName);
$customer->setLastname($lastName);
$customer->setPassword($password);
// save customer
$customer->save();
$customer->setConfirmation(null);
$customer->save();
$customAddress = $objectManager->get('MagentoCustomerModelAddressFactory')->create();
$customAddress->setData($address)
->setCustomerId($customer->getId())
->setIsDefaultBilling('1')
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
// save customer address
$customAddress->save();
//reindex customer grid index
$indexerFactory = $objectManager->get('MagentoIndexerModelIndexerFactory');
$indexerId = 'customer_grid';
$indexer = $indexerFactory->create();
$indexer->load($indexerId);
$indexer->reindexAll();
// Create customer session
$customerSession = $objectManager->create('MagentoCustomerModelSession');
$customerSession->setCustomerAsLoggedIn($customer);
echo 'Customer with email '.$email.' is successfully created.';
} catch (Exception $e) {
echo $e->getMessage();
}
}
add a comment |
Here is the solution :
<?php
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
$websiteId = $storeManager->getStore()->getWebsiteId();
$firstName = 'Test';
$lastName = 'Test';
$email = 'aabb@example.com';
$password = 'Test@123';
$address = array(
'customer_address_id' => '',
'prefix' => '',
'firstname' => $firstName,
'middlename' => '',
'lastname' => $lastName,
'suffix' => '',
'company' => '',
'street' => array(
'0' => 'Customer Address 1', // this is mandatory
'1' => 'Customer Address 2' // this is optional
),
'city' => 'New York',
'country_id' => 'US', // two letters country code
'region' => 'New York', // can be empty '' if no region
'region_id' => '43', // can be empty '' if no region_id
'postcode' => '10450',
'telephone' => '123-456-7890',
'fax' => '',
'save_in_address_book' => 1
);
$customerFactory = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();
/**
* check whether the email address is already registered or not
*/
$customer = $customerFactory->setWebsiteId($websiteId)->loadByEmail($email);
/**
* if email address already registered, return the error message
* else, create new customer account
*/
if ($customer->getId()) {
echo 'Customer with email '.$email.' is already registered.';
} else {
try {
$customer = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();
$customer->setWebsiteId($websiteId);
$customer->setEmail($email);
$customer->setFirstname($firstName);
$customer->setLastname($lastName);
$customer->setPassword($password);
// save customer
$customer->save();
$customer->setConfirmation(null);
$customer->save();
$customAddress = $objectManager->get('MagentoCustomerModelAddressFactory')->create();
$customAddress->setData($address)
->setCustomerId($customer->getId())
->setIsDefaultBilling('1')
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
// save customer address
$customAddress->save();
//reindex customer grid index
$indexerFactory = $objectManager->get('MagentoIndexerModelIndexerFactory');
$indexerId = 'customer_grid';
$indexer = $indexerFactory->create();
$indexer->load($indexerId);
$indexer->reindexAll();
// Create customer session
$customerSession = $objectManager->create('MagentoCustomerModelSession');
$customerSession->setCustomerAsLoggedIn($customer);
echo 'Customer with email '.$email.' is successfully created.';
} catch (Exception $e) {
echo $e->getMessage();
}
}
Here is the solution :
<?php
use MagentoFrameworkAppBootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('MagentoFrameworkAppState');
$state->setAreaCode('frontend');
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$storeManager = $objectManager->get('MagentoStoreModelStoreManagerInterface');
$websiteId = $storeManager->getStore()->getWebsiteId();
$firstName = 'Test';
$lastName = 'Test';
$email = 'aabb@example.com';
$password = 'Test@123';
$address = array(
'customer_address_id' => '',
'prefix' => '',
'firstname' => $firstName,
'middlename' => '',
'lastname' => $lastName,
'suffix' => '',
'company' => '',
'street' => array(
'0' => 'Customer Address 1', // this is mandatory
'1' => 'Customer Address 2' // this is optional
),
'city' => 'New York',
'country_id' => 'US', // two letters country code
'region' => 'New York', // can be empty '' if no region
'region_id' => '43', // can be empty '' if no region_id
'postcode' => '10450',
'telephone' => '123-456-7890',
'fax' => '',
'save_in_address_book' => 1
);
$customerFactory = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();
/**
* check whether the email address is already registered or not
*/
$customer = $customerFactory->setWebsiteId($websiteId)->loadByEmail($email);
/**
* if email address already registered, return the error message
* else, create new customer account
*/
if ($customer->getId()) {
echo 'Customer with email '.$email.' is already registered.';
} else {
try {
$customer = $objectManager->get('MagentoCustomerModelCustomerFactory')->create();
$customer->setWebsiteId($websiteId);
$customer->setEmail($email);
$customer->setFirstname($firstName);
$customer->setLastname($lastName);
$customer->setPassword($password);
// save customer
$customer->save();
$customer->setConfirmation(null);
$customer->save();
$customAddress = $objectManager->get('MagentoCustomerModelAddressFactory')->create();
$customAddress->setData($address)
->setCustomerId($customer->getId())
->setIsDefaultBilling('1')
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
// save customer address
$customAddress->save();
//reindex customer grid index
$indexerFactory = $objectManager->get('MagentoIndexerModelIndexerFactory');
$indexerId = 'customer_grid';
$indexer = $indexerFactory->create();
$indexer->load($indexerId);
$indexer->reindexAll();
// Create customer session
$customerSession = $objectManager->create('MagentoCustomerModelSession');
$customerSession->setCustomerAsLoggedIn($customer);
echo 'Customer with email '.$email.' is successfully created.';
} catch (Exception $e) {
echo $e->getMessage();
}
}
answered Jul 31 '18 at 12:38
Amatya TrivediAmatya Trivedi
539
539
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f236588%2fmagento-2-programmatically-create-customer-and-then-login%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Any error in log file when above script executed?
– Dhiren Vasoya
Jul 31 '18 at 11:01
No error in log files. I can log in from login page but it is not visible in admin customer's grid.
– Amatya Trivedi
Jul 31 '18 at 11:45
Try to run indexing one, then check.
– Dhiren Vasoya
Jul 31 '18 at 11:51