为已定义的购物车数量迭代添加运输成本的公式



让我们假设我们有这些变量。

// base shipping cost of product
$product_shipping_cost = 10; 
// You can send 4 same products in the same pack with $product_shipping_cost 
$product_shipping_interval_quantity = 4;
// Your current quantity in cart
$cart_quantity = 9;
// defined total shipping cost, should be in this case 30;
// because 9 in $cart quantity
$total_shipping_cost = 0;

这在现实生活中应该如何工作

基本上我们有这个:

  • 当$cart_quantity==1时,$total_shipping_cost为10$
  • 当$cart_quantity<=4则$total_shipping_cost仍然是10$
  • 当$cart_quantity==5时,$total_shipping_cost应为20$
  • 当$cart_quantity<=8那么$total_shipping_cost仍然是20$
  • 当$cart_quantity==9时,$total_shipping_cost应为30$
  • 等等

如何实现这样的公式?

我试过了:

$total_shipping_cost = $product_shipping_cost * floor($cart_quantity/$product_shipping_interval_quantity)

但它不起作用,它只在12年4月8日等时间发生变化,而不是在13年5月9日,也不是从10开始,即:购物车中有2件。

使用天花板而不是地板:

$total_shipping_cost = $product_shipping_cost * 
ceil($cart_quantity/$product_shipping_interval_quantity)

最新更新