Magento 2: Adding products to guest cart from external site via REST API
-
05-10-2020 - |
Question
I am trying to build a simple tool that lets me add products directly to the shopping cart from an external CMS site using the REST API. Currently, I am trying to leverage the guest-carts
to minimize security risk.
It looks like I need to first generate a new guest cart id:
POST /rest/default/V1/guest-carts
Then I add products to that cart:
POST /rest/default/V1/guest-carts/<generated cart id>/items {
"cartItem": {
"sku": "<item_sku>",
"qty": 1
}
}
Which returns:
{ "message": "No such entity with %fieldName = %fieldValue",
"parameters": { "fieldName": "cartId", "fieldValue": null } }
If I add cartId
to the first part I now get:
Next Exception: Message: Property "CartId" does not have corresponding setter in class "Magento\Quote\Api\Data\CartItemInterface". in <magento_root>/vendor/magento/framework/Webapi/ErrorProcessor.php:195
Stack trace:
#0 <magento_root>/vendor/magento/framework/Webapi/ErrorProcessor.php(139): Magento\Framework\Webapi\ErrorProcessor->_critical(Object(LogicException))
#1 <magento_root>/vendor/magento/module-webapi/Controller/Rest.php(219): Magento\Framework\Webapi\ErrorProcessor->maskException(Object(LogicException))
#2 <magento_root>/var/generation/Magento/Webapi/Controller/Rest/Interceptor.php(37): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http))
#3 <magento_root>/vendor/magento/framework/App/Http.php(135): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#4 <magento_root>/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch()
#5 <magento_root>/index.php(39): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))
#6 {main} [] []
What's going on? How do I use the guest cart REST API?
Solution
Nevermind, it looks like you need to include quote_id
in the cartItem
payload.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange