Version: current

     1 <?php
     2
     3 /**
     4  * Helper class for calculating rates for available shipping options.
     5  * Provides a little caching, so estimates aren't calculated more than once.
     6  */
     7 class ShippingEstimator {
     8
     9     protected $package, $address, $estimates = null, $calculated = false;
    10
    11     function __construct(ShippingPackage $package, Address $address){
    12         $this->package = $package;
    13         $this->address = $address;
    14     }
    15
    16     function getEstimates(){
    17         if ($this->calculated){
    18             return $this->estimates;
    19         }
    20
    21         $this->estimates = new DataObjectSet();
    22         if ($options = ShippingMethod::get_shipping_methods()) {
    23             foreach ($options as $option) {
    24                 if ($option->calculateRate($this->package, $this->address) !== null) {
    25                     $this->estimates->push($option);
    26                 }
    27             }
    28         }
    29
    30         $this->estimates->sort("CalculatedRate","ASC"); //sort by rate, lowest to highest
    31         $this->calculated = true;
    32         return $this->estimates;
    33     }
    34 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -