简体   繁体   English

如何计算购物车产品运费

[英]How to calculate shopping cart product shipping

I have products in a cart from different sellers with different shipping prices.我的购物车中有来自不同卖家的不同运费的产品。

what would be the way to calculate the shipping?计算运费的方法是什么?

This is what I have so far: I initialize the variable shipping:到目前为止,这就是我所拥有的:我初始化了变量运输:

public shipping: number;

Then I do a foreach for the shopping cart然后我为购物车做了一个foreach

  this.cart.forEach(item => {
        this.shipping += item.product.shippingCost;
    });

The above is giving me shipping to be 'NAN'.以上是让我航运成为“南”。

What would be the way to do this, considering if products are from different sellers.考虑到产品是否来自不同的卖家,有什么方法可以做到这一点。

I am not sure of the real way to go about this.我不确定真正的方法来解决这个问题。

For example, If different kind of goods (product 1: a book, product 2: a car), are in the cart with different shipping requirements but from the same seller.例如,如果不同种类的商品(产品 1:一本书,产品 2:汽车),在具有不同运输要求但来自同一卖家的购物车中。 Do I have to calculate the shipping for such product differently?我是否必须以不同方式计算此类产品的运费? Considering this same seller can just bundle the car and the book to one package.考虑到同一卖家可以将汽车和书捆绑到一个包裹中。

What should be the way to calculate shipping for such a client listing site sale shopping cart?对于这样的客户列表网站销售购物车,计算运费的方法应该是什么?

在此处输入图片说明

Initialize "this.shipping" with 0 before adding values in it.在添加值之前用 0 初始化“this.shipping”。

Edit1: before this loop, use this statement. Edit1:在这个循环之前,使用这个语句。

this.shipping = 0

You can use map and reduce to avoid initialize value您可以使用mapreduce来避免初始化值

 this.cart
.map(item => Number(item.product.shippingCost))
.reduce((prev, next) => prev + next);

if you have seller id in this object then you can filter by seller id如果您在此对象中有卖家 ID,那么您可以按卖家 ID 过滤

 this.cart
.filter(item => item.seller_id === SELLER_ID)
.map(item => Number(item.product.shippingCost))
.reduce((prev, next) => prev + next);

Note : SELLER_ID is your specific seller id注意: SELLER_ID是您的特定卖家 ID

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

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