Developer forum

Forum » Development » Setting "active" cart via dwapi

Setting "active" cart via dwapi

Mikkel Hammer
Mikkel Hammer
Reply

Hi,

Is it possible to set current "active" cart for a user via the dwapi?
We have an app that uses the dwapi, where users can create new carts and add products and so on..

But we have an "issue" where if the user creates a cart and add products to it via the app, and then logs into the webshop, the cart is not "active" and shows the user has an empty cart.
The cart that was created via the app, is only visible for the user as a draft.

We're looking for a way to control which cart to load when the user logs into the webshop.
Hope anyone can help :)

Kinds regards,
Mikkel H


Replies

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Mikkel

The webapi is stateless so the concept of active cart does not exist. You have carts - which is active depends on the javascript app and what it remembers.

If you are using a regular DW implementation in razor, you can use a cart command to set the active cart: https://doc.dynamicweb.com/template-tags/introduction/ecommerce/cart-commands#10191

Votes for this answer: 1
 
Mikkel Hammer
Mikkel Hammer
Reply

Hi Nicolai,

The webshop i'm mentioning is a regular DW implementation, the problem is that we're trying to "sync" the webshop(DW) with our cart from the app.
Meaning that if a user has no current cart on the webshop(DW) and creates one via the app(dwapi). 
We want that created cart to be the current cart when the user then enters the webshop. So a way to override/change the current cart in the DW webshop context so to speak.

I tried calling the "CartCmd=SetCart" command from the app via a HttpClient GET/POST request, but it doesn't seem to work when i log into the webshop with the user. 
The cart is still only a draft cart.

The only success we have setting the cart on the webshop, is if we the open the cart in a mobile browser via the app with the "CartCmd=LoadOrder" command.
But we're looking for a more subtle way to change it, preferably during cart creation.

Maybe we'll try to look into making a custom solution and get back with what we come up with,
but i thought others would find changing the current DW cart via the dwapi a useful command :)

Kind regards,
Mikkel Hammer

 
Nicolai Pedersen
Reply

I do not think we have something for that - should be fairly simple though. This subscriber will have the user after login. If Common.Context.Cart is null, go to EcomOrders table, find the latest version of an orderid that is a cart - the userid is on that cart. Then set common.context.cart to an order instance of that cartid - using OrderService

[Dynamicweb.Extensibility.Notifications.Subscribe(Dynamicweb.Notifications.Standard.User.OnExtranetLogin)]
    public class OnExtranetLogOnObserver : Dynamicweb.Extensibility.Notifications.NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.Notifications.NotificationArgs args)
        {
            if (args == null)
                return;

            if (!(args is Dynamicweb.Notifications.Standard.User.OnExtranetLoginArgs))
                return;

            

            Dynamicweb.Notifications.Standard.User.OnExtranetLoginArgs item = (Dynamicweb.Notifications.Standard.User.OnExtranetLoginArgs)args;

            var user = item.User;
            
        }
    }
 
Mikkel Hammer
Mikkel Hammer
Reply

Hi Nicolai,

Thanks a lot for helping with the base of the NotificationSubscriber, was something of that sort we were going to do.

But now that i think of it, it sort of "breaks" the whole meaning of drafts in the standard DW implementation, since it would load your lastest draft order if you emptied your cart before logging out, if you just saved your current cart as a draft and then logs out, or if you just completed your current cart and then logs out, since those scenarios leaves you with no "active" cart.

Which leads into another scenario with the dwapi with the whole talk of standard DW "active" cart.
When you get all your carts with the dwapi, you have no way of knowing if it's a draft or an "active" DW cart.

In the GET carts (/dwapi/ecommerce/carts/) request below here, i get two carts. Both are actual draft carts.
So if i log into the webshop, i'll have no current cart.
But when i get all carts for the user for our app, i can only use Modified as what i "think" might be the current webshop cart.
If we just always load the lastest modified cart, we'll have two different scenarios for our users, where on the webshop you have no cart, but the app loads your lastest edited draft cart.



