使用MVC / EF总结购物车中的重复订单项

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用MVC / EF总结购物车中的重复订单项相关的知识,希望对你有一定的参考价值。

我目前正在创建一个简单的购物车,根据对表的调用显示行项目。我主要是在这里利用基本的CRUD,所以我正在读表,过滤掉userID并检查是否已经分配了orderID(以避免重新显示购物车中的订购商品)。

一切都很好,所有工作,但出于美观的原因,我想处理相同产品(相同的ProductID,相同的ProductName,但即不同的数量)被汇总在一个位置(订单数量A +订单数量B等)的情况比拥有不同数量的多个订单项。

旁注:我提供订阅以及单个订单(由表中的布尔值区分),所以请不要在此处激怒。

LineItems的模型如下所示:

public class LineItems
{
    public int                                                      ID                                  { get; set; }
    public string                                                   UserID                              { get; set; }
    public int                                                      ProductID                           { get; set; }
    public string                                                   ProductName                         { get; set; }
    //[Range(1, int.MaxValue, ErrorMessage                            = "Naja... Menge 0 lohnt sich nicht zu liefern...")]
    public int                                                      SubscriptionQuantity                { get; set; }
    public string                                                   SubscriptionCadenceCategory         { get; set; }
    public int                                                      SubscriptionCadenceValue            { get; set; }
    public decimal                                                  SubscriptionPrice                   { get; set; }
    public bool                                                     IsSingleOrder                       { get; set; }
    //[Range(1, int.MaxValue, ErrorMessage                            = "Naja... Menge 0 lohnt sich nicht zu liefern...")]
    public int                                                      SingleOrderQuantity                 { get; set; }
    public decimal                                                  SingleOrderPrice                    { get; set; }
    public decimal                                                  TotalPrice                          { get; set; }
    public int                                                      OrderNumber                         { get; set; }
    public DateTime                                                 UpdatedTimeStampUTC                 { get; set; }
    public string                                                   UpdatedLatitude                     { get; set; }
    public string                                                   UpdatedLongitude                    { get; set; }
    public string                                                   UpdatedLocation                     { get; set; }
}

和观点:

