Created
July 4, 2012 04:32
-
-
Save jedateach/3045350 to your computer and use it in GitHub Desktop.
Region Shipping Modifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class RegionShippingModifier extends OrderModifier{ | |
//TODO: allow defining regions / prices with databobjects in SiteConfig | |
static $db = array( | |
'Region' => 'Varchar' | |
); | |
static $regionarray = null; // array of region - price | |
static function set_region_prices(array $prices){ | |
self::$regionarray = $prices; | |
} | |
static function get_region_prices(){ | |
return self::$regionarray; | |
} | |
static function getRegionList () { | |
$options = array(); | |
foreach(self::get_region_prices() as $region => $price){ | |
$options[$region] = $region.": $".number_format($price, 2, '.', ''); | |
} | |
return $options; | |
} | |
function TableTitle() { | |
$region = ($this->getreg()) ? " - ".$this->getreg() : ""; | |
return 'Delivery'.$region; | |
} | |
protected function getreg(){ | |
$region = $this->Region; | |
if(!$region && $mem = Member::currentUser()){ | |
$region = $mem->Region; | |
} | |
return $region; | |
} | |
function LiveAmount(){ | |
$region = $this->getreg(); | |
if(self::$regionarray && count(self::$regionarray) > 0){ | |
if(isset(self::$regionarray[$region])){ | |
return self::$regionarray[$region]; | |
} | |
} | |
if(isset(self::$regionarray['default'])){ | |
return self::$regionarray['default']; | |
} | |
return 0; | |
} | |
function ShowInTable() { | |
return $this->Amount(); | |
} | |
static function show_form() { | |
return true; | |
} | |
static function get_form($controller) { | |
$fields = new FieldSet(); | |
$regionList = self::getRegionList(); | |
$fields->push($regionField = new DropdownField('Region','Delivery Region',$regionList)); | |
$validator = null; | |
$actions = new FieldSet( | |
new FormAction('processRegionShippingOrderModifier', 'Update shipping region') | |
); | |
return new RegionShippingModifier_Form($controller, 'RegionShippingModifierForm', $fields, $actions, $validator); | |
} | |
function setRegion($region){ | |
$this->setField('Region', $region); | |
$this->Amount = $this->LiveAmount(); | |
$this->write(); | |
} | |
} | |
class RegionShippingModifier_Form extends OrderModifierForm { | |
function processRegionShippingOrderModifier($data, $form) { | |
$order = ShoppingCart::current_order(); | |
$modifiers = $order->Modifiers(); | |
foreach($modifiers as $modifier) { | |
if ($modifier instanceof RegionShippingModifier) { | |
$modifier->Region = $data['Region']; | |
} | |
} | |
return $this->redirect("success","Region changed successfully"); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment