1

I created a custom customer attribute using an install script.

$customerSetup -> addAttribute(\Magento\Customer\Model\Customer::ENTITY,
        'is_mpowersync',
        [
        'label' => 'Is Mpowersync',
        'system' => false,
        'position' => 120,
        'sort_order' =>120,
        'visible' =>  true,
        'type' => 'int',
        'input' => 'select',
        "source" => "\Magento\Eav\Model\Entity\Attribute\Source\Boolean",
        'required' => false,
        'default' => '0',
        'group' => 'Account Information',
        ]
        );

    $customerSetup -> getEavConfig() -> getAttribute('customer', 'is_mpowersync')->setData('is_user_defined',1)->setData('is_required',0)->setData('used_in_forms', ['adminhtml_customer', 'customer_account_create', 'customer_account_edit']) -> save();

I want to set a value the above created custom attribute programmatically creating the customer. I tried below 3 methods, but data is not saving.

1 . $newcustomer->setData('is_mpowersync','1');

2 . $newcustomer->setCustomAttribute('is_mpowersync',1);

3 . $newcustomer->setIsMpowersync(1);

Both method not saving the data, if I save from backend admin customer creation it saving the data for the custom attribute.

How can I save custom customer attribute in Magento 2 programmatically?

1

Here in this example, I have tried to save custom attribute value using the Controller file in custom extension. To fix this issue, firstly we need to create ‘controller.php’ file at below location.

app\code\Vendor\Extension\Controller\Index

<?php
Namespace Vendor\Extension\Controller\Index;

class Controller extends \Magento\Framework\App\Action\Action
{

protected $customer;

protected $customerFactory;

public function __construct(
    \Magento\Customer\Model\Customer $customer
    \Magento\Customer\Model\ResourceModel\CustomerFactory $customerFactory
)
{
    $this->customer = $customer;
    $this->customerFactory = $customerFactory;
}

public function execute()
{

    $custom = $this->_customerFactory ->create();
    $custom = $custom->setWebsiteId($helperData->getWebsiteId());
    $custom = $custom->loadByEmail("abc123@xyz.com");
    $custom->setCustomAttributeCode(custom attribute value);   

  // Main code to resolve this issue.
    $customerData = $custom->getDataModel();
    $customerData->setCustomAttribute('custom_attribute code', 'custom attribute value');
    $custom->updateData($customerData);

    $custom->save();
}
}

For more refrance click here

Note : Above code is not tested. you have to check.

I hope it helps!

  • thanks for your answer, i want to set the data during customer create programatically. – Mujahidh Apr 1 at 5:21
  • I will get back to you if I found something. – Chirag Patel Apr 1 at 5:34
  • i tried to load by email and update the attribute value, but still i cannot set the data and save it, but customer created – Mujahidh Apr 1 at 5:38
  • thanks, i missed this \Magento\Customer\Model\ResourceModel\CustomerFactory – Mujahidh Apr 2 at 4:22
  • ohhh! it's okay good now it's working. thanks for the appreciation. – Chirag Patel Apr 2 at 4:24

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.