我正在創建一個電子商務產品購物車,在添加第一個訂單時,它使用 createOrder 創建訂單,并在添加下一個產品時運行 updateCart。我必須在運行更新購物車時發送購物車中的先前專案,因為目前在添加下一個產品時它不起作用。
這是我的服務代碼:
static createOrder(
order: Order,
onSuccess: Function,
one rror: Function,
onFinal: Function
) {
const orderJson = serialize(Order, order)
const URL = ApiRoutes.CREATE_ORDER
axiosInstance
.post(URL, { "order": orderJson })
.then((res) => {
// const data = deserialize(Cart, res.data.order);
//onSuccess(res.data.success);
})
.catch((err) => {
onError(err);
});
}
static updateCart(
order: Order,
orderId: string,
onSuccess: Function,
one rror: Function,
onFinal: Function
) {
const orderJson = serialize(Order, order)
const URL = ApiRoutes.UPDATE_CART.replace(":id", `${orderId}`)
axiosInstance
.put(URL, { "order": orderJson })
.then((res) => {
// const data = deserialize(Cart, res.data.order);
//onSuccess(res.data.success);
})
.catch((err) => {
onError(err);
});
}
這就是我所說的:
const addToBasket = () => {
dispatch({
type: 'ADD_TO_BASKET',
item: {
"id": product.id,
"title": product.name,
"price": product.price,
"image": product.imageUrl,
},
});
// CREATE ORDER ONCE CART IS EMPTY ELSE UPDATE CART
if (props?.userCart?.orderItems?.length) {
OrderService.updateCart(
{
orderItems: [{
productId: `${product?.id}`,
quantity: 1,
// taxMode:0,
// taxPercentage:0,
//discountPercentage: ""
}]
},
props?.userCart?.id,
() => { },
() => { },
() => { },
)
} else {
OrderService.createOrder(
{
userId: user?.id,
storeId: store?.id,
orderItems: [
{
productId: `${product?.id}`,
quantity: 1,
taxMode: 0,
taxPercentage: 0,
discountPercentage: ""
}
]
},
() => { },
() => { },
() => { },
)
}
};
uj5u.com熱心網友回復:
只需使用擴展運算子將以前的產品添加到陣列的末尾 ...props.userCart.orderItems
OrderService.updateCart(
{
orderItems: [{
productId: `${product?.id}`,
quantity: 1,
// taxMode:0,
// taxPercentage:0,
//discountPercentage: ""
}, ...props.userCart.orderItems]
},
props?.userCart?.id,
() => { },
() => { },
() => { },
)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/414459.html
標籤:
