Developer forum

Forum » Development » Anonymous carts with webapi/headless implementation

Anonymous carts with webapi/headless implementation

Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi,

 

We have a customer doing there own development and they are using the webapi. The current struggle is that, mimicing their current site, they would allow users to add items to the cart, before they log in.

 

  • I can see the current endpoint requires a cart secret
    /dwapi/ecommerce/carts/{secret}/items
  • But to get the secret, I need to be authenticated
    /dwapi/ecommerce/carts/active
  • Which can only be set after the user has been authenticated (so as to get a BearerToken)
    /dwapi/users/authenticate

 

Could there be a way to use the authentication endpoint without username and password (anonymous) and return a token just for that "session"? Or some other way to manage carts for anonymous users.

 

Best Regards,

Nuno Aguiar


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

The cart endpoints supports anonymous with no problems - but the process have more steps than "cartcmd=add" where it will work something like this:

  1. Dynamicweb will create the cart (Order object) - using your context currency, country (for VAT and delivery cost) and language
  2. Dynamicweb will create the cart line(s)
  3. 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:

  1. 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)
    1. The response from that post will be an OrderViewmodel
      1. C# (https://doc.dynamicweb.com/apix/api/Dynamicweb.Ecommerce.Frontend.OrderViewModel.html)
      2. 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
  2. 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
    1. Store that secret in your JS app's state - along with the currency, country, language and user if he is logged in.
  3. Using the Cart secret, you can use the POST, PATCH and DELETE endpoints (/dwapi/ecommerce/carts/{secret}) to add, change and remove orderlines
  4. 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

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Nicolai,

 

Sounds good. I guess what I missed was the POST /dwapi/ecommerce/carts/create - not requiring the JWT Token. I'll get back to the Devs and see if they can get over this hump.

 

Thank you,

Nuno Aguiar

 

You must be logged in to post in the forum