1

I have created some custom attributes for a customer in magento2 community edition,

I have used below code to set in my custom code

use Magento\Customer\Api\AccountManagementInterface;
use Magento\Framework\App\Action\Context;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Model\CustomerExtractor;

class CreateCustomer extends \Magento\Customer\Controller\AbstractAccount
 {
  protected $customerRepository;
  protected $accountManagement;
  protected $customerExtractor;

  public function __construct(
    Context $context,
    AccountManagementInterface $accountManagement,
    CustomerExtractor $customerExtractor,
  ) {
    $this->accountManagement = $accountManagement;
    $this->customerExtractor = $customerExtractor;
   parent::__construct($context);
}
  public function execute()
{
  try {
       $customer = $this->customerExtractor->extract('customer_account_create', $this->_request);
        $password = $this->getRequest()->getParam('password');
        $confirmation = $this->getRequest()->getParam('password_confirmation');

        $customer = $this->accountManagement
            ->createAccount($customer, $password, $redirectUrl);

         $this->customerRepository = ObjectManager::getInstance()->get(
            \Magento\Customer\Api\CustomerRepositoryInterface::class
        );  

        $customer->setCustomAttribute('is_checked', 0);
        $this->customerRepository->save($customer);    
 }catch (StateException $e) {
      print_r($e->getMessage());
   }
  }
}
 $customer->setCustomAttribute('is_checked', 0);

This attribute is a custom attribute and I have set default_value as 0, but it is not saving on creating a customer. I can't see any value in the table for any of the customers.

This is not storing in the table. What is the reason please anyone helps me with this.

Here is the screenshot for the attribute in the table

enter image description here

attribute_code is customer_activated.

2
  • when you are storing that value are u using observer or with in controller ? Aug 28, 2018 at 6:46
  • I am using that code in controller. Aug 28, 2018 at 6:47

2 Answers 2

4

Try that code to save custom attribute of customer

$customerRepository = $this->objectManager->get('Magento\Customer\Api\CustomerRepositoryInterface');
$customerId = $params['customerId'];
$customer = $customerRepository->getById($customerId);
 
$customer->setCustomAttribute("is_checked",'0');
 
$customerRepository->save($customer);
3
  • is_checked is customer custom attribute of backend_type is int in the table Aug 28, 2018 at 6:59
  • I am also using same code Ansar, used within quotes and checked, its same again Aug 28, 2018 at 7:00
  • how to set custom address attribute value? I created multi select attribute, value s not getting saved Feb 1, 2019 at 4:26
0

Use setData to save the values, as try like this

$customer->setData(array('is_checked'=>0));

And use the try catch method for the save function to find possible exceptions.

Also please post the custom attribute creation code

7
  • can we use setData for custom attributes? Aug 28, 2018 at 7:18
  • Yes, Did you try this method?
    – Duke
    Aug 28, 2018 at 7:21
  • yes , i am getting error like below Exception message: Warning: Missing argument 2 for Magento\Framework\Api\AbstractSimpleObject::setData() Aug 28, 2018 at 7:23
  • try like this $customer->setData('is_checked',0);
    – Duke
    Aug 28, 2018 at 7:25
  • same again after using this code also. Aug 28, 2018 at 7:30

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.