Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Quantity Based Shipping for Ecommerce Module

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

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

Page: 1
Go to End
Author Topic: 2005 Views
  • pianomansam
    Avatar
    Community Member
    9 Posts

    Quantity Based Shipping for Ecommerce Module Link to this post

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

  • runnerman
    Avatar
    Community Member
    24 Posts

    Re: Quantity Based Shipping for Ecommerce Module Link to this post

    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.

  • pianomansam
    Avatar
    Community Member
    9 Posts

    Re: Quantity Based Shipping for Ecommerce Module Link to this post

    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.

  • runnerman
    Avatar
    Community Member
    24 Posts

    Re: Quantity Based Shipping for Ecommerce Module Link to this post

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

    ?>

  • runnerman
    Avatar
    Community Member
    24 Posts

    Re: Quantity Based Shipping for Ecommerce Module Link to this post

    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.

    2005 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.