简体   繁体   English

javascript对象中的购物车,带有json对象数量的商品数组

[英]Shopping cart in javascript object, with an array of json object quantity of items

I'm facing some problems with some shopping cart in javascript/json and ASP MVC 我在javascript / json和ASP MVC中遇到一些购物车问题

Scenario: The main function is to order products but is not a normal shopping cart for 1 user, it can be for 2 users, 3 users, 1 user etc, but ONLY 1 shopping cart (javascript object). 场景:主要功能是订购产品,但不是1位用户的普通购物车,它可以是2位用户,3位用户,1位用户等, 但只能订购1个购物车 (javascript对象)。 Thats something that depends for specific Buyer . 那取决于特定的购买者 So the main problem is that if the Buyer buy a "cake" for user1, and then wants to buy somethin to user2 with different date and same "cake", the product will be "2 cakes" but for the second user, and the first user lost the "cake". 因此,主要问题是,如果买方为用户1购买了一个“蛋糕”,然后又想用不同的日期和相同的“蛋糕”向用户2购买某物,那么该产品将是“ 2个蛋糕”,但对于第二个用户来说,第一个用户丢失了“蛋糕”。

I need to make the cake user specific and date specific too. 我也需要特定用户和日期。 So if Buyer buy a cake for user1 for 2014/05/01 (using a datepicker for example), and then buy a cake for user2 for 2014/05/02 (using a datepicker for example), the quantity of the product will be 1 for every user and not 2 cakes with the quantity in the array. 因此,如果买方为2014年1月1日的用户1购买蛋糕(例如,使用日期选择器),然后为2014年2月2日的用户2购买蛋糕(例如,使用日期选择器),则产品的数量为每个用户1个蛋糕,而不是2个蛋糕与数组中的蛋糕数量。

Actually I'm using a javascript object for the shopping cart like this 实际上,我正在将javascript对象用于购物车,就像这样

var ShoppingCart = {
    BuyerID: 1, //I will handle this later
    StoreID: 1,
    Total: 0,
    ShoppingCartItems: [] //array with json objects
};

Then I'm using a function to push to the array the elements I need on a button 然后我使用一个函数将按钮上需要的元素推送到数组

function AddItem (ProductID,ProductName, Description, Price, Quantity, StoreID){
       var UserID = $("#hiddenUserID").val(); //e.g. = 1
       var ProductDate = $("#hiddenDateField").val(); //e.g. = 2014/05/01
         ShoppingCartItems.push({
            "ProductID": ProductoID,
            "ProductName": ProductName,
            "Description": Description,
            "Price": Price,
            "Quantity": Quantity,
            "StoreID": StoreID,
            "UserID": UserID, //this will change later
            "ProductDate": ProductDate //this will change if the buyer choose another day
        })
}

This function works everytime the Buyer clicks on a button. 每当买方单击按钮时,此功能就起作用。 The products are loaded with ajax, that's not the problem. 产品装载了ajax,这不是问题。 After the user select from products a new array will be created with the product selected and all the specifications I use. 用户从产品中选择之后,将使用所选产品和我使用的所有规格创建一个新的阵列。 So I have 2 problems 所以我有两个问题

If the Buyer choose for example 1 cake, but he forgot he needed 2 cakes, if he click again the 2nd cake the array will push a new element or another product with quantity of 1 例如,如果买方选择1个蛋糕,但他忘记了需要2个蛋糕,那么如果他再次单击第二个蛋糕,则阵列将推入数量为1的新元素或其他产品

  • 1 Cake (for userID = 1, and date 2014/05/01) 1个蛋糕(对于userID = 1,日期为2014/05/01)
  • 1 Cake (for userID = 1, and dae 2014/05/01) 1个蛋糕(对于userID = 1,和dae 2014/05/01)

I want if the product with the same ProductID, userID and date, update the quantity to 2 cakes because the second problems originates from the first one, I need to update quantity by the ProductID, userID and date because if the Buyer is planning to buy a cake for every user, the product needs to be separated, for example this way it's wrong: 我想如果具有相同ProductID,userID和日期的产品将数量更新为2个蛋糕,因为第二个问题源自第一个蛋糕,我需要通过ProductID,userID和日期来更新数量,因为如果买方打算购买对于每个用户来说都是一个蛋糕,需要将产品分开,例如这种方式是错误的:


