Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Quantity Based Shipping for Ecommerce Module


Reply


5 Posts   2038 Views

Avatar
pianomansam

Community Member, 9 Posts

4 November 2008 at 10:28am

Edited: 04/11/2008 10:41am

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

Avatar
runnerman

Community Member, 24 Posts

7 November 2008 at 3:00am

Great stuff! I'd been thinking about this one for a while, but being new to PHP hadn't managed to get it working. I've managed to get the code to work for the more recent trunk versions of ecommerce, which use OrderModifier instead of ShippingCalculator, will upload when it's finished/tidied up.

Avatar
pianomansam

Community Member, 9 Posts

7 November 2008 at 3:20am

Yeah, I attempted to provide this functionality to the trunk release of Ecommerce, but reverted back when I saw the Reports feature didn't work anymore. It was very easy to change in the trunk release because of the new architecture, so I hope to see it up an running soon with full features.

Avatar
runnerman

Community Member, 24 Posts

7 November 2008 at 3:24am

Edited: 07/11/2008 3:28am

Right, here's a modified version to work with the newer trunk versions of ecommerce which use OrderModifier.

<?php

/**
* @package ecommerce
*/

/**
* DeliverychargeModifier is based on the default shipping calculation scheme.
* It lets you set shipping costs by quantity
*/
class DeliveryChargeModifier extends OrderModifier {
protected static $is_chargable = true;
static $default_charge = 0.7;

static $charges_by_quantity = array( 1 => 0.7, 4 => 1.5, 7 => 3);

static function set_charges($charge) {
self::$default_charge = $charge;
}

function LiveAmount() {
$o = ShoppingCart::current_order();
$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;
    }

// Display Functions

   function TableTitle() {
      return "Shipping";
   }
   function CartTitle() {return 'Shipping: ';}
}

?>

Avatar
runnerman

Community Member, 24 Posts

7 November 2008 at 3:26am

Edited: 07/11/2008 3:27am

Yes it'll be good to see the new release, should be a big improvement on a very useful module. I need the reports but had problems with the older 0.5 version so chose to upgrade.