Skip to content

Unable to set customer group when create customer with REST API

In Magento 2.4.5 I’m trying to create customer with REST API. In the payload I specify the group ID, but the new customer always get the default group ID.

Example payload:

$customerData = [
'customer' => [
    "email" => $_POST['email'],
    "firstname" => $_POST['company'],
    "lastname" => $codiceClienteTemp,
    "website_id" => $id_store_website_cliente,
    "store_id" => $id_store_website_cliente,
    "group_id" => $gruppoClienteTemp,
    "addresses" =>  [
        [
            "firstname" => $_POST['company'],
            "lastname" => 'none',
            "city" => $_POST['city'],
            "region" => [
                "region_code" =>$_POST['state'],
                "region" => $_POST['state']
            ],
            "postcode" => $_POST['code'],
            "country_id" => 'IT',
            "street" => [
                "data_item" => $_POST['lane']
            ],
            "defaultShipping" => true,
            "defaultBilling" => false,
        ],
        [
            "firstname" => $_POST['company'],
            "lastname" => 'none',
            "city" => $_POST['city'],
            "region" => [
                "region_code" =>$_POST['state'],
                "region" => $_POST['state']
            ],
            "postcode" => $_POST['code'],
            "country_id" => 'IT',
            "street" => [
                "data_item" => $_POST['lane']
            ],
            "defaultShipping" => false,
            "defaultBilling" => true,
        ]
    ],
],
"password" => $_POST['password']

];

And then I send the API call with cURL:

    $ch = curl_init("https://example.com/index.php/rest/V1/customers");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($customerData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . $token));
$result = curl_exec($ch);
$result = json_decode($result, 1);

I set $gruppoClienteTemp to 2, but the customer has group 1.