0

Customer custom attribute value did not save using rest API in Magento 2.3?

API URL: http://127.0.0.1/magento2/rest/V1/customers

Method: POST

My POST JSON request data is :

{
    "customer": {
        "email": "test@gmail.com",
        "firstname": "Tedst2",
        "lastname": "yosur",
        "store_id": 1
    },
    "password": "admin@123",
    "custom_attributes": [{
            "attribute_code": "test_attribute",
            "value": "simple"
        }
    ]
}

Setup-Script

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {

        /** @var CustomerSetup $customerSetup */
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();

        /** @var $attributeSet AttributeSet */
        $attributeSet = $this->attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        $customerSetup->addAttribute(Customer::ENTITY, 'test_attribute', [
            'type' => 'varchar',
            'label' => 'test attribute',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 1000,
            'position' => 1000,
            'system' => 0,
        ]);
        //add attribute to attribute set
        $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'test_attribute')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer'],
        ]);

        $attribute->save();


    }

The Customer is created but the test_attribute is not "simple". Is there anyone who had the same problem and fixed it? Please let me know how to resolve it?.or any another way to save?

0

I have used the below body for creating customers through Rest API and it worked properly.

{ 
  "customer": 
  {
  "email": "test@gmail.com", 
  "firstname": "x", 
  "lastname": "y", 
  "website_id":1, 
  "group_id":1, 
  "custom_attributes": 
  [ 
    { 
    "attribute_code": "test_attribute", 
    "value": "simple" 
    } 
  ]

},
"password": "admin@123"
}

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.