Developer forum

Forum » Ecommerce - Standard features » Creating a new cart with a unique cart context

Creating a new cart with a unique cart context

Scott Forsyth Dynamicweb Employee
Scott Forsyth
Reply

I have a situation where each browser session needs to have a unique cart, even if it's the same user. This is for a Punchout situation where we're supporting adding items to the cart and then passing the pick list back to the Punchout system. 

 

For this to work, I need someone to be able to start the Punchout session in two different browsers, but using the same username, and each browser should have isolated carts. The Punchout system starts off by sending the requests to a page that is listening with a custom module. On this module I do a number of things, including creating a new order.

 

I'm attempting to do this by using unique Order Contexts. What I'm struggling with is to create a new cart and set the OrderContextID. Here's the code that I'm currently using:

Order cart = Dynamicweb.eCommerce.Common.Context.Cart;

if (cart == null)
{
   cart = new Order() { IsCart = true, LanguageID = EComContext.LanguageID, ShopID = "SHOP1", OrderContextID = contextId };
   cart.Save();
   Context.SetCart(cart);
}

 

The contextId is a string value containing an available order context Id which hasn't been used by this user in the last 24 hours. 

 

The thing that goes wrong is that, when the cart doesn't already exist (i.e. cookies are cleared), this code is executed, but when adding an item to the cart, it ignores this new cart and creates a new one. I can see in the EcomOrders table that the first cart was created successfully. However, after adding a new item to the cart, a new cart is created rather than using the cart that I just created.

 

It tried to make the cart more valid by setting CustomerAccessUserID, CustomerAccessUserUserName, CustomerNumber, and VisitorSessionID (all of which are null after running the code above). However, that doesn't appear to help. A new cart is always created, so it ignores the one that I created.

 

So my question is: How do I correctly create a new cart and assign it a unique OrderContextID?

 

Thanks,

 

Scott

 


Replies

 
Nicolai Pedersen
Reply

How do you try to add products to this new context cart?

You have to say cartcmd=add&OrderContext={OrderContextID}

 
Scott Forsyth Dynamicweb Employee
Scott Forsyth
Reply

Ni Nicolai,

 

I guess there are three things that I need to keep track of then:

  1. Assign a unique OrderContextID to the cart (I believe I have that)
  2. Make sure that items added to the cart are assigned to the correct OrderContext
  3. When displaying the cart to the user, only display for the custom OrderContext

Is there a way programmatically to set the default for #2 for that user's session so that I don't need to update the templates to pass in the OrderContext? The cart has an OrderVisitorSessionID, so that should keep the cart to just that browser session for that user. And if I populate EcomOrderContextAccessUserRelation (manually if needed) then the cart should be tied to the user. Is seems doable, but I'm just missing some part of it.

 

For #3, can I switch the module programmatically so that for that user only, it's using a different OrderContext?

 

Maybe my issue in testing has been #2 and #3 fighting with each other. I may have added the items to the correct context, but then it appeared to be an empty cart because I was showing the wrong cart context.

 

Am I going about this the right way, or is there a better watch to approach this?

 

Thanks,

 

Scott

 

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Hi Scott

You just have to call OrderContext=id once and DW will remember it for subsequent addtocart etc.

Or by set eCommerce.Common.Context.CartContext = your newly created unique OrderContext.

If you set eCommerce.Common.Context.CartContext it will be saved on the session as well.

BR Nicolai

Votes for this answer: 1
 
Scott Forsyth Dynamicweb Employee
Scott Forsyth
Reply

Hi Nicolai,

 

That makes sense. I had tried to set the CartContext and I see now that you made the setter public just a month ago. I'll point my project to a newer version of the framework and I should be able to do that.

 

I'm doing all of this from backend code so I would prefer to not have to touch the templates or make a page request to call cartcmd and OrderContext. You've set me on the right path. 

 

Thanks!

 

Scott

 
Scott Forsyth Dynamicweb Employee
Scott Forsyth
Reply

Well, I'm a little bit closer, but I'm still not winning. I switched to a newer version of the platform so I can set the CartContext now in code. It seems like it should work, but the cart that is displayed is the generic cart (without the cart context).

 

I manually added an item using cartcmd=add&OrderContext={OrderContextID}. Now in the database I can see that the order (the cart context one) has an orderline. So it added correctly to the right cart.

 

And when I write the following log from code, it shows the new cart context ID:

Log($"Cart Context: {Dynamicweb.eCommerce.Common.Context.CartContext.ID}");

 

However, the cart still displays the general items and not the items for the cart context.

 

What updates do I need to make to the cart to have it use the selected context rather than the generic cart?

 

Thanks,

 

Scott

 

 
Nicolai Pedersen
Reply

Hi Scott

Glad you are getting closer!

If Common.Context.CartContext has a valid value, you should get the cart from that context when you go to the cart page.

On the paragraph settings, make sure no shop or context has been set. You can set a shop, but then you have to make sure your context created cart is in the same shop...

BR Nicolai

Capture.PNG

 

You must be logged in to post in the forum