简体   繁体   中英

Formula to add shipping cost for defined iteration of cart quantity

Let's assume that we have these variables.

// 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;

How should this work in real life? So basically we have this:

  • When $cart_quantity == 1 then $total_shipping_cost is 10$
  • When $cart_quantity <= 4 then $total_shipping_cost is still 10$
  • When $cart_quantity == 5 then $total_shipping_cost should be 20$
  • When $cart_quantity <= 8 then $total_shipping_cost is still 20$
  • When $cart_quantity == 9 then $total_shipping_cost should be 30$
  • and so on and so forth ...

How to achieve such a formula? I tried to

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

But it doesn't work, it changing only when 4/8/12 etc not on 5/9/13 and it is not starting with 10 when ie: 2 pcs in cart.

Use ceil instead of floor:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM