# calculate total price for a particular shop in cart and apply discount based on orderlimit for that shop .[Answered] RSS

## 6 replies

Last post Sep 09, 2019 05:35 AM by fiazahmed

• fiazahmed

Member

36 Points

125 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;
}``````

regards

fiaz ahmed ranjha

• mgebhard

All-Star

49871 Points

22160 Posts

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

There are a lot of problems with the code shown.  The first query fetches IDs form the entire Carts table.  Then it uses the same IDs to filter the same Carts table.  This is redundant. The shoptotal variable in the foreach loop always contains the calculation from the last item in the foreach loop.

We can't see the table design and I assume there are issue there as well.

• fiazahmed

Member

36 Points

125 Posts

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

thanks dear ,please how to work around .please look at the model of cart all the enteries in the cart table are as expected.i think we need to reshape query."calculate shoptotal for those items in the cart which belongs to a particular shop than apply shopdiscount if shoptotal greater or equal to orderlimit"repeat if the cart items belong to more than one shop.
regards
fiaz ahmed ranjha.

Participant

1310 Points

491 Posts

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

Hi fiazahmed,

From your code, I see that you only calculated the "shopdeal" when shoptotal >= item.OrderLimt is true, but when the condition is false, you didn't. Therefore, you should add the following code when it is false.

`shopdeal = shopdeal + shoptotal;`

Best Regards,

MSDN Community Support
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com
• fiazahmed

Member

36 Points

125 Posts

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

thanks dear ,

#### mgebhard

The first query fetches IDs form the entire Carts table.Then it uses the same IDs to filter the same Carts table.

" the purpose of first query is to fetch shopId of shops from the cart table , so that we can apply shopdeal for those shops in next query" . any suggestion to work around.

fiaz ahmed ranjha

• mgebhard

All-Star

49871 Points

22160 Posts

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

#### fiazahmed

" the purpose of first query is to fetch shopId of shops from the cart table , so that we can apply shopdeal for those shops in next query" . any suggestion to work around.

But the first query fetches every single ShopId in the Carts table - there's no filter.   Next, the code loops over the results of first query and uses the values to filter the same Carts table.

Finally the shoptotal variable contains the calculation results of the last item in the foreach loop.

The code make no logical sense and I'm pretty sure the database design has issues too.  I'm not sure how to help you.

• fiazahmed

Member

36 Points

125 Posts

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

thanks dear ,

after lot of efforts, following worked for me as expected,

and implemented successfully in :https://smartbook.pk/Items/DiscountItems

```     public decimal? ShopDeal()
{
decimal? shopdeal = 0;

var results = (from cartItems in db.Carts
where cartItems.CartId == ShoppingCartId
select new { cartItems.StoreId,cartItems.OrderLimit,cartItems.Duration,cartItems.ShopDiscount }).Distinct().ToList();
//db.Carts.Select(m => new { m.StoreId, m.OrderLimit, m.ShopDiscount, m.Giftback, m.Duration }.where()).Distinct().ToList();

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;
}
```

regards

fiaz ahmed ranjha