简体   繁体   English

如何在AngularJS中添加具有不同规格的相同产品

[英]How to add same product with different specifications in AngularJS

I have a list of products and a shopping cart. 我有产品清单和购物车。 Let's say I have a product something like this: 假设我有一个类似这样的产品:

   {
     "ProductId" : "1",
     "ProductName" : "Knee Caps",
     "Specifications" : {
           "Size" : ["S","M","L","XL","XXL"],
           "GrossWeight" : "200gm each",
           "Colour" : ["Beige","Grey"]
          },
     "SellingPrice" : 80
}

What I did so far is this: 到目前为止,我所做的是:

if(localStorage.getItem("productTable") != null)
{
  productTable = JSON.parse(localStorage["productTable"]);
  productTable[Id]={ProductId : Id, Colour : selectedColour, Size : selectedSize, QuantityAdded : selectedQuantity};
 }
 else {
   productTable[Id]={ProductId : Id, Colour : selectedColour, Size : selectedSize, QuantityAdded : selectedQuantity};                    
 }

What this code is doing is, it add's the product on the first time but if i want the same product with different Sizes and Color's , it replaces the original one. 该代码的作用是,它是第一次添加产品,但是如果我想要具有不同尺寸颜色的相同产品,它将替换原始产品。 Now I know this approach is not completely correct. 现在我知道这种方法并不完全正确。 Can someone please help me to figure out how can i add same product with different specifications? 有人可以帮我弄清楚如何添加具有不同规格的同一产品吗?

From what I understood, you wanted to keep Specifications property if it exists. 据我了解,您想保留“ Specifications属性(如果存在)。 I hope this will work. 我希望这会起作用。 Feel free to let me know in case you get some error. 如果您遇到任何错误,请随时告诉我。 Hopefully, you can get the clue aboout what I am doing 希望你能了解我在做什么

if(localStorage.getItem("productTable") != null)
{
  productTable = JSON.parse(localStorage["productTable"]);
  productTable[Id]={
      ProductId : Id,
      Colour : selectedColour, 
      Size : selectedSize,
      Specifications : checkSpecification(productTable[Id].Specifications,size,weight,color)
      QuantityAdded : selectedQuantity
  };
}
 else {
   productTable[Id]={ProductId : Id, Colour : selectedColour, Size : selectedSize, QuantityAdded : selectedQuantity};                    
}

function checkSpecification(obj,size,weight,color){
  if(obj){
     obj['Size'].push(size);
     obj['GrossWeight'] = weight || '';
     obj['Colour'].push(color);

     return obj;
  } 
}

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

相关问题 如何将不同尺寸的相同产品作为单独的商品添加到购物车中? - How to add the same product with different sizes to the shopping cart as a separate item? 将不同尺寸的相同产品添加到购物车 - Add same product with different sizes to the cart 将不同颜色和价格的相同产品添加到购物车 - Add same product with different color and price to the cart 添加到购物车后如何留在同一产品上? - How to stay on Same Product after Add to Cart? 如何添加具有一个 id 但具有不同参数的产品? - How to add a product with one id but with different parameters? AngularJs:如何在不同的域上设置相同的cookie - AngularJs: How to set same cookie on different domains 如何使用AngularJS添加不同的背景色 - How to add different background colour using AngularJS 反应:在同一产品页面上添加另一个具有不同属性的产品后,添加的产品更改了它的属性 - React: added product changes it's attributes after I add another product with different attribute while on the same product page 不再在桌上添加相同的产品 - not add same product on table again AngularJS,具有不同模板但具有相同控制器的多个路由怎么样? - AngularJS, How about multiple routes with different templates but the same controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM