0

I am using following code to create customer in magento 2 programmatically

namespace PackageName\ModuleName\Helper;
use Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');
$customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory');
$websiteId = $storeManager->getWebsite()->getWebsiteId();
// here $total_obj is customer array i am getting from third party service
foreach($total_obj as $arr){
 $customer = $customerFactory->create();
 $customer->setWebsiteId($websiteId);
 $customer->setEmail('[email protected]');
 $customer->setFirstname('FirstName');
 $customer->setLastname('LastName');
 $customer->setPassword('123456789');
 $customer->setLoginEmail('[email protected]');
 $customer->save();
}

from the above code i am not able to set login_email value. all details getting saved but not this custom attribute is not saving.

Please let me know what i am doing wrong.

for creation custom attribute i had followed https://webkul.com/blog/how-to-create-customer-custom-attribute-in-magento-2-0/

asked Aug 7, 2018 at 14:03

2 Answers 2

0

**Customer custom attribute not saving while creating customer programmatically in magento 2.1.11*

Reindex and flush cache then it should be work

Run these command

 php bin/magento cache:flush
 php bin/magento indexer:reindex
answered Aug 7, 2018 at 14:13
0

Use setCustomAttribute to save custom attributes

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $customerRepository =$objectManager->get('Magento\Customer\Api\CustomerRepositoryInterface'); 
 $customerId = 'you customer Id';
 $customerNew = $customerRepository->getById($customerId); 
 $customerNew->setCustomAttribute("custom_attribute_code","value");
 $customerRepository->save($customerNew); 

This may help you. Please check this

answered Aug 7, 2018 at 15:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.