Version: current

     1 <?php
     2 /**
     3  * Табличный метод расчета стоимости доставки (от него наследуем способы доставки)
     4  *
     5  * @package cart_shipping
     6  * @author menedem
     7  */
     8 class TableShippingMethod extends ShippingMethod{
     9
    10     static $packageConstraints = array(); //Массив полей, по которым отбираются стоимости доставки
    11
    12     static function setConstraints($val) {
    13         self::$packageConstraints = $val;
    14     }
    15
    16     function getConstraints() {
    17         user_error("Не задано поле для отбора способа доставки в _config.php", E_USER_ERROR);
    18     }
    19
    20     static $needRegionRestriction = true;
    21
    22     static function setNeedRegionRestriction($val) {
    23         self::$needRegionRestriction = $val;
    24     }
    25
    26     static function getNeedRegionRestriction() {
    27         return self::$needRegionRestriction;
    28     }
    29
    30     /**
    31      * Find the appropriate shipping rate from stored table range metrics
    32      */
    33     function calculateRate(ShippingPackage $package, Address $address) {
    34         $rate = null;
    35         if ($this->Rates()->exists()) {
    36             $shippingRateClass = $this->Rates()->First()->ClassName;
    37             $packageconstraints = $this->getConstraints();
    38             $constraintfilters = array();
    39             foreach($packageconstraints as $pakval){
    40                 $mincol = "\"{$shippingRateClass}\".\"{$pakval}Min\"";
    41                 $maxcol = "\"{$shippingRateClass}\".\"{$pakval}Max\"";
    42                 $constraintfilters[] = "(".
    43                     "$mincol >= 0" .
    44                     " AND $mincol <= " . $package->{$pakval}() . //{$pakval}
    45                     " AND $maxcol > 0". //ignore constraints with maxvalue = 0
    46                     " AND $maxcol >= " . $package->{$pakval}() . //{$pakval}
    47                     " AND $mincol < $maxcol" . //sanity check
    48                 ")";
    49             }
    50             $filter = "(".implode(") AND (",array(
    51                 "\"ShippingMethodID\" = ".$this->ID,
    52                 (self::getNeedRegionRestriction()) ? RegionRestriction::address_filter($address) : array(), //address restriction if needed
    53                 implode(" OR ",$constraintfilters) //metrics restriction
    54             )).")";
    55             if ($tr = DataObject::get_one($shippingRateClass, $filter, true, "Rate ASC")) {
    56                 $rate = $tr->Rate;
    57             }
    58             $this->CalculatedRate = $rate;
    59         }
    60         return $rate;
    61     }
    62 }
    63
    64 /**
    65  * Adds extra metric ranges to restrict with, rather than just region.
    66  */
    67 class TableShippingRate extends RegionRestriction {
    68
    69     static $db = array(
    70         'Rate' => 'CatalogPrice',
    71     );
    72
    73     static $summary_fields = array(
    74         'Country',
    75         'State',
    76         'City',
    77         'PostalCode',
    78         'Rate'
    79     );
    80
    81     static $default_sort = '"Country" ASC, "State" ASC, "City" ASC, "PostalCode" ASC, "Rate" ASC';
    82 }

[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. -