0

I have created a custom attribute for a customer in Magento 2 and when I tried to save the value it gives me an error "Trying to get property of non-object".

How can I save the attribute value? Here is my code to save the attribute. Any help.

$customer is the factory class of customer

$customer->setCustomAttribute('custom', 'hello');
$customer->save();


$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();

        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        $customerSetup->addAttribute(
                \Magento\Customer\Model\Customer::ENTITY,
                'custom',
                [
                    'type'         => 'varchar',
                    'label'        => 'custom',
                    'input'        => 'text',
                    'required'     => false,
                    'visible'      => false,
                    'user_defined' => true,
                    'sort_order' => 90,
                    'position' => 90,
                    'system' => 0,
                ]

            );
        $attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'custom')
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address'],
            ]);

        $attribute->save();
2
  • can you please add the code? How you created Custom Attribute? Jul 17, 2018 at 18:31
  • Abhishek Panchal please check now.
    – Avesh Naik
    Jul 18, 2018 at 8:20

2 Answers 2

0

Try Loading customer with customer repository like below:

protected $_customerRepositoryInterface;
public function __construct(
    ....
    \Magento\Customer\Model\Customer $customerModel,
    ....
) {
    ....
    $this->customerModel = $customerModel;
}

Then load customer and set value of custom attribute like below:

$customerId = 1;
$customer = $this->customerModel->load($customerId);
$customer->setData('custom','value') // or $customer->setCustom('value');
$customer->save();
3
  • Uncaught Error: Call to undefined method save()
    – Avesh Naik
    Jul 18, 2018 at 8:36
  • Try the exact code I edited above.
    – mighty_hk
    Jul 18, 2018 at 8:45
  • Now different error Trying to get property of non-object
    – Avesh Naik
    Jul 18, 2018 at 8:57
0

I have created two customer custom attributes user_name and recovery_email. Now while creating or updating customer I am using the following code. Hope this will help you as well.

User required classes

 use Magento\Framework\Exception\LocalizedException;

Declare customer factory member in your class

 /**
 * @var \Magento\Customer\Model\CustomerFactory
 */
protected $customerFactory;

Initialize it in the constructor

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

Use in your class methods

       try {
               // Instantiate object
                $websiteId = 1; //for now later you can update accordingly
                $customerObj = $this->customerFactory->create();
                $customerObj->setWebsiteId($websiteId);

                // Preparing data for new customer
                $customerObj->setEmail($email);
                $customerObj->setFirstname($firstName);
                $customerObj->setLastname($lastName);
                $customerObj->setPassword($password);
                $customerObj->setUserName($userName); //attribute code is user_name
                $customerObj->setRecoveryEmail($email); //attribute code is recovery_email

                // Save data
                $customerObj->save();
                $customerObj->sendNewAccountEmail();


            }catch (\Exception $exception){
                throw new LocalizedException(__($exception->getMessage()));
            }

Feel free to comment if there is an issue. Here you can use

 //$customerObj->setUserName($userName);
 $customerObj->setCustom('value'); 

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.