My first contribution, and my first SilverStripe site!
I had the need to calculate shipping based on the quantity of purchase orders. I won't be changing the costs often, otherwise I could have made custom database fields to hold the shipping costs. I hacked Simple Shipping Calculator and have the following code successfully charging $2 for 1-2 items, $0.75 for 3-19 items, and $0.50 for 20 or more items. Enjoy!
Sam Oltz
Spearia, Inc.
<?php
/**
* @package ecommerce
*/
/**
* SimpleShoppingCalculator is the default shipping calculation scheme.
* It lets you set a fixed shipping costs, or a fixed cost for each country you're delivering to.
* If you require more advanced shipping control, we suggest that you create your own subclass of {@link ShippingCalculator}
*/
class SimpleShippingCalculator extends ShippingCalculator {
static $default_charge = 2;
// static $charges_by_country = array('US' => 5);
static $charges_by_quantity = array( 1 => 2, 3 => 0.75, 20 => 0.50);
static function set_charges($charge) {
self::$default_charge = $charge;
}
function getCharge(Order $o) {
$orderItems = $o->Items();
// Calculate the total quantity of the order
if($orderItems){
foreach($orderItems as $orderItem){
$totalQuantity += $orderItem->quantity;
}
}
foreach(self::$charges_by_quantity as $quan => $price){
if ($totalQuantity >= $quan){
$totalCost = $price * $totalQuantity;
}
}
return $totalCost;
}
}