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": "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!

Related Questions

How to show downloadable links in a block?

0  Asked on January 16, 2021 by kamzata

     

Magento tax_class_id not importing

1  Asked on January 15, 2021 by sushivam

     

How to add firebase cdn’s in magento2?

0  Asked on January 15, 2021 by usama-sohail

   

Paypal log error

1  Asked on January 14, 2021 by fja3omega

     

Enable Dynamic price in bundle product

1  Asked on January 9, 2021 by miguel-angel-espinosa-garca

         

Magento 2 hidden products

1  Asked on January 6, 2021 by ramesh-kr

         

Ask a Question

Get help from others!

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