Developer forum

Forum » Development » Save cart on ApiController

Save cart on ApiController

Chris Søgaard
Chris Søgaard
Reply

Hi

I'm trying to create a new cart in backend and applying this cart to the current context. I have created the logic in an MVC Controller inheriting from ApiController to use this as an API method in the frontend. My logic fails in the method Dynamicweb.Ecommerce.Orders.ShippingCalculator.CheckShop(Fee fee) propably due to the fact that PageView.Current() would be null in a situation like this. 

This is how I have implemented the code marked with the line where the exception is thrown:

Any ideas how to overcome this. The exception is also thrown, if I try to save the order/cart further down, so order service seems to rely on something that is not present in this context. Normal add to cart works fine on this solution.

Hope to find some good ideas here.

BR Chris 


Replies

 
Nicolai Pedersen
Reply

Hi Chris

A controller is what we refer to as context free - i.e. no session and hence no user etc.

So in order for this controller to interact with context you need to enable sessions for the controller - you need to call something like this for api requests in globalasax:

HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);

See https://docs.microsoft.com/en-us/dotnet/api/system.web.httpcontext.setsessionstatebehavior?view=netframework-4.8

 
Chris Søgaard
Chris Søgaard
Reply

Hi Nicolai

Thank you for your quick response.

I did set session behaviour for this request, and session is also there. I can get user from GetCurrentExtranetUser method and I can also get Cart (if there is a cart) and other information from Dynamicweb.Ecommerce.Common.Context.

Also if I implement a custom shipping provider in the code which would return a static fee, then my code does not fail.

So it seems that this is tied to the code that would get the shipping fee from shipping fee setup in the backend.

BR Chris

 
Nicolai Pedersen
Reply

Hi Chris

Yes, you are right. I can see that.

There is a fee added to that order - and if it has a shopid and the shipping calculate tries to match that on the website shopid. That is wrong - it should use the order shopid. Those 2 properties always has the same value, but here it fails because of the lack of pageview.

The solution to this issue is of course to change the shipping calculator to use the order shopid to validate the fee. I have just changed that and is commiting that to head now.

What version are you on? 9.10 or 9.12?

BR Nicolai

 
Nicolai Pedersen
Reply

Devops#5774

 
Chris Søgaard
Chris Søgaard
Reply

That was quick 😊

We're on 9.12.2

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Great.

Next Dynamicweb.Ecommerce package should hold this change. If qa approves it :-). We are little understaffed today, so maybe not until Monday.

Attached a pirate version of the build for you to test.

BR Nicolai

Votes for this answer: 1

 

You must be logged in to post in the forum