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

• ### Question

• User1493762548 posted

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

please help how to calculate shoptotal for different shops and then apply shopdiscount.

regards

fiaz ahmed ranjha

Saturday, August 31, 2019 6:23 AM

### Answers

• User1493762548 posted

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

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Monday, September 9, 2019 5:35 AM

### All replies

• User475983607 posted

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.

Saturday, August 31, 2019 11:52 AM
• User1493762548 posted
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.
Sunday, September 1, 2019 4:23 PM
• User-17257777 posted

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,

Jiadong Meng.

Wednesday, September 4, 2019 3:32 AM
• User1493762548 posted

thanks dear ,

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.

regards and thanks in advance

fiaz ahmed ranjha

Friday, September 6, 2019 10:26 AM
• User475983607 posted

" 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.

regards and thanks in advance

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.

Friday, September 6, 2019 1:18 PM
• User1493762548 posted

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

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Monday, September 9, 2019 5:35 AM