以下是一个简单的PHP实例,用于实现阶梯批发价格策略。该策略根据购买数量提供不同的价格折扣。
```php

// 定义商品的基本价格
$basePrice = 100;
// 定义阶梯批发价格表
$wholesalePrices = [
1 => $basePrice, // 购买1件商品的价格
5 => 90, // 购买5件商品的价格
10 => 80, // 购买10件商品的价格
20 => 70 // 购买20件商品的价格
];
// 获取用户购买的商品数量
$quantity = $_POST['quantity'] ?? 0;
// 计算批发价格
function calculateWholesalePrice($quantity, $wholesalePrices) {
foreach ($wholesalePrices as $limit => $price) {
if ($quantity >= $limit) {
return $price;
}
}
return $basePrice; // 如果没有达到任何阶梯,则按基础价格计算
}
// 计算总价格
$totalPrice = $quantity * calculateWholesalePrice($quantity, $wholesalePrices);
// 输出结果
echo "







