Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ENHANCEMENT: Flexible method to set billing and shipping addresses which

does not rely on Geoip class.

Removing shipping country dependency on Geoip class. Can pass any
countrycode => countryname combo now.

Moving countries to Address class.

Refactor addresses to Address class instead of Shipping.
  • Loading branch information...
commit 132f941ccc2a61ece8e6f3fb795c7db62191cfc7 1 parent 4e44433
@frankmullenger authored
View
11 code/customer/CheckoutPage.php
@@ -169,6 +169,9 @@ function OrderForm() {
if ($member->ID) $form->loadDataFrom($member);
if ($billingAddress) $form->loadDataFrom($billingAddress->getCheckoutFormData('Billing'));
if ($shippingAddress) $form->loadDataFrom($shippingAddress->getCheckoutFormData('Shipping'));
+
+ //Hook for editing the checkout page order form
+ $this->extend('updateOrderForm', $form);
return $form;
}
@@ -193,7 +196,7 @@ private function addBillingAddressFields(&$fields, &$validator) {
$cityField = new TextField('Billing[City]', 'City');
$cityField->setCustomValidationMessage('Please enter your city.');
- $countryField = new DropdownField('Billing[Country]', 'Country', Geoip::getCountryDropDown());
+ $countryField = new DropdownField('Billing[Country]', 'Country', Address::$billing_countries);
$countryField->setCustomValidationMessage('Please enter your country.');
if (!Member::currentUserID() && Geoip::$default_country_code) $countryField->setValue(Geoip::$default_country_code);
@@ -241,11 +244,11 @@ private function addShippingAddressFields(&$fields, &$validator) {
$cityField = new TextField('Shipping[City]', 'City');
$cityField->setCustomValidationMessage('Please enter a city.');
- $countryField = new DropdownField('Shipping[Country]', 'Country', Shipping::supported_countries());
+ $countryField = new DropdownField('Shipping[Country]', 'Country', Address::$shipping_countries);
$countryField->setCustomValidationMessage('Please enter a country.');
- if (!Member::currentUserID() && Geoip::$default_country_code) $countryField->setValue(Geoip::$default_country_code); //Should probably do a default country in Shipping
+ if (!Member::currentUserID() && Geoip::$default_country_code) $countryField->setValue(Geoip::$default_country_code);
- $regions = Shipping::supported_regions();
+ $regions = Address::$shipping_regions;
$regionField = null;
if (!empty($regions)) {
$regionField = new RegionField('Shipping[Region]', 'Region');
View
2  code/form/RegionField.php
@@ -29,7 +29,7 @@ function __construct($name, $title = null, $source = array(), $value = "", $form
function FieldHolder() {
- $regions = Shipping::supported_regions();
+ $regions = Address::$shipping_regions;
$jsonRegions = json_encode($regions);
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
View
4 code/modifiers/shipping/FlatFee/FlatFeeShippingRate.php
@@ -50,7 +50,7 @@ public function getCMSFields_forPopup() {
$amountField->setAllowedCurrencies(Product::$allowed_currency);
$fields->push($amountField);
- $countryField = new DropdownField('CountryCode', 'Country', Shipping::supported_countries());
+ $countryField = new DropdownField('CountryCode', 'Country', Address::$shipping_countries);
$fields->push($countryField);
return $fields;
@@ -81,7 +81,7 @@ public function SummaryOfAmount() {
* @return String Name of country
*/
public function SummaryOfCountryCode() {
- $supportedCountries = Shipping::supported_countries();
+ $supportedCountries = Address::$shipping_countries;
if (in_array($this->CountryCode, array_keys($supportedCountries))) {
return $supportedCountries[$this->CountryCode];
}
View
66 code/modifiers/shipping/Shipping.php
@@ -1,66 +0,0 @@
-<?php
-/**
- * A convenience class to set shipping supported countries in a centralised location.
- *
- * @author Frank Mullenger <frankmullenger@gmail.com>
- * @copyright Copyright (c) 2011, Frank Mullenger
- * @package swipestripe
- * @subpackage modifiers
- * @version 1.0
- */
-class Shipping extends DataObject {
-
- /**
- * Countries allowed to be shipped to, these will be options in the shipping address
- * of the Checkout form.
- *
- * @see Shipping::set_supported_countries()
- * @var Array List of countries that goods can be shipped to e.g:'NZ' => 'New Zealand'
- */
- protected static $supported_countries = array(
- );
-
- protected static $supported_regions = array(
- );
-
- /**
- * Set countries that are supported for shipping to.
- *
- * @see Shipping::$supported_countries
- * @param array $countries
- */
- public static function set_supported_countries(Array $countries) {
-
- //Check each of the countries before adding them
- foreach ($countries as $countryCode) {
-
- if ($countryName = Geoip::countryCode2name($countryCode)) {
- self::$supported_countries[$countryCode] = $countryName;
- }
- else {
- user_error("Cannot set allowed country, it must be a country code supported by Geoip class.", E_USER_WARNING);
- //TODO return meaningful error to browser in case error not shown
- return;
- }
- }
- }
-
- /**
- * Get countries supported for shipping.
- *
- * @see Shipping::$supported_countries
- * @var Array List of countries that goods can be shipped to e.g:'NZ' => 'New Zealand'
- */
- public static function supported_countries() {
- return self::$supported_countries;
- }
-
- public static function set_supported_regions(Array $regions) {
- self::$supported_regions = $regions;
- }
-
- public static function supported_regions() {
- return self::$supported_regions;
- }
-
-}
View
4 code/modifiers/tax/FlatFee/FlatFeeTaxRate.php
@@ -51,7 +51,7 @@ public function getCMSFields_forPopup() {
$amountField->setAllowedCurrencies(Product::$allowed_currency);
$fields->push($amountField);
- $countryField = new DropdownField('CountryCode', 'Country', Shipping::supported_countries());
+ $countryField = new DropdownField('CountryCode', 'Country', Address::$shipping_countries);
$fields->push($countryField);
$rateField = new NumericField('Rate', 'Tax rate as a percentage');
@@ -94,7 +94,7 @@ public function SummaryOfRate() {
* @return String Name of country
*/
public function SummaryOfCountryCode() {
- $supportedCountries = Shipping::supported_countries();
+ $supportedCountries = Address::$shipping_countries;
if (in_array($this->CountryCode, array_keys($supportedCountries))) {
return $supportedCountries[$this->CountryCode];
}
View
36 code/order/Address.php
@@ -9,6 +9,42 @@
* @version 1.0
*/
class Address extends DataObject {
+
+ /**
+ * Countries for the billing address of the Checkout form.
+ *
+ * @var Array List of countries for billing address e.g:'NZ' => 'New Zealand'
+ */
+ public static $billing_countries = array(
+ );
+
+ /**
+ * Regions allowed to be shipped to, currently unused.
+ */
+ public static $billing_regions = array(
+ );
+
+ /**
+ * Countries allowed to be shipped to, these will be options in the shipping address
+ * of the Checkout form.
+ *
+ * @var Array List of countries that goods can be shipped to e.g:'NZ' => 'New Zealand'
+ */
+ public static $shipping_countries = array(
+ );
+
+ /**
+ * Regions allowed to be shipped to, these will be options in the shipping address
+ * of the Checkout form.
+ *
+ * @var Array List of regions that goods can be shipped to e.g:
+ * 'NZ' => array(
+ * 'NI' => 'North Island',
+ * 'SI' => 'South Island')
+ */
+ public static $shipping_regions = array(
+ );
+
/**
* DB fields for an address
View
31 code/order/Order.php
@@ -667,8 +667,9 @@ function addAddressesAtCheckout(Array $data) {
$member = Customer::currentUser() ? Customer::currentUser() : singleton('Customer');
$order = CartControllerExtension::get_current_order();
- $supportedCountries = Shipping::supported_countries();
- $supportedRegions = Shipping::supported_regions();
+ $billingCountries = Address::$billing_countries;
+ $shippingCountries = Address::$shipping_countries;
+ $shippingRegions = Address::$shipping_regions;
//If there is a current billing and shipping address, update them, otherwise create new ones
$existingBillingAddress = $this->BillingAddress();
@@ -680,8 +681,8 @@ function addAddressesAtCheckout(Array $data) {
$newData[$fieldName] = $value;
}
- $newData['CountryName'] = (in_array($newData['Country'], array_keys($supportedCountries)))
- ? $supportedCountries[$newData['Country']]
+ $newData['CountryName'] = (in_array($newData['Country'], array_keys($billingCountries)))
+ ? $billingCountries[$newData['Country']]
: null;
if ($member->ID) $newData['MemberID'] = $member->ID;
@@ -702,8 +703,8 @@ function addAddressesAtCheckout(Array $data) {
$billingAddress->State = $data['Billing']['State'];
$billingAddress->Country = $data['Billing']['Country'];
- $billingAddress->CountryName = (in_array($data['Billing']['Country'], array_keys($supportedCountries)))
- ? $supportedCountries[$data['Billing']['Country']]
+ $billingAddress->CountryName = (in_array($data['Billing']['Country'], array_keys($billingCountries)))
+ ? $billingCountries[$data['Billing']['Country']]
: null;
$billingAddress->Type = 'Billing';
@@ -716,13 +717,13 @@ function addAddressesAtCheckout(Array $data) {
$newData[$fieldName] = $value;
}
- $newData['CountryName'] = (in_array($newData['Country'], array_keys($supportedCountries)))
- ? $supportedCountries[$newData['Country']]
+ $newData['CountryName'] = (in_array($newData['Country'], array_keys($shippingCountries)))
+ ? $shippingCountries[$newData['Country']]
: null;
- if (isset($newData['Region']) && isset($supportedRegions[$newData['Country']])) {
- if (in_array($newData['Region'], array_keys($supportedRegions[$newData['Country']]))) {
- $newData['RegionName'] = $supportedRegions[$newData['Country']][$newData['Region']];
+ if (isset($newData['Region']) && isset($shippingRegions[$newData['Country']])) {
+ if (in_array($newData['Region'], array_keys($shippingRegions[$newData['Country']]))) {
+ $newData['RegionName'] = $shippingRegions[$newData['Country']][$newData['Region']];
}
}
else $newData['RegionName'] = null;
@@ -746,12 +747,12 @@ function addAddressesAtCheckout(Array $data) {
$shippingAddress->Country = $data['Shipping']['Country'];
$shippingAddress->Region = (isset($data['Shipping']['Region'])) ? $data['Shipping']['Region'] : null;
- $shippingAddress->CountryName = (in_array($data['Shipping']['Country'], array_keys($supportedCountries)))
- ? $supportedCountries[$data['Shipping']['Country']]
+ $shippingAddress->CountryName = (in_array($data['Shipping']['Country'], array_keys($shippingCountries)))
+ ? $shippingCountries[$data['Shipping']['Country']]
: null;
- $shippingAddress->RegionName = (isset($data['Shipping']['Region']) && isset($supportedRegions[$data['Shipping']['Country']]) && in_array($data['Shipping']['Region'], array_keys($supportedRegions[$data['Shipping']['Country']])))
- ? $supportedRegions[$data['Shipping']['Country']][$data['Shipping']['Region']]
+ $shippingAddress->RegionName = (isset($data['Shipping']['Region']) && isset($shippingRegions[$data['Shipping']['Country']]) && in_array($data['Shipping']['Region'], array_keys($shippingRegions[$data['Shipping']['Country']])))
+ ? $shippingRegions[$data['Shipping']['Country']][$data['Shipping']['Region']]
: null;
$shippingAddress->Type = 'Shipping';
View
2  tests/CheckoutTest.php
@@ -58,7 +58,7 @@ function setUp() {
));
//Enable shipping countries
- Shipping::set_supported_countries(array('NZ', 'AU'));
+ Address::$shipping_countries = array('NZ' => 'New Zealand', 'AU' => 'Australia');
}
/**
View
2  tests/OrderModifierTest.php
@@ -34,7 +34,7 @@ function setUp() {
));
//Flat fee shipping enabled for NZ and AU
- Shipping::set_supported_countries(array('NZ', 'AU'));
+ Address::$shipping_countries = array('NZ' => 'New Zealand', 'AU' => 'Australia');
FlatFeeShipping::enable();
}
Please sign in to comment.
Something went wrong with that request. Please try again.