Developer forum

Forum » Ecommerce - Standard features » Get Cart/Order by Name

Get Cart/Order by Name

Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi guys,

I have a situation where I need to create a cart and set it active in order to start adding to it immediately after creation.

I could create a cart using the cartcmd=createnew property and a CartName.
I now want to set the cart active, but I need to know the ID of the cart I have just created.

I could not find any obvious method that would allow me to get a Cart or Order object based on the Name. I see that the value of CartName is saved in OrderDisplayName.

Can anyone suggest a way to get this CartID based on the name?

If I use Dynamicweb.Ecommerce.Services.Orders.GetCustomerUnclosedOrders I only get the active cart

Thank you,

Adrian


Replies

 
Shiwanka Chathuranga Dynamicweb Employee
Shiwanka Chathuranga
Reply

Does this help?

var ActiveCart=Dynamicweb.Ecommerce.Common.Context.Cart;

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Shiwanka,

Thank you for the response.
The newly created cart is not returned by the Dynamicweb.Ecommerce.Common.Context.Cart.

You have to set the cart to the user and then it is available in the Common.Context.

I have used this for setting it up: Dynamicweb.Ecommerce.Common.Context.SetCart(newCart);

The missing piece is getting an Order object based on the CartName.

For now, I have used a direct SQL request to the Orders table but I am sure there should be better options. Unfortunately, I could not find a suitable one after trying various methods for a couple of hours.

I hope that someone will suggest a better option.

Thank you,
Adrian

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

You can do like this - to get the latest cart of the user - you can also return additional order objects.:

var filter = new OrderSearchFilter();
            filter.IsCart = true;
            filter.Completed = OrderSearchFilter.CompletedStates.NotCompleted;
            filter.PageNumber = 1;
            filter.PageSize = 1;
            filter.CustomerId = userId;
            filter.OrderBy = $"OrderDate DESC";
 foreach (var order in Services.Orders.GetOrdersBySearch(filter).ResultOrders){

var name = order.DisplayName;

}

After the cart has been retrieved, you can set it as active using the CartService.

BR Nicolai

Votes for this answer: 1
 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

Thank you for the response.

I have tried using the CartService but I could not use it.

I have tried Dynamicweb.Ecommerce.Services.Carts.SetUserCart(Order, User, Boolean) but I get a message saying that Services does not contain Carts.

Could it be Internal/Private? My solution runs on 9.13.13.

I have ended up using Dynamicweb.Ecommerce.Common.Context.SetCart(newCart) where newCart is an Order object.

Thank you,

Adrian

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Services.Cart is internal - so that is why.

But you can do new CartService(); or context as you do now.

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Thank you

Adrian

 

You must be logged in to post in the forum