Developer forum

Forum » Development » Productquantity via API

Productquantity via API


Reply
How do I get the quantity of a product that's currently in the cart, using the ProductID and VariantID in my usercontrol (with the Ecommerce API)?

Replies

 
Reply
Hi Maurits,

The cart is stored in the Session (as an Order) so I think the quickest way to do it is with a bit of Linq against the Order object:

Order order = Session["EcomCart"] as Order;
if (order != null)
{
  string productId = "PROD22"; // Your ID here
  string variantId = "VO1"; // Your ID here
  OrderLine orderLine = (from OrderLine o in order.OrderLines
                        where o.ProductID == productId && o.ProductVariantID == variantId
                        select o).FirstOrDefault();
  if (orderLine != null)
  {
    double quantity = orderLine.Quantity;
  }
}

Hope this helps,

Imar
 
Reply
Thanx for your reply.
I'm afraid I can't use Linq, because my sollution uses Framework 2.0. Could you rewrite the code without the use of Linq?
 
Reply
You can simply loop over the items in order.OrderLines and check the ProductID and ProductVariantID....

Imar

 
Nicolai Høeg Pedersen
Reply
The cart should not be accessed through the session object but like this:

Dynamicweb.eCommerce.Common.

Context.Cart
 
Reply
Ooops, sorry about that. In that case, you need something like this:

Order order = Dynamicweb.eCommerce.Common.Context.Cart;
if (order != null)
{
  OrderLine orderLine = null;
  string productId = "PROD22"; // Your ID here
  string variantId = "VO1"; // Your ID here
  foreach (OrderLine o in order.OrderLines)
  {
    if (o.ProductID == productId && o.ProductVariantID == variantId)
    {
      orderLine = o;
      break;
    }
  }
  if (orderLine != null)
  {
    double quantity = orderLine.Quantity;
  }
}

Imar
 
Reply
Thanx for your answers! It helped a lot. I always have trouble finding the right context for getting data from the API, whether it is the ID of the selected language layer or it's the content of the cart.
The API Reference is still a bit difficult to work with, I couldn't find it in there. But hopefully that will change, when they implement the search functionality and offer some examples.

But for now I am helped and can go on. Thanx for that!
 
Reply
It might be a nice idea to have some global Context object that provides access to other context stuff, so you can do stuff like:

Dynamicweb.Context.eCommerce.Cart

and 

Dynamicweb.Context.User.Email

and

Dynamicweb.Context.Area.CurrentID

and so on. Would make things a little easier to find.....

Imar

 

You must be logged in to post in the forum