0

When I try to add to card any item via REST API I get this error:

"{"message":"\"%fieldName\" is required. Enter and try again.","parameters":{"fieldName":"quoteId"}}"

First time I get the cart id calling this

"rest/V1/carts/mine"

than I call

"rest/V1/carts/mine/items"

with this data:

{
  "cartItem": {
    "sku": "24-WG080",
    "qty": 1,
    "quote_id": cartID
  }
}

Tried with cart-item, quoteId, quote_id and more...

I'm stuck, I have no ideea what is wrong, the documentation is wrong? https://devdocs.magento.com/guides/v2.4/rest/tutorials/orders/order-add-items.html

Enabed developer mode, and now I got this error in response:

"#0 [internal function]: Magento\Quote\Model\Quote\Item\Repository->save(Object(Magento\Quote\Model\Quote\Item))
#1 /home2/xxx/vendor/magento/module-webapi/Controller/Rest/SynchronousRequestProcessor.php(95): call_user_func_array(Array, Array)
#2 /home2/xxx/vendor/magento/module-webapi/Controller/Rest.php(188): Magento\Webapi\Controller\Rest\SynchronousRequestProcessor->process(Object(Magento\Framework\Webapi\Rest\Request\Proxy))
#3 /home2/xxx/vendor/magento/framework/Interception/Interceptor.php(58): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http))
#4 /home2/xxx/vendor/magento/framework/Interception/Interceptor.php(138): Magento\Webapi\Controller\Rest\Interceptor->___callParent('dispatch', Array)
#5 /home2/xxx/vendor/magento/framework/Interception/Interceptor.php(153): Magento\Webapi\Controller\Rest\Interceptor->Magento\Framework\Interception\{closure}(Object(Magento\Framework\App\Request\Http))
#6 /home2/xxx/generated/code/Magento/Webapi/Controller/Rest/Interceptor.php(23): Magento\Webapi\Controller\Rest\Interceptor->___callPlugins('dispatch', Array, Array)
#7 /home2/xxx/vendor/magento/framework/App/Http.php(116): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#8 /home2/xxx/generated/code/Magento/Framework/App/Http/Interceptor.php(23): Magento\Framework\App\Http->launch()
#9 /home2/xxx/vendor/magento/framework/App/Bootstrap.php(264): Magento\Framework\App\Http\Interceptor->launch()
#10 /home2/xxx/pub/index.php(29): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http\Interceptor))
#11 {main}"

3 Answers 3

2
  1. Create a request to create the empty cart POST

{{magento_api_url}}/rest/default/V1/guest-carts/

this will return a token which is the cart ID (e.g 'KZepbhhPiGORreBKqIRlMCywGDTyQtR4')

  1. Use that cart ID in the request which adds an item to the cart POST

{{magento_api_url}}/rest/default/V1/guest-carts/KZepbhhPiGORreBKqIRlMCywGDTyQtR4/items

with the payload similar to what you see below (where quote_id is the key you previously used and the SKU is the SKU of the product you want to add):

{
    "cartItem": {
        "qty": 5,
        "sku": "24-UG06"
    }
}
0

Refer to the Below Reference.

https://devdocs.magento.com/guides/v2.4/rest/tutorials/orders/order-create-quote.html

1
  • guest cart works, the one for logged in users doesn't
    – Adrian
    Jan 5, 2022 at 8:42
0

There is a lot of missing context in this question but the short answer is no, the documentation you mention is not wrong, you are probably not sending Authorization: Bearer <customer token> in the headers.

The long and detailed answer is...

Check vendor/magento/module-quote/etc/webapi.xml. Almost all the /V1/carts/mine API endpoints have the following:

<data>
    <parameter name="cartId" force="true">%cart_id%</parameter>
</data>

The force="true" property set any parameter with it (cartId in this case) as null. This is being made by vendor/magento/module-webapi/Controller/Rest/ParamsOverrider.php.

The override method iterates over the params received in your API request and the following lines overrides the provided value by null:

public function override(array $inputData, array $parameters)
{
    foreach ($parameters as $name => $paramData) {
        ...
        if ($paramData[Converter::KEY_FORCE] || !$this->isNestedArrayValueSet($inputData, $arrayKeys)) {
            ...
            if (isset($this->paramOverriders[$paramValue])) {
                $value = $this->paramOverriders[$paramValue]->getOverriddenValue();
            } ...
            ...
        }
    }
    ...
}

$paramData[Converter::KEY_FORCE] is what the XML sent previously mentioned (force="true").

In the particular case of /V1/carts/mine/ endpoints, the cartId value is set as null because it is intended to be retrieved from the context. You can see this implementation at vendor/magento/module-quote/Model/Webapi/ParamOverriderCartId.php:

public function getOverriddenValue()
{
    try {
        if ($this->userContext->getUserType() === UserContextInterface::USER_TYPE_CUSTOMER) {
            $customerId = $this->userContext->getUserId();

            /** @var \Magento\Quote\Api\Data\CartInterface */
            $cart = $this->cartManagement->getCartForCustomer($customerId);
            if ($cart) {
                return $cart->getId();
            }
        }
    } catch (NoSuchEntityException $e) {
        throw new NoSuchEntityException(__('Current customer does not have an active cart.'));
    }
    return null;
}

Having said that, the solution is to avoid using /V1/carts/mine without sending customer token as a header.

Use /V1/carts/{cartId} endpoints instead. In your case, use /V1/carts/{cartId}/items/{itemId} to add/update the specified cart item.

Please be aware, on any interaction with the cart/quote via API... Provide quote_id into the body of the request for endpoints which mentions this value in the Adobe Commerce's documentation. For example:

POST https://www.example.com/rest/V1/carts/INSERT_QUOTE_ID_VALUE/items

{
    "cartItem": {
        "sku": "INSERT_SKU_VALUE",
        "qty": 1,
        "quote_id": "INSERT_QUOTE_ID_VALUE"
    }
}

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.