为购物车数量的定义迭代添加运费的公式
Formula to add shipping cost for defined iteration of cart quantity
假设我们有这些变量。
// 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)
但它不起作用,它仅在 2012 年 4 月 8 日等而不是 2013 年 5 月 9 日发生变化,并且它不是以 10 开头,即:购物车中有 2 件。
使用 ceil 而不是 floor:
$total_shipping_cost = $product_shipping_cost *
ceil($cart_quantity/$product_shipping_interval_quantity)
假设我们有这些变量。
// 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)
但它不起作用,它仅在 2012 年 4 月 8 日等而不是 2013 年 5 月 9 日发生变化,并且它不是以 10 开头,即:购物车中有 2 件。
使用 ceil 而不是 floor:
$total_shipping_cost = $product_shipping_cost *
ceil($cart_quantity/$product_shipping_interval_quantity)