Formula to add shipping cost for defined iteration of cart quantity

By | October 30, 2018
Questions:

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.

Answers:
Category: php

Leave a Reply

Your email address will not be published. Required fields are marked *