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

namespace PackageName\ModuleName\Helper;
use Magento\Framework\App\Bootstrap;

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$state = $objectManager->get('\Magento\Framework\App\State');

$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->setEmail('[email protected]');
    $customer->setLoginEmail('[email protected]');

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/

2 Answers 2


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

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

This may help you. Please check this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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