AnswerBun.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": "[email protected]",
    "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": "[email protected]"
}

Correct answer by Dev on August 23, 2020

Add your own answers!

Related Questions

Magento 2 call APIexternal

1  Asked on November 27, 2021

   

Override method in LayoutProcessor

2  Asked on November 25, 2021 by creativename

       

Magento 2: How to set default layout to 1column

2  Asked on November 25, 2021 by nhinzky

   

Magento 2 Forgot Password page incorrect captcha

4  Asked on November 25, 2021 by mahmoudismail

         

Magento2: How to redirect minicart on checkout cart page?

3  Asked on November 25, 2021 by ravi-soni

     

magento 2 order grid loading issue after data migration

4  Asked on November 25, 2021 by nagaraju-kasa

         

Different locations for search bar depending on width

1  Asked on November 25, 2021 by jannis-s

     

Insert a custom block into another custom block, in Magento 2

1  Asked on November 25, 2021 by danilomorowinstech

   

How to Show Cart Discount in magento 2

1  Asked on November 25, 2021

 

How to set region value in checkout page?

1  Asked on November 25, 2021 by nubitol

       

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir