List regions

This commit is contained in:
Mike Jolley 2015-12-10 17:07:18 +00:00
parent e93853e4cf
commit 4557157890
3 changed files with 76 additions and 3 deletions

View File

@ -78,7 +78,7 @@
<div class="edit"><input type="text" name="zone_name[{{ data.zone_id }}]" data-attribute="zone_name" value="{{ data.zone_name }}" placeholder="<?php esc_attr_e( 'Zone Name', 'woocommerce' ); ?>" /></div> <div class="edit"><input type="text" name="zone_name[{{ data.zone_id }}]" data-attribute="zone_name" value="{{ data.zone_name }}" placeholder="<?php esc_attr_e( 'Zone Name', 'woocommerce' ); ?>" /></div>
</td> </td>
<td class="wc-shipping-zone-region"> <td class="wc-shipping-zone-region">
<div class="view">Add stuff here</div> <div class="view">{{ data.formatted_zone_location }}</div>
<div class="edit"> <div class="edit">
<select multiple="multiple" name="zone_locations[{{ data.zone_id }}]" data-attribute="zone_locations" data-placeholder="<?php _e( 'Select regions within this zone', 'woocommerce' ); ?>" class="wc-shipping-zone-region-select"> <select multiple="multiple" name="zone_locations[{{ data.zone_id }}]" data-attribute="zone_locations" data-placeholder="<?php _e( 'Select regions within this zone', 'woocommerce' ); ?>" class="wc-shipping-zone-region-select">
<?php <?php

View File

@ -87,6 +87,78 @@ class WC_Shipping_Zone {
return $this->data['zone_locations']; return $this->data['zone_locations'];
} }
/**
* Return a text string representing what this zone is for.
* @return string
*/
public function get_formatted_location( $max = 10 ) {
$location_parts = array();
$all_continents = WC()->countries->get_continents();
$all_countries = WC()->countries->get_countries();
$all_states = WC()->countries->get_states();
$locations = $this->get_zone_locations();
$continents = array_filter( $locations, array( $this, 'location_is_continent' ) );
$countries = array_filter( $locations, array( $this, 'location_is_country' ) );
$states = array_filter( $locations, array( $this, 'location_is_state' ) );
$postcodes = array_filter( $locations, array( $this, 'location_is_postcode' ) );
foreach ( $continents as $location ) {
$location_parts[] = $all_continents[ $location->code ]['name'];
}
foreach ( $countries as $location ) {
$location_parts[] = $all_countries[ $location->code ];
}
foreach ( $states as $location ) {
$location_codes = explode( ':', $location->code );
$location_parts[] = $all_states[ $location_codes[ 0 ] ][ $location_codes[ 1 ] ];
}
if ( sizeof( $location_parts ) > $max ) {
$remaining = sizeof( $location_parts ) - $max;
return sprintf( _n( '%s and %d other region', '%s and %d other regions', $remaining, 'woocommerce' ), implode( ', ', array_splice( $location_parts, 0, $max ) ), $remaining );
} else {
return implode( ', ', $location_parts );
}
}
/**
* Location type detection
* @param object $location
* @return boolean
*/
private function location_is_continent( $location ) {
return 'continent' === $location->type;
}
/**
* Location type detection
* @param object $location
* @return boolean
*/
private function location_is_country( $location ) {
return 'country' === $location->type;
}
/**
* Location type detection
* @param object $location
* @return boolean
*/
private function location_is_state( $location ) {
return 'state' === $location->type;
}
/**
* Location type detection
* @param object $location
* @return boolean
*/
private function location_is_postcode( $location ) {
return 'postcode' === $location->type;
}
/** /**
* Set zone ID * Set zone ID
* @access private * @access private

View File

@ -26,8 +26,9 @@ class WC_Shipping_Zones {
$zones = array(); $zones = array();
foreach ( $raw_zones as $raw_zone ) { foreach ( $raw_zones as $raw_zone ) {
$zone = new WC_Shipping_Zone( $raw_zone ); $zone = new WC_Shipping_Zone( $raw_zone );
$zones[] = $zone->get_data(); $zones[ $zone->get_zone_id() ] = $zone->get_data();
$zones[ $zone->get_zone_id() ]['formatted_zone_location'] = $zone->get_formatted_location();
} }
return $zones; return $zones;