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('customeremail@mail.com');
    $customer->setFirstname('FirstName');
    $customer->setLastname('LastName');
    $customer->setPassword('123456789');
    $customer->setLoginEmail('test@gmail.com');
    $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/

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
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.