So i think a solution could maybe be to extend the cart model with an "active" property, both in the UserCartsResponse model so you can see if the current user has an active cart, and which one it is, else you can create a new cart. And then in the OrderViewModel so you can change which cart is the "active" one from the dwapi.

But that would probably mean something have to change in standard DW to use the same logic with the "active" cart.
Since the only thing i've found that DW uses for selecting "active" cart is some sort of sessionid on the cart, but i'm not sure that's the only thing that get used.

EDIT: just a note on the above line i've made italic (doesn't seem it allows me to cross over text). I've been made aware that it seems to be stored in the AccessUser table in the AccessUserCartId column. Maybe that's something we can hook into? So another approach could be extending UserViewModel with an "ActiveCartSecret". That way you can get the information without checking through every order in /dwapi/ecommerce/carts/ for an "active" property (though it would still be a welcome change to have it labeled on there). Then you can just call /dwapi/ecommerce/carts/{secret} to check if the cart is still available. But there a "mismatch" with AccessUser storing it as Cart Id, and the dwapi needing the cart secret, but it still seems as a possible route to go aswell :)

Hope it makes sense :)


Best regards,
Mikkel Hammer

 
Nicolai Pedersen
Reply
This post has been marked as an answer

It does makes sense and I get you.

Active cart is not just that simple. A user can have many active carts -

  • using order contexts (See table [EcomOrderContextAccessUserRelation]),
  • shared carts (more users on the same account depending on settings),
  • isolated carts ("/Globalsettings/Ecom/Cart/FullCartIsolation") or
  • future carts (You can set ?orderdate=<some-future-date>).

AccessUserCartID can currently be used if the cart is created in the Dynamicweb Frontend - that fields is not set when the cart is created using the webapi. 

We could set the AccessUserCartID when creating carts (POST), updating carts (PATCH) and creating orders (POST /createorder) if a valid bearer token is in the header. And then return that value on the user object on auth. But it would not bbbe working completely like the current implementation though.

Votes for this answer: 2
 
Mikkel Hammer
Mikkel Hammer
Reply

Hi Nicolai,

Aha, didn't know it had such a indepth logic behind it, thanks for pointing it out and explaining!

It sounds like that would be enough to fulfill what we're looking for, though im not sure what you mean with the "But it would not bbbe working completely like the current implementation though" part. Is it regarding the webapi needing the cart secret and not cart id?

Best regards,

Mikkel Hammer

 
Nicolai Pedersen
Reply

By that I mean that if you are using the more complex active cart features in a frontend and mixes with this when it comes, unexpected results could occir.

so a very rare potential issue not relevant for you 😉

 
Mikkel Hammer
Mikkel Hammer
Reply

Thanks for the clarification, and glad to hear it shouldn't be relevant for us 😉

Is this something we could expect any time soonish, or is it a feature you'll look into maybe along with DW10?
Since it will come with unexpected results in rare occasions.

And thanks for your insigth and explations on the topic, it helps alot! 

 
Mikkel Hammer
Mikkel Hammer
Reply

Hi DW,

Where did this end up? :)

Best regards,
Mikkel Hammer

 
Mikkel Hammer
Mikkel Hammer
Reply

bump

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

Devops#10881

This feature will set the AccessUserCartID on the accessuser when a cart is changed in the webapi.

if the cart has a context id, it will be stored on EcomOrderContextAccessUserRelation like using regular cart commands.

Currently a pull request - will be released when merged. This change only affects Dynamicweb.Ecommerce package.

BR Nicolai

Votes for this answer: 1
 
Mikkel Hammer
Mikkel Hammer
Reply

Thanks Nicolai, much appreciated!

Best regards,
Mikkel Hammer

 
Kristian Kirkholt Dynamicweb Employee
Kristian Kirkholt
Reply
This post has been marked as an answer

Hi Mikkel

Feature #10881 has been implemented in Dynamicweb version 9.14.6
You can get this version from the download section https://doc.dynamicweb.dk/downloads/dynamicweb-9

Kind Regards
Care Support
Kristian Kirkholt

Votes for this answer: 1

 

You must be logged in to post in the forum