<div id="shoppingCart" class="productTableSection row">
        @foreach (var item in Model)
        {
            if (item.IsSingleOrder == false)
            {
                <div id="shoppingCartSubscriptionLineItemArea" class=" tableArea col-xs-offset-1 col-xs-10 col-sm-offset-1 col-sm-10 col-md-offset-1 col-md-10 col-lg-offset-2 col-lg-8">
                    @html.HiddenFor(modelItem => item.ID, new { @class = "tableField col-xs-12 col-sm-12 col-md-12 col-lg-12" })
                    <a id="shoppingCartSubscriptionRemoveLink" class="tableField col-xs-1 col-sm-1 col-md-1 col-lg-1" href="#">
                        <i id="shoppingCartSubscriptionRemove" class="fas fa-trash-alt"></i>
                    </a>
                    <a id="shoppingCartSubscriptionLineItemLink" class="tableField col-xs-10 col-sm-10 col-md-10 col-lg-10" href="@Url.Action("ProductSubscription", "freshNclean", new { id = item.ID })">
                        <div id="shoppingCartSubscriptionProductNameField" class="tableField col-xs-7 col-sm-8 col-md-9 col-lg-9">
                            @Html.DisplayFor(modelItem => item.ProductName)
                        </div>
                        <div id="shoppingCartSubscriptionProductPriceField" class="tableField col-xs-5 col-sm-4 col-md-3 col-lg-3">
                            CHF @Html.DisplayFor(modelItem => item.SubscriptionPrice)
                        </div>
                        <div id="shoppingCartSubscriptionProductQuantityField" class="tableField col-xs-4 col-sm-4 col-md-5 col-lg-5">
                            @Html.DisplayFor(modelItem => item.SubscriptionQuantity) x
                        </div>
                        <div id="shoppingCartSubscriptionCadenceValueField" class="tableField col-xs-3 col-sm-2 col-md-2 col-lg-1">
                            alle @Html.DisplayFor(modelItem => item.SubscriptionCadenceValue)
                        </div>
                        <div id="shoppingCartSubscriptionCadenceCategoryField" class="tableField col-xs-5 col-sm-6 col-md-5 col-lg-6">
                            @Html.DisplayFor(modelItem => item.SubscriptionCadenceCategory)
                        </div>
                        <div id="shoppingCartSubscriptionTotalPriceField" class="tableField col-xs-12 col-sm-12 col-md-12 col-lg-12">
                            CHF @Html.DisplayFor(modelItem => item.TotalPrice)
                        </div>
                    </a>
                    <a id="shoppingCartSubscriptionEditLink" class="tableField col-xs-1 col-sm-1 col-md-1 col-lg-1" href="#">
                        <i id="shoppingCartSubscriptionEdit" class="fas fa-edit"></i>
                    </a>
                </div>
            }
            else
            {
                <div id="shoppingCartSingleOrderLineItemArea" class=" tableArea col-xs-offset-1 col-xs-10 col-sm-offset-1 col-sm-10 col-md-offset-1 col-md-10 col-lg-offset-2 col-lg-8">
                    @Html.HiddenFor(modelItem => item.ID, new { @class = "tableField col-xs-12 col-sm-12 col-md-12 col-lg-12" })
                    <a id="shoppingCartSingleOrderRemoveLink" class="tableField col-xs-1 col-sm-1 col-md-1 col-lg-1" href="#">
                        <i id="shoppingCartSingleOrderRemove" class="fas fa-trash-alt"></i>
                    </a>
                    <a id="shoppingCartSingleOrderLineItemLink" class="tableField col-xs-10 col-sm-10 col-md-10 col-lg-10" href="@Url.Action("ProductSingleOrder", "freshNclean", new { id = item.ID })">
                        <div id="shoppingCartSingleOrderProductNameField" class="tableField col-xs-7 col-sm-8 col-md-9 col-lg-9">
                            @Html.DisplayFor(modelItem => item.ProductName)
                        </div>
                        <div id="shoppingCartSingleOrderProductPriceField" class="tableField col-xs-5 col-sm-4 col-md-3 col-lg-3">
                            CHF @Html.DisplayFor(modelItem => item.SingleOrderPrice)
                        </div>
                        <div id="shoppingCartSingleOrderProductQuantityField" class="tableField col-xs-12 col-sm-12 col-md-12 col-lg-12">
                            @Html.DisplayFor(modelItem => item.SingleOrderQuantity) x
                        </div>
                        <div id="shoppingCartSingleOrderTotalPriceField" class="tableField col-xs-12 col-sm-12 col-md-12 col-lg-12">
                            CHF @Html.DisplayFor(modelItem => item.TotalPrice)
                        </div>
                    </a>
                    <a id="shoppingCartSingleOrderEditLink" class="tableField col-xs-1 col-sm-1 col-md-1 col-lg-1" href="#">
                        <i id="shoppingCartSingleOrderEdit" class="fas fa-edit"></i>
                    </a>
                </div>
            }
        }
    </div>

最后控制者:

// GET: /freshNclean/ShoppingCart
    [Authorize]
    public ActionResult ShoppingCart(Orders model)
    {
        // define variables
        var userID                                                  = User.Identity.GetUserId();
        DateTime nowUTC                                             = DateTime.Now.ToUniversalTime();
        DateTime nowLocal                                           = DateTime.Now.ToLocalTime();                                           
        // track user activity: get method is restricted to activity name and timestamp
        var LOADED                                                  = new UserActivities
        {
            UserID                                                  = userID,
            ActivityName                                            = "ShoppingCart_Loaded",
            ActivityTimeStampUTC                                    = nowUTC,
            ActivityLatitude                                        = "n/a",
            ActivityLongitude                                       = "n/a",
            ActivityLocation                                        = "n/a"
        };
        DATADB.UserActivityList.Add(LOADED);
        DATADB.SaveChanges();
        return View(DATADB.LineItemList.Where(x => x.UserID == userID).Where(x => x.OrderNumber == 0).ToList());
    }
答案

我终于弄明白了 - 不是真的。我所做的是改变了写入购物车的方法。但最终结果是有效的。

以上是关于使用MVC / EF总结购物车中的重复订单项的主要内容,如果未能解决你的问题,请参考以下文章

禁用 Woocommerce 购物车订单项数量价格计算

如何从 Woocommerce 中的订单项中获取产品 sku [重复]

如何从 WooCommerce 中的订单项中获取产品类别 ID

购物车的设计与实现

订单以及相关周边表设计思路(经过公司多年的经验总结);

Oracle SQL 中的最后一个订单项