• fiazahmed

Member

36 Points

126 Posts

### calculate total price for a particular shop in cart and apply discount based on orderlimit for th...

deat all,

i have a multi-vendor e-commerce website in which users shop from different shops .we have to apply shopdiscount if total price of items  for a particular shop is >=orderlimit and duration =" started". Repeat this if the cart contains items from more than one shop.

the cart model is

namespace myapp.Models
public class Cart
{
[Key]
public int RecordId { get; set; }
public string CartId { get; set; }
public Guid ItemId { get; set; }
public Guid StoreId { get; set; }

public Decimal? ShopDiscount { get; set; }   // shop  deal discount
public long ShopId { get; set; }
[Display(Name = "Discount Time")]
public string Duration { get; set; }        // the time of shop deal
[StringLength(100, ErrorMessage = "Must be less than 100 characters")]
public string StoreName { get; set; }
public decimal? Giftback { get; set; }
[Display(Name = "Min order For Disc")]
public Decimal? OrderLimit { get; set; }    // Minimum order to apply shopdiscount
public int Count { get; set; }
public decimal? Discount { get; set; }   // Product discount
public System.DateTime DateCreated { get; set; }
public virtual Item Item { get; set; }
}
}

here is the shop deal function i tried but it returns always 0.

`````` public decimal? ShopDeal()
{
decimal? shopdeal = 0;
//get record with different shops so that calculate shop total
//separatly
var results = db.Carts.Select(m => new { m.CartId, m.StoreId,
m.OrderLimit, m.ShopDiscount, m.Duration }).Distinct().ToList();       // i think here is problem it does not return different shops

// calculate total price of all items for a particular shop
foreach (var item in results)
{
decimal? shoptotal = (from cartItems in db.Carts
where cartItems.CartId == ShoppingCartId
&& cartItems.Item.StoreId ==
item.StoreId
select
(decimal?)cartItems.Item.Price).Sum();
if (shoptotal >= item.OrderLimit && item.Duration ==
"Started")
{
shopdeal = shopdeal + shoptotal * item.ShopDiscount / 100;
}

}

return shopdeal;
}``````