feat: 商品添加克重字段,订单添加通道费,运费按重量计算
- 商品模型: 添加weight字段(克重) - 订单模型: 添加channel_fee字段(支付通道费) - 后台商品管理: 添加克重输入框 - 后台订单详情: 显示通道费 - 订单创建: 根据商品重量计算运费,添加通道费计算 - 购物车: 根据商品重量计算运费,显示通道费 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -243,6 +243,7 @@ func (h *OrderHandler) Create(c *gin.Context) {
|
||||
|
||||
var subtotal float64
|
||||
var totalQuantity int
|
||||
var totalWeight float64
|
||||
var orderItems []models.OrderItem
|
||||
supplierMap := make(map[uint]bool)
|
||||
|
||||
@@ -280,6 +281,7 @@ func (h *OrderHandler) Create(c *gin.Context) {
|
||||
|
||||
subtotal += cart.Product.Price * float64(cart.Quantity)
|
||||
totalQuantity += cart.Quantity
|
||||
totalWeight += cart.Product.Weight * float64(cart.Quantity)
|
||||
orderItems = append(orderItems, models.OrderItem{
|
||||
ProductID: cart.ProductID,
|
||||
Quantity: cart.Quantity,
|
||||
@@ -298,7 +300,7 @@ func (h *OrderHandler) Create(c *gin.Context) {
|
||||
break
|
||||
}
|
||||
|
||||
var shippingFeeFirstWeight, shippingFeePerGram, serviceFeeRate, taxRate float64
|
||||
var shippingFeeFirstWeight, shippingFeePerGram, serviceFeeRate, taxRate, channelFeeRate float64
|
||||
var setting models.SystemSetting
|
||||
if err := utils.DB.Where("`key` = ?", "shipping_fee_first_weight").First(&setting).Error; err == nil {
|
||||
shippingFeeFirstWeight, _ = strconv.ParseFloat(setting.Value, 64)
|
||||
@@ -312,17 +314,23 @@ func (h *OrderHandler) Create(c *gin.Context) {
|
||||
if err := utils.DB.Where("`key` = ?", "tax_rate").First(&setting).Error; err == nil {
|
||||
taxRate, _ = strconv.ParseFloat(setting.Value, 64)
|
||||
}
|
||||
if err := utils.DB.Where("`key` = ?", "payment_channel_fee_rate").First(&setting).Error; err == nil {
|
||||
channelFeeRate, _ = strconv.ParseFloat(setting.Value, 64)
|
||||
}
|
||||
|
||||
shippingFee := shippingFeeFirstWeight
|
||||
if subtotal >= 99 || shippingFeeFirstWeight == 0 {
|
||||
shippingFee = 0
|
||||
} else if totalQuantity > 500 {
|
||||
shippingFee += shippingFeePerGram * float64(totalQuantity-500)
|
||||
// 根据商品重量计算运费
|
||||
shippingFee := 0.0
|
||||
if subtotal < 99 && shippingFeeFirstWeight > 0 {
|
||||
shippingFee = shippingFeeFirstWeight
|
||||
if totalWeight > 500 {
|
||||
shippingFee += shippingFeePerGram * (totalWeight - 500)
|
||||
}
|
||||
}
|
||||
|
||||
serviceFee := subtotal * serviceFeeRate / 100
|
||||
tax := subtotal * taxRate / 100
|
||||
totalAmount := subtotal + shippingFee + serviceFee + tax
|
||||
channelFee := subtotal * channelFeeRate / 100
|
||||
totalAmount := subtotal + shippingFee + serviceFee + tax + channelFee
|
||||
|
||||
order := models.Order{
|
||||
UserID: userID,
|
||||
@@ -331,6 +339,7 @@ func (h *OrderHandler) Create(c *gin.Context) {
|
||||
ShippingFee: shippingFee,
|
||||
ServiceFee: serviceFee,
|
||||
Tax: tax,
|
||||
ChannelFee: channelFee,
|
||||
TotalAmount: totalAmount,
|
||||
Status: models.OrderStatusPendingPayment,
|
||||
ShippingAddressID: &req.ShippingAddressID,
|
||||
|
||||
Reference in New Issue
Block a user