TransWikia.com

Customer custom attribute value did not save using rest api in magento 2.3?

Magento Asked on August 23, 2020

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": "[email protected]",
        "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?

One Answer

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

{ 
  "customer": 
  {
  "email": "[email protected]", 
  "firstname": "x", 
  "lastname": "y", 
  "website_id":1, 
  "group_id":1, 
  "custom_attributes": 
  [ 
    { 
    "attribute_code": "test_attribute", 
    "value": "simple" 
    } 
  ]

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

Correct answer by Dev on August 23, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP