Started on zone regions selector
This commit is contained in:
parent
3a8ed22905
commit
cf5381083d
|
@ -1,10 +1,18 @@
|
|||
/* global shippingZonesLocalizeScript, ajaxurl */
|
||||
( function( $, data, wp, ajaxurl ) {
|
||||
$( function() {
|
||||
|
||||
var $table = $( '.wc_shipping_zones' ),
|
||||
$tbody = $( '.wc-shipping-zone-rows' ),
|
||||
$save_button = $( 'input[name="save"]' ),
|
||||
var $table = $( '.wc_shipping_zones' ),
|
||||
$tbody = $( '.wc-shipping-zone-rows' ),
|
||||
$save_button = $( 'input[name="save"]' ),
|
||||
$row_template = wp.template( 'wc-shipping-zone-row' ),
|
||||
select2_args = $.extend({
|
||||
minimumResultsForSearch: 10,
|
||||
allowClear: $( this ).data( 'allow_clear' ) ? true : false,
|
||||
placeholder: $( this ).data( 'placeholder' ),
|
||||
matcher: function( term, text, opt ) {
|
||||
return text.toUpperCase().indexOf( term.toUpperCase() ) >= 0 || opt.attr( "alt" ).toUpperCase().indexOf( term.toUpperCase() ) >= 0;
|
||||
}
|
||||
}, getEnhancedSelectFormatString() );
|
||||
|
||||
// Backbone model
|
||||
ShippingZone = Backbone.Model.extend({
|
||||
|
@ -30,6 +38,7 @@
|
|||
}
|
||||
},
|
||||
onSaveResponse: function( response, textStatus ) {
|
||||
console.log(response);
|
||||
if ( 'success' === textStatus ) {
|
||||
if ( response.success ) {
|
||||
shippingZone.set( 'zones', response.data.zones );
|
||||
|
@ -45,7 +54,7 @@
|
|||
|
||||
// Backbone view
|
||||
ShippingZoneView = Backbone.View.extend({
|
||||
rowTemplate: wp.template( 'wc-shipping-zone-row' ),
|
||||
rowTemplate: $row_template,
|
||||
initialize: function() {
|
||||
this.listenTo( this.model, 'change:zones', this.setUnloadConfirmation );
|
||||
this.listenTo( this.model, 'saved:zones', this.clearUnloadConfirmation );
|
||||
|
@ -73,9 +82,16 @@
|
|||
// Populate $tbody with the current zones
|
||||
$.each( zones, function( id, rowData ) {
|
||||
view.$el.append( view.rowTemplate( rowData ) );
|
||||
|
||||
var locations = view.$el.find( 'select[name="zone_locations[' + rowData.zone_id + ']"]');
|
||||
|
||||
// Select values in region select
|
||||
_.each( rowData.zone_locations, function( location ) {
|
||||
locations.find( 'option[value="' + location.type + ':' + location.code + '"]' ).prop( "selected", true );
|
||||
} );
|
||||
} );
|
||||
|
||||
// Make the rows functiothis.$el.find( '.wc-shipping-zone-delete' ).on( 'click', { view: this }, this.onDeleteRow );
|
||||
// Make the rows function
|
||||
this.$el.find('.view').show();
|
||||
this.$el.find('.edit').hide();
|
||||
this.$el.find( '.wc-shipping-zone-edit' ).on( 'click', { view: this }, this.onEditRow );
|
||||
|
@ -127,6 +143,7 @@
|
|||
event.preventDefault();
|
||||
$( this ).closest('tr').find('.view, .wc-shipping-zone-edit').hide();
|
||||
$( this ).closest('tr').find('.edit').show();
|
||||
$( '.wc-shipping-zone-region-select' ).select2( select2_args );
|
||||
event.data.view.model.trigger( 'change:zones' );
|
||||
},
|
||||
onDeleteRow: function( event ) {
|
||||
|
@ -184,15 +201,13 @@
|
|||
zones = _.indexBy( model.get( 'zones' ), 'zone_id' ),
|
||||
changes = {};
|
||||
|
||||
reordered_zones = _.map( zones, function( zone ) {
|
||||
_.each( zones, function( zone ) {
|
||||
var old_position = parseInt( zone.zone_order, 10 );
|
||||
var new_position = parseInt( $table.find( 'tr[data-id="' + zone.zone_id + '"]').index(), 10 );
|
||||
|
||||
if ( old_position !== new_position ) {
|
||||
changes[ zone.zone_id ] = _.extend( changes[ zone.zone_id ] || {}, { zone_order : new_position } );
|
||||
}
|
||||
|
||||
return zone;
|
||||
} );
|
||||
|
||||
if ( _.size( changes ) ) {
|
||||
|
@ -209,6 +224,7 @@
|
|||
} );
|
||||
|
||||
shippingZoneView.render();
|
||||
|
||||
$tbody.sortable({
|
||||
items: 'tr',
|
||||
cursor: 'move',
|
||||
|
@ -216,5 +232,55 @@
|
|||
handle: 'td.wc-shipping-zone-sort',
|
||||
scrollSensitivity: 40
|
||||
});
|
||||
|
||||
function getEnhancedSelectFormatString() {
|
||||
var formatString = {
|
||||
formatMatches: function( matches ) {
|
||||
if ( 1 === matches ) {
|
||||
return wc_enhanced_select_params.i18n_matches_1;
|
||||
}
|
||||
return wc_enhanced_select_params.i18n_matches_n.replace( '%qty%', matches );
|
||||
},
|
||||
formatNoMatches: function() {
|
||||
return wc_enhanced_select_params.i18n_no_matches;
|
||||
},
|
||||
formatAjaxError: function() {
|
||||
return wc_enhanced_select_params.i18n_ajax_error;
|
||||
},
|
||||
formatInputTooShort: function( input, min ) {
|
||||
var number = min - input.length;
|
||||
|
||||
if ( 1 === number ) {
|
||||
return wc_enhanced_select_params.i18n_input_too_short_1;
|
||||
}
|
||||
|
||||
return wc_enhanced_select_params.i18n_input_too_short_n.replace( '%qty%', number );
|
||||
},
|
||||
formatInputTooLong: function( input, max ) {
|
||||
var number = input.length - max;
|
||||
|
||||
if ( 1 === number ) {
|
||||
return wc_enhanced_select_params.i18n_input_too_long_1;
|
||||
}
|
||||
|
||||
return wc_enhanced_select_params.i18n_input_too_long_n.replace( '%qty%', number );
|
||||
},
|
||||
formatSelectionTooBig: function( limit ) {
|
||||
if ( 1 === limit ) {
|
||||
return wc_enhanced_select_params.i18n_selection_too_long_1;
|
||||
}
|
||||
|
||||
return wc_enhanced_select_params.i18n_selection_too_long_n.replace( '%qty%', limit );
|
||||
},
|
||||
formatLoadMore: function() {
|
||||
return wc_enhanced_select_params.i18n_load_more;
|
||||
},
|
||||
formatSearching: function() {
|
||||
return wc_enhanced_select_params.i18n_searching;
|
||||
}
|
||||
};
|
||||
|
||||
return formatString;
|
||||
}
|
||||
});
|
||||
})( jQuery, shippingZonesLocalizeScript, wp, ajaxurl );
|
||||
|
|
|
@ -102,7 +102,7 @@ class WC_Admin_Assets {
|
|||
wp_register_script( 'flot-pie', WC()->plugin_url() . '/assets/js/jquery-flot/jquery.flot.pie' . $suffix . '.js', array( 'jquery', 'flot' ), WC_VERSION );
|
||||
wp_register_script( 'flot-stack', WC()->plugin_url() . '/assets/js/jquery-flot/jquery.flot.stack' . $suffix . '.js', array( 'jquery', 'flot' ), WC_VERSION );
|
||||
wp_register_script( 'wc-settings-tax', WC()->plugin_url() . '/assets/js/admin/settings-views-html-settings-tax' . $suffix . '.js', array( 'jquery', 'wp-util', 'underscore', 'backbone' ), WC_VERSION );
|
||||
wp_register_script( 'wc-shipping-zones', WC()->plugin_url() . '/assets/js/admin/wc-shipping-zones' . $suffix . '.js', array( 'jquery', 'wp-util', 'underscore', 'backbone', 'jquery-ui-sortable' ), WC_VERSION );
|
||||
wp_register_script( 'wc-shipping-zones', WC()->plugin_url() . '/assets/js/admin/wc-shipping-zones' . $suffix . '.js', array( 'jquery', 'wp-util', 'underscore', 'backbone', 'jquery-ui-sortable', 'wc-enhanced-select' ), WC_VERSION );
|
||||
|
||||
// Chosen is @deprecated (2.3) in favour of select2, but is registered for backwards compat
|
||||
wp_register_script( 'ajax-chosen', WC()->plugin_url() . '/assets/js/chosen/ajax-chosen.jquery' . $suffix . '.js', array( 'jquery', 'chosen' ), WC_VERSION );
|
||||
|
|
|
@ -75,10 +75,34 @@
|
|||
<td width="1%" class="wc-shipping-zone-sort"></td>
|
||||
<td class="wc-shipping-zone-name">
|
||||
<div class="view">{{ data.zone_name }}</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>
|
||||
<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>
|
||||
<td><a class="wc-shipping-zone-add-method button" href="#"><?php esc_html_e( 'Add a shipping method', 'woocommerce' ); ?></a></td>
|
||||
<td class="wc-shipping-zone-region">
|
||||
<div class="view">Add stuff here</div>
|
||||
<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">
|
||||
<?php
|
||||
foreach ( $continents as $continent_code => $continent ) {
|
||||
echo '<option value="continent:' . esc_attr( $continent_code ) . '" alt="">' . esc_html( $continent['name'] ) . '</option>';
|
||||
|
||||
$countries = array_intersect( array_keys( $allowed_countries ), $continent['countries'] );
|
||||
|
||||
foreach ( $countries as $country_code ) {
|
||||
echo '<option value="country:' . esc_attr( $country_code ) . '" alt="' . esc_attr( $continent['name'] ) . '">' . esc_html( ' ' . $allowed_countries[ $country_code ] ) . '</option>';
|
||||
|
||||
if ( $states = WC()->countries->get_states( $country_code ) ) {
|
||||
foreach ( $states as $state_code => $state_name ) {
|
||||
echo '<option value="state:' . esc_attr( $country_code . ':' . $state_code ) . '" alt="' . esc_attr( $continent['name'] . ' ' . $allowed_countries[ $country_code ] ) . '">' . esc_html( ' ' . $state_name ) . '</option>';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
<a href=""><?php _e( 'Limit to specific zip codes', 'woocommerce' ); ?></a>
|
||||
</div>
|
||||
</td>
|
||||
<td class="wc-shipping-zone-methods"><a class="wc-shipping-zone-add-method button" href="#"><?php esc_html_e( 'Add a shipping method', 'woocommerce' ); ?></a></td>
|
||||
<td class="wc-shipping-zone-actions">
|
||||
<a class="wc-shipping-zone-delete" href="#"><?php _e( 'Delete', 'woocommerce' ); ?></a><a class="wc-shipping-zone-edit" href="#"><?php _e( 'Edit', 'woocommerce' ); ?></a>
|
||||
</td>
|
||||
|
|
|
@ -3054,9 +3054,10 @@ class WC_AJAX {
|
|||
}
|
||||
|
||||
$zone_data = array_intersect_key( $data, array(
|
||||
'zone_id' => 1,
|
||||
'zone_name' => 1,
|
||||
'zone_order' => 1
|
||||
'zone_id' => 1,
|
||||
'zone_name' => 1,
|
||||
'zone_order' => 1,
|
||||
'zone_locations' => 1
|
||||
) );
|
||||
|
||||
if ( isset( $zone_data['zone_id'] ) ) {
|
||||
|
@ -3070,12 +3071,33 @@ class WC_AJAX {
|
|||
$zone->set_zone_order( $zone_data['zone_order'] );
|
||||
}
|
||||
|
||||
if ( isset( $zone_data['zone_locations'] ) ) {
|
||||
$locations = array_filter( array_map( 'wc_clean', (array) $zone_data['zone_locations'] ) );
|
||||
$zone->clear_locations();
|
||||
foreach ( $locations as $location ) {
|
||||
// Each posted location will be in the format type:code
|
||||
$location_parts = explode( ':', $location );
|
||||
switch ( $location_parts[0] ) {
|
||||
case 'state' :
|
||||
$zone->add_location( $location_parts[1] . ':' . $location_parts[2], 'state' );
|
||||
break;
|
||||
case 'country' :
|
||||
$zone->add_location( $location_parts[1], 'country' );
|
||||
break;
|
||||
case 'continent' :
|
||||
$zone->add_location( $location_parts[1], 'continent' );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$zone->save();
|
||||
}
|
||||
}
|
||||
|
||||
wp_send_json_success( array(
|
||||
'zones' => WC_Shipping_Zones::get_zones(),
|
||||
'test' => print_r($_POST['changes'], true)
|
||||
) );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -134,7 +134,7 @@ class WC_Shipping_Zone {
|
|||
* @return boolean
|
||||
*/
|
||||
public function is_valid_location_type( $type ) {
|
||||
return in_array( $type, array( 'postcode', 'state' ) );
|
||||
return in_array( $type, array( 'postcode', 'state', 'country', 'continent' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -143,7 +143,7 @@ class WC_Shipping_Zone {
|
|||
* @param string $type state or postcode
|
||||
*/
|
||||
public function add_location( $code, $type ) {
|
||||
if ( is_valid_location_type( $type ) ) {
|
||||
if ( $this->is_valid_location_type( $type ) ) {
|
||||
$location = array(
|
||||
'code' => wc_clean( $code ),
|
||||
'type' => wc_clean( $type )
|
||||
|
@ -153,12 +153,20 @@ class WC_Shipping_Zone {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all locations for this zone.
|
||||
*/
|
||||
public function clear_locations() {
|
||||
$this->data['zone_locations'] = array();
|
||||
$this->_locations_changed = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set locations
|
||||
* @param array $locations Array of locations
|
||||
*/
|
||||
public function set_locations( $locations = array() ) {
|
||||
$this->data['zone_locations'] = array();
|
||||
$this->clear_locations();
|
||||
|
||||
foreach ( $locations as $location ) {
|
||||
$this->add_location( $location['code'], $location['type'] );
|
||||
|
@ -174,7 +182,7 @@ class WC_Shipping_Zone {
|
|||
private function read_zone_locations( $zone_id ) {
|
||||
global $wpdb;
|
||||
|
||||
if ( $locations = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_shipping_zone_locations WHERE zone_id = %d LIMIT 1;", $zone_id ) ) ) {
|
||||
if ( $locations = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_shipping_zone_locations WHERE zone_id = %d;", $zone_id ) ) ) {
|
||||
foreach ( $locations as $location ) {
|
||||
$this->add_location( $location->location_code, $location->location_type );
|
||||
}
|
||||
|
@ -210,6 +218,7 @@ class WC_Shipping_Zone {
|
|||
if ( ! $this->get_zone_id() || ! $this->_locations_changed ) {
|
||||
return false;
|
||||
}
|
||||
global $wpdb;
|
||||
$wpdb->delete( $wpdb->prefix . 'woocommerce_shipping_zone_locations', array( 'zone_id' => $this->get_zone_id() ) );
|
||||
|
||||
foreach ( $this->get_zone_locations() as $location ) {
|
||||
|
|
Loading…
Reference in New Issue