Shipping table rate based methods, like Fish and Ships, are flexible, highly customisable and easy to use and understand. However, this way has a flaw: what happens with non wide-ranges based shipping rates? Until now, maybe you need to create tons of rules in this scenario. But from now on Fish and Ships Pro have a new special action: math expressions. Oh, yeah 🙂
Math expressions come with a math parser. With it you can use a wide range of variables like weight, volume, quantity (here the full list with explanation) and most of the PHP math functions (here the list too), both below this article
Table of Contents
A simple case: shipping rates increasing half kilo each time
Let’s say we want to set this shipping rates:
- up to 0.5Kg : 2$
- up to 1Kg : 3$
- up to 1.5Kg: 4$
- up to 2Kg: 5$
Until now, you must create four rules, one per each 0.5Kg range. This is quick, right. But… what happens if you want to cover until 100Kg? Will you put 200 rules?! Until now, you can use our price fixed + price* weight. But it’s not rounding every half kilo, so you get:
- 0.5Kg calculation: 1$ + 2$ * weight = 2$. This is ok!
- 0.75Kg calculation: 1$ + 2$ * weight = 2.5$. This is wrong!
…this is because the needed formula should round up weight into half kilos, there is a function for it: ceil(). We need to multiply weight * 2, round up it, and then divide again by 2:
Half kilo rounded up = ceil( weight * 2 ) / 2
…every half kilo cost 1$, and we need to add a 1$ extra on any case (note that price start on 2$ for half kilo, not 1$):
result = 1 + 1 * ceil( weight* 2 ) / 2;
Here the math expression you need to put on Fish and Ships. Note that you dont’have to put currency, and must start with result = and end with semicolon .
Complex case: Math expressions on pallet calculation
Now considere we’ve a construction store, and we sell briks. There is three sizes of bricks, and all of them will be sent in pallets. The shipping rates are based on the amount of pallets. It doesn’t matter if they are full or half filled, here the maximum amout of bricks per pallet:
- Small bricks: 576 per pallet
- Mid sized bricks: 180 per pallet
- Big bricks: 90 per pallet
Every pallet cost 95$, and the products can’t be mixed in the pallet. Here two examples of what we need:
- 200 small bricks: 1 * 95$ = 95$
- 200 small bricks + 200 mid sized will cost: 1 * 95$ + 2 * 95$ = 285$
First of all, we need to discriminate the different brick sizes. We create three shipping classes, and apply to the products:
- 576xPallet
- 180xPallet
- 90xPallet
Then we create three shipping rules, to filter products per size:
Note that we’re put the pallet price on the shipping costs field. We will get it on the variable rule_cost of the math expression. Now we will add on each rule the special action math expression, and this is the formula used for small bricks (576 per pallet):
result = rule_cost * ceil ( qty / 576 );
- From 1 to 576 bricks, the cost will be: 95 * 1
- Form 577 bricks to 1.152, the cost will be: 95 * 2
- …the same for more bricks: 3 pallets, 4, etc.
And a final question: on our shop, distinct SKU products can’t be mixed on the pallets. For example: 250 white small bricks and 250 black small bricks will be sent using two pallets. This is achieved by setting the group by option to “Per ID / SKU” on the math expression dialog. The products will be grouped first, and then, for every group (in this case, per each colour of brick) the math expression will be evaluated.
Variables on the math expressions
Here the list of variables you can use on your math expressions. It will be set on each rule, with the values of the products on the cart who have fulfilled the rule selection filter:
- price
- weight
- volume
- volumetric: (see explanation here)
- qty: number of products
- rule_cost: the shipping cost field value on the rule (see previous pallets example)
- groups: the number of groups using the math expression group by setting (see previous pallets example)
- cart_total: global cart price: all products minus coupons applied
- min_dimension / mid_dimension / max_dimension : only available on group by setting set to non grouped
- l_w_h_dim: sum of dimensions, only available on group by setting set to non grouped
PHP functions on math expressions
Most of the PHP functions focused on math are available on the math parser. Use in this way:
round( 10.5 )
Here the list. Can get detailed info here:
- floor — Round fractions down
- round — Rounds a float
- ceil — Round fractions up
- sin — Sine
- asin — Arc sine
- sinh — Hyperbolic sine
- asinh — Inverse hyperbolic sine
- cos — Cosine
- acos — Arc cosine
- cosh — Hyperbolic cosine
- acosh — Inverse hyperbolic cosine
- tan — Tangent
- atan — Arc tangent
- tanh — Hyperbolic tangent
- atanh — Inverse hyperbolic tangent
- sqrt — Square root
- abs — Absolute value
- log — Natural logarithm
…and the constants (use it without parenthesis):
- pi — The pi number
- e – The Euler number