Developer forum

Forum » Feature requests » Information about quantity in cart in ProductViewModel

Information about quantity in cart in ProductViewModel

Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi guys,

We often receive request to display the quantity in the cart for products, either in ProductList or in ProductDetail.

It would be helpful to have this information in the ProductViewModel. Or at least a very efficient method for getting this information based on the ProductID.

I am not sure if we already have something like this, hence this Feature request.

Thank you,

Adrian


Replies

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

It cannot be on the productviewmodel as that model is context independent and also used in feeds, webapis and other stuff. So the productviewmodel only contains information related to the product and not information related to users, carts or anything not product information.

But it is relatively simple to do in your template:

@inherits ViewModelTemplate<ProductViewModel>
@using Dynamicweb.Ecommerce.ProductCatalog

@{
 bool isProductInCart = Dynamicweb.Ecommerce.Common.Context.Cart.ProductOrderLines.Any(p => p.ProductId == Model.Id);
 bool isProductVariantInCart = Dynamicweb.Ecommerce.Common.Context.Cart.ProductOrderLines.Any(p => p.ProductId == Model.Id && p.ProductVariantId == Model.VariantId);
}

But I also think it is an odd behavior - this is not what you see in a regular webshop - for a reason...

Be careful of performance.

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

Hi Nicolai,

It makes sense.

Thank you for the solution.

I am aware of the performance implications. However, in some cases, the users need more awareness on the quantities they order. Therefore they need to know how many products they have in cart and in some cass they add products or increase the orderlines. Not an usual approach, I agree, but it makes sense in some situations.

And we've had this request in the last week on 2 separate projects from different countries.

I also have a related question, (which could be a stupid question). I have also noticed there is a metod for loading the cart into session (CartService.LoadCart()) would this be a more efficient option? Can I read the cart content from the session?

Thank you,

Adrian

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi,

 

Just pinging in here as well... I also don't like this for obvious performance reasons, but we've had our share of customers (mostly B2B) who will also want something that would require knowing how many items are already in the cart; just to say it's not that uncommon of a request.

 

We always custom develop it, and never add it to our "baseline" templates (SwiftRizzo) so that users other than myself or a conscious developer, use it carelessly.

 

Another thing we do as well is to create a custom Macro for Repository queries, so that we can have a list of products excluding the ones already in the cart. We use it to get products the user typically purchases (but obviously don't want to recommend if they already have it there).

 

BR,

Nuno Aguiar

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

The example above is the most efficient and the cart is always loaded if the user is current. So no need to do anything else.

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

Thank you,

I will proceed as indicated.

Thank you,
Adrian