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.

  • when you are storing that value are u using observer or with in controller ? – Ansar Husain Aug 28 '18 at 6:46
  • I am using that code in controller. – jafar pinjar Aug 28 '18 at 6:47
4

Try that code to save custom attribute of customer

$customerRepository = $this->objectManager->get('Magento\Customer\Api\CustomerRepositoryInterface');

$customer = $customerRepository->getById('customer_id');

$customer->setCustomAttribute("is_checked",'0');

$customerRepository->save($customer);
| improve this answer | |
  • is_checked is customer custom attribute of backend_type is int in the table – jafar pinjar Aug 28 '18 at 6:59
  • I am also using same code Ansar, used within quotes and checked, its same again – jafar pinjar Aug 28 '18 at 7:00
  • how to set custom address attribute value? I created multi select attribute, value s not getting saved – jafar pinjar Feb 1 '19 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

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

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.