简体   繁体   English

NopCommerce 4.5:计算购物车总数以创建基于客户角色的折扣

[英]NopCommerce 4.5: Calculating shopping cart total for creating customer role based discount

I'm using NopCommerce v 4.5.3.我正在使用 NopCommerce v 4.5.3。 I need to build a plugin that can create discounts to configure free shipping for certain roles for purchases beyond specified rates.我需要构建一个可以创建折扣的插件,以便为某些角色配置超出指定费率的购买免费送货。 Like: Free shipping above 100$ for Role A, 200$ for role B and so on.比如:角色 A 满 100 美元免运费,角色 B 满 200 美元,依此类推。

It's to be done by creating a new discount, assigning to free shipping as discount type, giving 100% discount (for free shipping).这是通过创建一个新的折扣,分配给免费送货作为折扣类型,给予 100% 折扣(免费送货)来完成的。 And in the requirements, by setting the first requirement group as Required customer roles as role and by setting the second requirement group as Customer spend must be X (this is the one I am building)在需求中,通过将第一个需求组设置为 Required customer roles as role 并将第二个需求组设置为 Customer spend must be X(这是我正在构建的)

There's a free plugin that sort of matches my requirement. 有一个符合我要求的免费插件。

It calculates total orders made by the customer (the past orders) from the orders table and then lets the user set free shipping for customer.它从订单表中计算客户的总订单(过去的订单),然后让用户为客户设置免费送货。 The logic used here for it is:这里使用的逻辑是:

var orders = await _orderService.SearchOrdersAsync(request.Store.Id,
                customerId: request.Customer.Id,
                osIds: new List<int> { (int)OrderStatus.Complete });
            var spentAmount = orders.Sum(o => o.OrderTotal);
                if (spentAmount > spentAmountRequirement)

In this case, they could do it quick as they just needed to sum values in one table by using the SearchOrdersAsync() method.在这种情况下,他们可以快速完成,因为他们只需要使用 SearchOrdersAsync() 方法对一个表中的值求和。

But for the cart, I need to first filter the shopping cart table by using the customer ID, then taking the required productIDs from the shoppingcart and then summing up its price from the product table.但是对于购物车,我需要先使用客户 ID 过滤购物车表,然后从购物车中获取所需的产品 ID,然后从产品表中汇总其价格。

The query for it:对它的查询:

select sum(Price) from dbo.Product
where id in (
select ProductId
from dbo.ShoppingCartItem
where CustomerId = 2047)
--Result:1800

It's only after I get the Product IDs, that I can go to the Product table and filter them to sum up their Prices.只有在获得产品 ID 后,我才能将 go 转到产品表并过滤它们以汇总它们的价格。

I've tried using the GetShoppingCartAsync() method.我试过使用 GetShoppingCartAsync() 方法。 But I'm unable to use any lists here to store the multiple products the customer may have in cart as there's no list in the definition of GetShoppingCartAsync() while the SearchOrdersAsync() for orders has a list called osIds.但是我无法在此处使用任何列表来存储客户可能在购物车中拥有的多个产品,因为 GetShoppingCartAsync() 的定义中没有列表,而订单的 SearchOrdersAsync() 有一个名为 osIds 的列表。

GetShoppingCartAsync() method returns IList<ShoppingCartItem> and the ShoppingCartItem entity contains the ProductId of a cart item. GetShoppingCartAsync()方法返回IList<ShoppingCartItem>并且ShoppingCartItem实体包含购物车商品的ProductId Since nopCommerce doesn't give the functionality to filter cart items by ProductIds and you need the original price of the Products, you can do the following:由于 nopCommerce 没有提供按 ProductIds 过滤购物车项目的功能,而您需要产品的原价,您可以执行以下操作:

Get the all the ProductIds first首先获取所有的 ProductIds

var productIds = (await _shoppingCartService.GetShoppingCartAsync(customer, ShoppingCartType.ShoppingCart, storeId: store.Id)).Select(sc => sc.ProductId);

Sum the price of the products总结产品的价格

var productPrice = (await _productService.GetProductsByIdsAsync(pIds.ToArray())).Sum(p => p.Price);

The productPrice will return the Total sum of the Products that the customer has in their cart. productPrice将返回客户购物车中产品的总和。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM