Shopping Cart PHP Algorithms

When working with shipping methods, developers are able to code custom PHP algorithms to calculate pricing. Below you will find some common examples.

To configure a custom PHP algorithm for a shipping method, navigate to:
Dashboard > Settings > Commerce > Shipping Methods > New Shipping Method. 

When creating the new shipping method, select the "Custom PHP Algorithm" pricing option. 

Example One: Using Postcodes

A florist charges $10 delivery to suburbs nearby, $20 to suburbs slightly further away, otherwise a flat rate of $30. Shipping is calculated per cart, not per item.

<?php /** * Calculates the cost of shipping for a cart * @return float Shipping cost */ return function() { $iPostcode = \Components\Commerce\Carts\Current::getShippingColumn('shipping_postcode'); $arrPostcodesNearby = array(4000, 4001, 4002, 4003, 4004, 4005); $arrPostcodesOuter = array(4010, 4011, 4012, 4013, 4014, 4015, 4016); if(in_array($iPostcode, $arrPostcodesNearby)) { return 10; } if(in_array($iPostcode, $arrPostcodesOuter)) { return 20; } return 30; }; ?>

Example Two: Restrict by States within Australia

A retailer offers free shipping, however only delivers to certain states within Australia. Where the user is located outside the allowed states, show an error prompting customers to contact.

<?php /** * Calculates the cost of shipping for a cart * @return float Shipping cost */ return function() { // Get Shipping Country and State. $strState = \Components\Commerce\Carts\Current::getShippingColumn('shipping_state'); // List of allowed shipping States. $arrAllowedStates = array('QLD','VIC','NSW','SA','ACT'); // Free shipping if shipping state is allowed. if (in_array($strState, $arrAllowedStates)) { return 0; } // Throw error if not. else if(isset($strState)) { $pException = new \Framework\Exceptions\Validation(); $pException->setTitle('Sorry, our website cannot provide an instant shipping cost for delivery in your area. Please <a href="/contact-us/">contact us</a> to discuss'); throw $pException; } }; ?>

Example Three: Using Weight

An online retailer ships heavy items around the country (eg. trampolines and accessories). A delivery fee is calculated based on the weight of the products.

Charge $25 per item under 10kg, and $40 per item between 10-20kg. Where items in the cart are over 20kg, throw an error asking the user to request a quote. 

Charge per item in the shopping cart, rather than a flat total for the entire cart. 

<?php /** * Calculates the cost of shipping for a cart, has been customized as follows: * * $25 each item under 10kg * $40 each item between 10kg and 20kg * * Where the product weight is greater than 20kg, throw an error asking user to request a quote. * * @return float Shipping cost */ return function() { $iTotal = 0; // Loop over each item in the cart foreach(\Components\Commerce\Carts\Current::getAll() as $arrCartItem) { // Get the weight of the product $iWeight = \Components\Commerce\Products::getColumn($arrCartItem['product_id'], 'product_shipping_weight_g'); // Check weight is set if (!$iWeight) { $pException = new \Framework\Exceptions\Validation(); $pException->setTitle('Please <a href="/contact/">contact us</a> to order these items so that we can discuss freight options with you'); throw $pException; } // $25 under 10kg if($iWeight < 10000) { $iTotal += 25; } // $40 between 10kg and 20kg elseif($iWeight >= 10000 && $iWeight < 20000) { $iTotal += 40; } // Weight greater than 20kg else { $pException = new \Framework\Exceptions\Validation(); $pException->setTitle('Please <a href="/contact/">contact us</a> to order these items so that we can discuss freight options with you'); throw $pException; } } return $iTotal; }; ?>

Example Four: Using Product Categories

An electronics store charges based on the Product Category of the Products in the shopping cart.

Charge $200 delivery for Desks, and $150 for Computers (where Desks and Computers are the Product Categories). Otherwise charge $50. 


<?php /** * Calculates the cost of shipping for a cart * * @return float Shipping cost */ return function() { foreach(\Components\Commerce\Carts\Current::getAll() as $arrCartItem) { if($arrCartItem['product_id']) { $arrCategories = \Components\Commerce\Products\Categories::getAllRecursiveForProduct($arrCartItem['product_id']); foreach($arrCategories as $arrCategory) { // Desks are Product Category ID 3 if($arrCategory['product_category_id'] == 3) { return 200; } // Computers are Product Category ID 4 if($arrCategory['product_category_id'] == 4) { return 150; } } } } // Everything Else return 50; }; ?>