3

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.

1
  • I have the same issue in Magento 2.4.6
    – Akif
    Commented Jan 19 at 13:39

4 Answers 4

0

As you are accessing the POST /V1/customers API with a bearer token I am assuming you are creating customers from an external system, as opposed to a headless front-end or mobile app.

Looking at this file from the version you are using there is an ACL check. Does the Bearer token you are using have the Magento_Customer::manage permission?

3
  • thank you for your response. With token I'm using I can create the customer without problem, only the group is wrong. But if I try to update the customer with same token to change group after creation i got this error message: Customer is not authorized to access Magento_Customer::manage Commented Oct 21, 2022 at 13:57
  • Creating a customer via the API can be done by anonymous users. Who are you authenticating to the API as? The API is giving you the error of the exact issue. My guess is the token is for a user without the required permission, simple as that.
    – Craig
    Commented Oct 23, 2022 at 17:19
  • with anonymous user the customer is created but with default group id, not the group id I send with the API call. I solved using an admin token, with admin token I can set any group Id. Thank you for your support Commented Oct 24, 2022 at 10:12
0

I've tested your code in Magento 2.4.5 vanilla with sample data, and it set the correct customer group as expected (group 2). Might be you should double-check $gruppoClienteTemp, try to hardcode it to 2, and test again.

0

I am also facing a similar kind of problem in Adobe commerce 2.4.4. I tired to create customer using BULK API but the customer group is always mapped to the default customer group. Kindly advise on this issue.

0

I just had same issue. in my case, it was caused by a wrong token. make sure you're using the correct token in integration menu in the backend. or if you're using different way to use token please check if you use registered token. so when you are using a wrong token, the customer group ID will be not stored correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.