Products for userID = 1 userID = 1的产品

  • 1 cake (for userID = 1, date 2014/05/01) 1个蛋糕(用于userID = 1,日期2014/05/01)
  • 1 cake (for userID = 1, date 2014/05/02) 1个蛋糕(用于userID = 1,日期2014/05/02)
  • 1 cake (for userID = 1, date 2014/05/02) 1个蛋糕(用于userID = 1,日期2014/05/02)

Products for userID = 2 userID = 2的产品

  • 1 cake (for userID = 2, date 2014/05/01) 1个蛋糕(用于userID = 2,日期2014/05/01)

I want the array to have the quantity separated for productID, userID and date so each product need to have a "link" between the userID, and the date to have something like this: 我希望数组将产品ID,用户ID和日期的数量分开,因此每个产品都需要在用户ID和日期之间有一个“链接”,以便具有以下内容:

Products for userID = 1 userID = 1的产品

  • 1 cake (for userID = 1, date 2014/05/01) 1个蛋糕(用于userID = 1,日期2014/05/01)
  • 2 cake (for userID = 1, date 2014/05/02) 2个蛋糕(用于userID = 1,日期2014/05/02)

Products for userID = 2 userID = 2的产品

  • 1 cake (for userID = 2, date 2014/05/01) 1个蛋糕(用于userID = 2,日期2014/05/01)

So If the user1 and user 2 have the same product for the same date, I need a validation to separate the quantity for each product by userID and DATE, that's the big problem. 因此,如果user1和user 2在同一日期具有相同的产品,则需要验证以按userID和DATE分隔每种产品的数量,这是个大问题。


Actually Im using MVC so this way is the best way to send with json and ajax to a specific model (for modelstate.isvalid). 实际上,我使用MVC,因此这种方式是将json和ajax发送到特定模型的最佳方法(对于modelstate.isvalid)。 So this code is "tested" with the things I need for correct data to the database. 因此,此代码已通过“测试”了我需要的正确数据到数据库的内容。 Hope someone can help me with this. 希望有人可以帮助我。

Thanks in advance 提前致谢

When you push a new item to the cart you need to compare it's properties to the objects in the cart and if the product id, user id, and date match, then you want to add the new quantity to the old quantity of the old object in the array instead of pushing a new one. 将新项目推入购物车时,需要将其属性与购物车中的对象进行比较,如果产品ID,用户ID和日期匹配,则要将新数量添加到旧对象的旧数量中而不是推一个新的。 Alternatively you could post process the cart array after an item is added and merge duplicates while adding their quantities. 或者,您可以在添加项目后对购物车数组进行后处理,并在添加数量的同时合并重复项。 I'm not sure which method is better. 我不确定哪种方法更好。

function AddItem (ProductID,ProductName, Description, Price, Quantity, StoreID) {
  var merged = false;
  var UserID = $("#hiddenUserID").val(); //e.g. = 1
  var ProductDate = $("#hiddenDateField").val(); //e.g. = 2014/05/01
  var newItemObj = {
    "ProductID": ProductID,
    "ProductName": ProductName,
    "Description": Description,
    "Price": Price,
    "Quantity": Quantity,
    "StoreID": StoreID,
    "UserID": UserID,
    "ProductDate": ProductDate
  };
  // loop through items in cart
  for ( var i = 0; i < ShoppingCartItems.length; i++ ) {

    // if product ID, user ID, and date are the same
    if ( ShoppingCartItems[i].ProductID   == newItemObj.ProductID &&
         ShoppingCartItems[i].UserID      == newItemObj.UserID &&
         ShoppingCartItems[i].ProductDate == newItemObj.ProductDate ) {

      // add the quantity of the new obj to the old one
      ShoppingCartItems[i].Quantity += newItemObj.Quantity;

      // if two items are merged, set a flag
      merged = true;
    }
  };
  // if no merge happened, just add the item normally
  if ( !merged ) {
    ShoppingCartItems.push( newItemObj );
  }
}

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

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