Posted on 16/11/2023 16:48:26
The cart endpoints supports anonymous with no problems - but the process have more steps than "cartcmd=add" where it will work something like this:
- Dynamicweb will create the cart (Order object) - using your context currency, country (for VAT and delivery cost) and language
- Dynamicweb will create the cart line(s)
- Dynamicweb will store the cart in your cookie/session/user
When in headless, you need to do the same - but now you are in charge - you own 'state' in the client where in the above scenario, Dynamicweb takes care of state on the server.
So it will be like this when doing an anonymous cart:
- Create the cart using POST /dwapi/ecommerce/carts/create - you need to send currency, country and language from your users context/state (Your JS app holds these now)
- The response from that post will be an OrderViewmodel
- C# (https://doc.dynamicweb.com/apix/api/Dynamicweb.Ecommerce.Frontend.OrderViewModel.html)
- Json: See the "Schema" in the 'Respsonses' section of the Swagger UI: https://swiftdemo.dynamicweb-cms.com/dwapi/docs/index.html?url=/dwapi/api.json&layout=BaseLayout#/Carts/Carts_CreateCartBody
- The OrderViewmodel from the return above, will have a 'Secret' property that you need next: https://doc.dynamicweb.com/apix/api/Dynamicweb.Ecommerce.Frontend.OrderViewModel.html#Dynamicweb_Ecommerce_Frontend_OrderViewModel_Secret
- Store that secret in your JS app's state - along with the currency, country, language and user if he is logged in.
- Using the Cart secret, you can use the POST, PATCH and DELETE endpoints (/dwapi/ecommerce/carts/{secret}) to add, change and remove orderlines
- To complete the cart and place an order, make a POST to /dwapi/ecommerce/carts/{secret}/createorder - order is now complete
If the user is logged in using the POST /dwapi/users/authenticate you get back a JWT - that JWT can be used at any given time on the cart endpoints adding a bearer token - it is not required, but optional. If it is passed along, the cart that is created in step 1 above will be stored on that user and you can later return that users carts using the /dwapi/ecommerce/carts endpoint which requires a JWT to work. Also when the JWT is added, the user will get personalized prices and discounts - and if using live integration, that will be triggered.
BR Nicolai