Work on shipping zone screens

This commit is contained in:
Mike Jolley 2016-09-24 03:22:57 +01:00
parent 0898060f49
commit 83cc0c7066
11 changed files with 292 additions and 382 deletions

File diff suppressed because one or more lines are too long

View File

@ -2268,23 +2268,114 @@ table.wc_shipping {
}
}
}
.wc-shipping-zone-settings {
th {
padding: 24px 24px 24px 0;
}
td.forminp {
padding: 15px 10px;
input, textarea {
padding: 8px;
width: 448px;
max-width: 100% !important;
}
.wc-shipping-zone-region-select {
width: 448px;
max-width: 100% !important;
.select2-choices {
padding: 8px 8px 4px;
border-color: #DDDDDD;
min-height: 0;
line-height: 1;
input {
padding: 0;
}
li {
margin: 0 4px 4px 0;
}
}
}
.select2-container-active {
.select2-choices {
border-color: #777;
}
}
}
.wc-shipping-zone-postcodes-toggle {
margin: 0;
font-size: 0.9em;
text-decoration: underline;
}
.wc-shipping-zone-postcodes-toggle + .wc-shipping-zone-postcodes {
display:none;
}
.wc-shipping-zone-postcodes {
textarea {
margin: 10px 0;
}
.description {
font-size: 0.9em;
color: #999;
}
}
}
table.wc-shipping-zones, table.wc-shipping-zone-methods, table.wc-shipping-classes {
td, th {
vertical-align: top;
line-height: 24px;
padding: 1em;
padding: 1em !important;
font-size: 14px;
background: #fff;
li {
line-height: 24px;
font-size: 14px;
}
.woocommerce-help-tip {
margin: 0 !important;
}
}
thead {
th {
vertical-align: middle;
}
.wc-shipping-zone-sort {
text-align: center;
}
}
tbody td {
padding: 1.5em 1em !important;
}
th.remove {
width: 1%;
}
td.remove {
a {
width: 2em;
padding: 1em 0 0 0;
height: 0;
display: inline-block;
overflow: hidden;
font-size: 1.25em;
position: relative;
&::before {
@include icon_dashicons( '\f335' );
position: absolute;
top:0;
right: 0;
line-height: 24px;
}
&:hover {
&::before {
color: $red;
}
}
}
}
td.wc-shipping-zones-blank-state, td.wc-shipping-zone-method-blank-state {
background: #f7f1f6 !important;
overflow: hidden;
position: relative;
padding: 7.5em 7.5%;
padding: 7.5em 7.5% !important;
border-bottom: 2px solid #eee2ec;
p, li {
color: #a46497;
@ -2403,22 +2494,6 @@ table.wc-shipping-zones, table.wc-shipping-zone-methods, table.wc-shipping-class
a.wc-shipping-zone-delete:hover, a.wc-shipping-class-delete:hover {
color: red;
}
.wc-shipping-zone-postcodes-toggle {
float: right;
margin: 0;
font-size: 0.9em;
text-decoration: underline;
}
.wc-shipping-zone-postcodes {
display:none;
textarea {
margin-top: 1em;
}
.description {
font-size: 0.9em;
color: #999;
}
}
}
.wc-shipping-class-count {
text-align: center;
@ -2439,18 +2514,9 @@ table.wc-shipping-zones, table.wc-shipping-zone-methods, table.wc-shipping-class
li::before {
content: ', ';
}
li:first-child::before,
li:nth-child( 2 )::before {
li:first-child::before {
content: '';
}
li:first-child {
display: block;
}
li.wc-shipping-zone-methods-add-row {
position: absolute;
right: 0;
top: 0;
}
}
.add_shipping_method {
display: block;

View File

@ -21,19 +21,24 @@
this.trigger( 'change:methods' );
},
save: function() {
if ( _.size( this.changes ) ) {
$.post( ajaxurl + ( ajaxurl.indexOf( '?' ) > 0 ? '&' : '?' ) + 'action=woocommerce_shipping_zone_methods_save_changes', {
wc_shipping_zones_nonce : data.wc_shipping_zones_nonce,
changes : this.changes,
zone_name : $('#zone_name').val(),
zone_locations : $('#zone_locations').val(),
zone_postcodes : $('#zone_postcodes').val(),
zone_id : data.zone_id
}, this.onSaveResponse, 'json' );
} else {
shippingMethod.trigger( 'saved:methods' );
}
},
onSaveResponse: function( response, textStatus ) {
if ( 'success' === textStatus ) {
if ( response.success ) {
if ( response.data.zone_id !== data.zone_id ) {
data.zone_id = response.data.zone_id;
if ( window.history.pushState ) {
window.history.pushState({}, '', 'admin.php?page=wc-settings&tab=shipping&zone_id=' + response.data.zone_id );
}
}
shippingMethod.set( 'methods', response.data.methods );
shippingMethod.trigger( 'change:methods' );
shippingMethod.changes = {};
@ -57,12 +62,21 @@
$( window ).on( 'beforeunload', { view: this }, this.unloadConfirmation );
$save_button.on( 'click', { view: this }, this.onSubmit );
// Settings modals
$( document.body ).on( 'input', '#zone_name', { view: this }, this.onUpdateZoneName );
$( document.body ).on( 'input', '#zone_locations, #zone_postcodes', { view: this }, this.onUpdateZone );
$( document.body ).on( 'click', '.wc-shipping-zone-method-settings', { view: this }, this.onConfigureShippingMethod );
$( document.body ).on( 'click', '.wc-shipping-zone-add-method', { view: this }, this.onAddShippingMethod );
$( document.body ).on( 'wc_backbone_modal_response', this.onConfigureShippingMethodSubmitted );
$( document.body ).on( 'wc_backbone_modal_response', this.onAddShippingMethodSubmitted );
$( document.body ).on( 'change', '.wc-shipping-zone-method-selector select', this.onChangeShippingMethodSelector );
$( document.body ).on( 'click', '.wc-shipping-zone-postcodes-toggle', this.onTogglePostcodes );
},
onUpdateZone: function() {
shippingMethod.trigger( 'change:methods' );
},
onUpdateZoneName: function() {
$('.wc-shipping-zone-name').text( $( this ).val() );
shippingMethod.trigger( 'change:methods' );
},
block: function() {
$( this.el ).block({
@ -300,6 +314,12 @@
zone_id : data.zone_id
}, function( response, textStatus ) {
if ( 'success' === textStatus && response.success ) {
if ( response.data.zone_id !== data.zone_id ) {
data.zone_id = response.data.zone_id;
if ( window.history.pushState ) {
window.history.pushState({}, '', 'admin.php?page=wc-settings&tab=shipping&zone_id=' + response.data.zone_id );
}
}
// Trigger save if there are changes, or just re-render
if ( _.size( shippingMethodView.model.changes ) ) {
shippingMethodView.model.save();
@ -319,6 +339,12 @@
$( this ).parent().find( '.wc-shipping-zone-method-description' ).remove();
$( this ).after( '<p class="wc-shipping-zone-method-description">' + description + '</p>' );
$( this ).closest( 'article' ).height( $( this ).parent().height() );
},
onTogglePostcodes: function( event ) {
event.preventDefault();
var $tr = $( this ).closest( 'tr');
$tr.find( '.wc-shipping-zone-postcodes' ).show();
$tr.find( '.wc-shipping-zone-postcodes-toggle' ).hide();
}
} ),
shippingMethod = new ShippingMethod({

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
/* global wc_enhanced_select_params, shippingZonesLocalizeScript, ajaxurl */
/* global shippingZonesLocalizeScript, ajaxurl */
( function( $, data, wp, ajaxurl ) {
$( function() {
var $table = $( '.wc-shipping-zones' ),
@ -6,14 +6,6 @@
$save_button = $( '.wc-shipping-zone-save' ),
$row_template = wp.template( 'wc-shipping-zone-row' ),
$blank_template = wp.template( 'wc-shipping-zone-row-blank' ),
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({
@ -88,10 +80,7 @@
$tbody.on( 'sortupdate', { view: this }, this.updateModelOnSort );
$( window ).on( 'beforeunload', { view: this }, this.unloadConfirmation );
$save_button.on( 'click', { view: this }, this.onSubmit );
$( document.body ).on( 'click', '.add_shipping_method:not(.disabled)', { view: this }, this.onAddShippingMethod );
$( document.body ).on( 'click', '.wc-shipping-zone-add', { view: this }, this.onAddNewRow );
$( document.body ).on( 'wc_backbone_modal_response', this.onAddShippingMethodSubmitted );
$( document.body ).on( 'change', '.wc-shipping-zone-method-selector select', this.onChangeShippingMethodSelector );
},
block: function() {
$( this.el ).block({
@ -137,57 +126,9 @@
var view = this;
var $tr = view.$el.find( 'tr[data-id="' + rowData.zone_id + '"]');
// Select values in region select
_.each( rowData.zone_locations, function( location ) {
if ( 'string' === jQuery.type( location ) ) {
$tr.find( 'option[value="' + location + '"]' ).prop( 'selected', true );
} else {
if ( 'postcode' === location.type ) {
var postcode_field = $tr.find( '.wc-shipping-zone-postcodes :input' );
if ( postcode_field.val() ) {
postcode_field.val( postcode_field.val() + '\n' + location.code );
} else {
postcode_field.val( location.code );
}
$tr.find( '.wc-shipping-zone-postcodes' ).show();
$tr.find( '.wc-shipping-zone-postcodes-toggle' ).hide();
} else {
$tr.find( 'option[value="' + location.type + ':' + location.code + '"]' ).prop( 'selected', true );
}
}
} );
if ( rowData.zone_postcodes ) {
_.each( rowData.zone_postcodes, function( location ) {
var postcode_field = $tr.find( '.wc-shipping-zone-postcodes :input' );
if ( postcode_field.val() ) {
postcode_field.val( postcode_field.val() + '\n' + location.code );
} else {
postcode_field.val( location.code );
}
$tr.find( '.wc-shipping-zone-postcodes' ).show();
$tr.find( '.wc-shipping-zone-postcodes-toggle' ).hide();
} );
}
// List shipping methods
view.renderShippingMethods( rowData.zone_id, rowData.shipping_methods );
// Make the row function
$tr.find( '.view' ).show();
$tr.find( '.edit' ).hide();
$tr.find( '.wc-shipping-zone-edit' ).on( 'click', { view: this }, this.onEditRow );
$tr.find( '.wc-shipping-zone-cancel-edit' ).on( 'click', { view: this }, this.onCancelEditRow );
$tr.find( '.wc-shipping-zone-delete' ).on( 'click', { view: this }, this.onDeleteRow );
$tr.find( '.wc-shipping-zone-postcodes-toggle' ).on( 'click', { view: this }, this.onTogglePostcodes );
// Editing?
if ( true === rowData.editing ) {
$tr.addClass( 'editing' );
$tr.find( '.wc-shipping-zone-edit' ).trigger( 'click' );
}
},
initRows: function() {
// Stripe
@ -196,7 +137,6 @@
} else {
$table.find( 'tbody.wc-shipping-zone-rows' ).next( 'tbody' ).find( 'tr' ).removeClass( 'odd' );
}
// Tooltips
$( '#tiptip_holder' ).removeAttr( 'style' );
$( '#tiptip_arrow' ).removeAttr( 'style' );
@ -231,73 +171,6 @@
event.data.view.model.save();
event.preventDefault();
},
onAddNewRow: function( event ) {
event.preventDefault();
var view = event.data.view,
model = view.model,
zones = _.indexBy( model.get( 'zones' ), 'zone_id' ),
changes = {},
size = _.size( zones ),
newRow = _.extend( {}, data.default_zone, {
zone_id : 'new-' + size + '-' + Date.now(),
editing : true
} );
$( '.wc-shipping-zones-blank-state' ).closest( 'tr' ).remove();
newRow.zone_order = 1 + _.max(
_.pluck( zones, 'zone_order' ),
function ( val ) {
// Cast them all to integers, because strings compare funky. Sighhh.
return parseInt( val, 10 );
}
);
changes[ newRow.zone_id ] = newRow;
model.logChanges( changes );
view.renderRow( newRow );
view.initRows();
},
onTogglePostcodes: function( event ) {
event.preventDefault();
var $tr = $( this ).closest( 'tr');
$tr.find( '.wc-shipping-zone-postcodes' ).show();
$tr.find( '.wc-shipping-zone-postcodes-toggle' ).hide();
},
onEditRow: function( event ) {
event.preventDefault();
event.data.view.model.trigger( 'change:zones' );
$( this ).closest('tr').addClass( 'editing' );
$( this ).closest('tr').find('.view').hide();
$( this ).closest('tr').find('.edit').show();
$( '.wc-shipping-zone-region-select:not(.enhanced)' ).select2( select2_args );
$( '.wc-shipping-zone-region-select:not(.enhanced)' ).addClass('enhanced');
var addShippingMethod = $( this ).closest('tr').find('.add_shipping_method');
addShippingMethod.addClass( 'disabled' );
addShippingMethod.tipTip({ 'attribute': 'data-disabled-tip', 'fadeIn': 50, 'fadeOut': 50, 'delay': 50 });
},
onCancelEditRow: function( event ) {
var view = event.data.view,
model = view.model,
row = $( this ).closest('tr'),
zone_id = row.data('id'),
zones = _.indexBy( model.get( 'zones' ), 'zone_id' );
event.preventDefault();
model.discardChanges( zone_id );
if ( zones[ zone_id ] ) {
zones[ zone_id ].editing = false;
row.after( view.rowTemplate( zones[ zone_id ] ) );
view.initRow( zones[ zone_id ] );
}
row.remove();
view.initRows();
},
onDeleteRow: function( event ) {
var view = event.data.view,
model = view.model,
@ -374,44 +247,6 @@
if ( _.size( changes ) ) {
model.logChanges( changes );
}
},
onAddShippingMethod: function( event ) {
var zone_id = $( this ).closest('tr').data('id');
event.preventDefault();
$( this ).WCBackboneModal({
template : 'wc-modal-add-shipping-method',
variable : {
zone_id : zone_id
}
});
$( '.wc-shipping-zone-method-selector select' ).change();
},
onAddShippingMethodSubmitted: function( event, target, posted_data ) {
if ( 'wc-modal-add-shipping-method' === target ) {
shippingZoneView.block();
// Add method to zone via ajax call
$.post( ajaxurl + ( ajaxurl.indexOf( '?' ) > 0 ? '&' : '?' ) + 'action=woocommerce_shipping_zone_add_method', {
wc_shipping_zones_nonce : data.wc_shipping_zones_nonce,
method_id : posted_data.add_method_id,
zone_id : posted_data.zone_id
}, function( response, textStatus ) {
if ( 'success' === textStatus && response.success ) {
// Method was added. Render methods.
shippingZoneView.renderShippingMethods( posted_data.zone_id, response.data.methods );
}
shippingZoneView.unblock();
}, 'json' );
}
},
onChangeShippingMethodSelector: function() {
var description = $( this ).find( 'option:selected' ).data( 'description' );
$( this ).parent().find( '.wc-shipping-zone-method-description' ).remove();
$( this ).after( '<p class="wc-shipping-zone-method-description">' + description + '</p>' );
$( this ).closest( 'article' ).height( $( this ).parent().height() );
}
} ),
shippingZone = new ShippingZone({
@ -431,55 +266,5 @@
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 );

File diff suppressed because one or more lines are too long

View File

@ -190,7 +190,7 @@ class WC_Settings_Shipping extends WC_Settings_Page {
if ( isset( $_REQUEST['zone_id'] ) ) {
$hide_save_button = true;
$this->zone_methods_screen( absint( $_REQUEST['zone_id'] ) );
$this->zone_methods_screen( wc_clean( $_REQUEST['zone_id'] ) );
} elseif ( isset( $_REQUEST['instance_id'] ) ) {
$this->instance_settings_screen( absint( $_REQUEST['instance_id'] ) );
} else {
@ -204,16 +204,37 @@ class WC_Settings_Shipping extends WC_Settings_Page {
* @param int $zone_id
*/
protected function zone_methods_screen( $zone_id ) {
$wc_shipping = WC_Shipping ::instance();
if ( 'new' === $zone_id ) {
$zone = new WC_Shipping_Zone();
$zone->set_zone_name( __( 'New Zone', 'woocommerce' ) );
} else {
$zone = WC_Shipping_Zones::get_zone( $zone_id );
$shipping_methods = $wc_shipping->get_shipping_methods();
}
if ( ! $zone ) {
wp_die( __( 'Zone does not exist!', 'woocommerce' ) );
}
$allowed_countries = WC()->countries->get_allowed_countries();
$wc_shipping = WC_Shipping ::instance();
$shipping_methods = $wc_shipping->get_shipping_methods();
$continents = WC()->countries->get_continents();
// Prepare locations
$locations = array();
$postcodes = array();
foreach ( $zone->get_zone_locations() as $location ) {
if ( 'postcode' === $location->type ) {
$postcodes[] = $location->code;
} else {
$locations[] = $location->type . ':' . $location->code;
}
}
wp_localize_script( 'wc-shipping-zone-methods', 'shippingZoneMethodsLocalizeScript', array(
'methods' => $zone->get_shipping_methods(),
'zone_name' => $zone->get_zone_name(),
'zone_id' => $zone->get_zone_id(),
'wc_shipping_zones_nonce' => wp_create_nonce( 'wc_shipping_zones_nonce' ),
'strings' => array(

View File

@ -6,36 +6,93 @@ if ( ! defined( 'ABSPATH' ) ) {
<h2>
<a href="<?php echo admin_url( 'admin.php?page=wc-settings&tab=shipping' ); ?>"><?php _e( 'Shipping Zones', 'woocommerce' ); ?></a> &gt;
<?php echo esc_html( $zone->get_zone_name() ); ?>
<?php echo wc_help_tip( __( 'The following shipping methods apply to customers with shipping addresses within this zone.', 'woocommerce' ) ); ?>
<span class="wc-shipping-zone-name"><?php echo esc_html( $zone->get_zone_name() ); ?></span>
</h2>
<?php do_action( 'woocommerce_shipping_zone_before_methods_table' ); ?>
<table class="wc-shipping-zone-methods widefat">
<table class="form-table wc-shipping-zone-settings">
<tbody>
<tr valign="top" class="">
<th scope="row" class="titledesc">
<label for="zone_name"><?php esc_html_e( 'Zone Name', 'woocommerce' ); ?></label>
<?php echo wc_help_tip( __( 'This is the name of the zone for your reference.', 'woocommerce' ) ); ?>
</th>
<td class="forminp">
<input type="text" name="zone_name" id="zone_name" value="<?php echo esc_attr( $zone->get_zone_name() ); ?>" placeholder="<?php _e( 'Zone Name', 'woocommerce' ); ?>">
</td>
</tr>
<tr valign="top" class="">
<th scope="row" class="titledesc">
<label for="zone_locations"><?php esc_html_e( 'Zone Region(s)', 'woocommerce' ); ?></label>
<?php echo wc_help_tip( __( 'These are regions inside this zone. Customers will be matched against these regions.', 'woocommerce' ) ); ?>
</th>
<td class="forminp">
<select multiple="multiple" id="zone_locations" name="zone_locations" data-placeholder="<?php _e( 'Select regions within this zone', 'woocommerce' ); ?>" class="wc-shipping-zone-region-select chosen_select">
<?php
foreach ( $continents as $continent_code => $continent ) {
echo '<option value="continent:' . esc_attr( $continent_code ) . '" ' . selected( in_array( "continent:$continent_code", $locations ), true, false ) . ' 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 ) . '" ' . selected( in_array( "country:$country_code", $locations ), true, false ) . ' alt="' . esc_attr( $continent['name'] ) . '">' . esc_html( '&nbsp;&nbsp; ' . $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 ) . '" ' . selected( in_array( "state:$country_code:$state_code", $locations ), true, false ) . ' alt="' . esc_attr( $continent['name'] . ' ' . $allowed_countries[ $country_code ] ) . '">' . esc_html( '&nbsp;&nbsp;&nbsp;&nbsp; ' . $state_name ) . '</option>';
}
}
}
}
?>
</select>
<?php if ( empty( $postcodes ) ) : ?>
<a class="wc-shipping-zone-postcodes-toggle" href="#"><?php _e( 'Limit to specific ZIP/postcodes', 'woocommerce' ); ?></a>
<?php endif; ?>
<div class="wc-shipping-zone-postcodes">
<textarea name="zone_postcodes" id="zone_postcodes" placeholder="<?php esc_attr_e( 'List 1 postcode per line', 'woocommerce' ); ?>" class="input-text large-text" cols="25" rows="5"><?php echo esc_textarea( implode( "\n", $postcodes ) ); ?></textarea>
<span class="description"><?php _e( 'Postcodes containing wildcards (e.g. CB23*) and fully numeric ranges (e.g. <code>90210...99000</code>) are also supported.', 'woocommerce' ) ?></span>
</div>
</td>
</tr>
<tr valign="top" class="">
<th scope="row" class="titledesc">
<label><?php esc_html_e( 'Shipping Method(s)', 'woocommerce' ); ?></label>
<?php echo wc_help_tip( __( 'The following shipping methods apply to customers with shipping addresses within this zone.', 'woocommerce' ) ); ?>
</th>
<td class="">
<table class="wc-shipping-zone-methods widefat">
<thead>
<tr>
<th class="wc-shipping-zone-method-sort"><?php echo wc_help_tip( __( 'Drag and drop to re-order your shipping methods. This is the order in which they will display during checkout.', 'woocommerce' ) ); ?></th>
<th class="wc-shipping-zone-method-title"><?php esc_html_e( 'Title', 'woocommerce' ); ?></th>
<th class="wc-shipping-zone-method-enabled"><?php esc_html_e( 'Enabled', 'woocommerce' ); ?></th>
<th class="wc-shipping-zone-method-type"><?php esc_html_e( 'Type', 'woocommerce' ); ?></th>
<th class="wc-shipping-zone-method-enabled"><?php esc_html_e( 'Enabled', 'woocommerce' ); ?></th>
<th class="wc-shipping-zone-method-description"><?php esc_html_e( 'Description', 'woocommerce' ); ?></th>
<th class="remove">&nbsp;</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="5">
<input type="submit" name="save" class="button button-primary wc-shipping-zone-method-save" value="<?php esc_attr_e( 'Save changes', 'woocommerce' ); ?>" disabled />
<input type="submit" class="button button-secondary wc-shipping-zone-add-method" value="<?php esc_attr_e( 'Add shipping method', 'woocommerce' ); ?>" />
<td colspan="6">
<input type="submit" class="button wc-shipping-zone-add-method" value="<?php esc_attr_e( 'Add shipping method', 'woocommerce' ); ?>" />
</td>
</tr>
</tfoot>
<tbody class="wc-shipping-zone-method-rows"></tbody>
</table>
</td>
</tr>
</tbody>
</table>
<p class="submit">
<input type="submit" name="submit" id="submit" class="button button-primary button-hero wc-shipping-zone-method-save" value="<?php esc_attr_e( 'Save changes', 'woocommerce' ); ?>" disabled />
</p>
<script type="text/html" id="tmpl-wc-shipping-zone-method-row-blank">
<tr>
<td class="wc-shipping-zone-method-blank-state" colspan="5">
<td class="wc-shipping-zone-method-blank-state" colspan="6">
<p class="main"><?php _e( 'Add shipping methods to this zone', 'woocommerce' ); ?></p>
<p><?php _e( 'You can add multiple shipping methods within this zone. Only customers within the zone will see them.', 'woocommerce' ); ?></p>
<p><?php _e( 'Click "Add shipping method" to get started.', 'woocommerce' ); ?></p>
@ -48,13 +105,11 @@ if ( ! defined( 'ABSPATH' ) ) {
<td width="1%" class="wc-shipping-zone-method-sort"></td>
<td class="wc-shipping-zone-method-title">
<a class="wc-shipping-zone-method-settings" href="admin.php?page=wc-settings&amp;tab=shipping&amp;instance_id={{ data.instance_id }}">{{ data.title }}</a>
<div class="row-actions">
<a class="wc-shipping-zone-method-settings" href="admin.php?page=wc-settings&amp;tab=shipping&amp;instance_id={{ data.instance_id }}"><?php _e( 'Settings', 'woocommerce' ); ?></a> | <a href="#" class="wc-shipping-zone-method-delete"><?php _e( 'Remove', 'woocommerce' ); ?></a>
</div>
</td>
<td width="1%" class="wc-shipping-zone-method-enabled"><a href="#">{{{ data.enabled_icon }}}</a></td>
<td class="wc-shipping-zone-method-type">{{ data.method_title }}</td>
<td width="1%" class="wc-shipping-zone-method-enabled"><a href="#">{{{ data.enabled_icon }}}</a></td>
<td class="wc-shipping-zone-method-description">{{ data.method_description }}</td>
<td class="remove"><a href="#" class="wc-shipping-zone-method-delete"><?php _e( 'Remove', 'woocommerce' ); ?></a></td>
</tr>
</script>

View File

@ -6,9 +6,9 @@ if ( ! defined( 'ABSPATH' ) ) {
<h2>
<?php _e( 'Shipping Zones', 'woocommerce' ); ?>
<?php echo wc_help_tip( __( 'A shipping zone is a geographic region where a certain set of shipping methods and rates apply.', 'woocommerce' ) . ' ' . __( 'WooCommerce will automatically choose the correct shipping zone based on your customer&lsquo;s shipping address and present the shipping methods within that zone to them.', 'woocommerce' ) ); ?>
<a href="<?php echo admin_url( 'admin.php?page=wc-settings&tab=shipping&zone_id=new' ); ?>" class="page-title-action"><?php esc_html_e( 'Add shipping zone', 'woocommerce' ); ?></a>
</h2>
<p><?php echo __( 'A shipping zone is a geographic region where a certain set of shipping methods are offered.', 'woocommerce' ) . ' ' . __( 'WooCommerce will match a customer to a single zone using their shipping address and present the shipping methods within that zone to them.', 'woocommerce' ); ?></p>
<table class="wc-shipping-zones widefat">
<thead>
<tr>
@ -16,13 +16,13 @@ if ( ! defined( 'ABSPATH' ) ) {
<th class="wc-shipping-zone-name"><?php esc_html_e( 'Zone Name', 'woocommerce' ); ?></th>
<th class="wc-shipping-zone-region"><?php esc_html_e( 'Region(s)', 'woocommerce' ); ?></th>
<th class="wc-shipping-zone-methods"><?php esc_html_e( 'Shipping Method(s)', 'woocommerce' ); ?></th>
<th class="remove">&nbsp;</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="4">
<td colspan="5">
<input type="submit" name="save" class="button button-primary wc-shipping-zone-save" value="<?php esc_attr_e( 'Save changes', 'woocommerce' ); ?>" disabled />
<a class="button button-secondary wc-shipping-zone-add" href="#"><?php esc_html_e( 'Add shipping zone', 'woocommerce' ); ?></a>
</td>
</tr>
</tfoot>
@ -32,14 +32,10 @@ if ( ! defined( 'ABSPATH' ) ) {
<td width="1%" class="wc-shipping-zone-worldwide"></td>
<td class="wc-shipping-zone-name">
<a href="<?php echo esc_url( admin_url( 'admin.php?page=wc-settings&tab=shipping&zone_id=0' ) ); ?>"><?php esc_html_e( 'Rest of the World', 'woocommerce' ); ?></a>
<div class="row-actions">
<a href="admin.php?page=wc-settings&amp;tab=shipping&amp;zone_id={{ data.zone_id }}"><?php _e( 'View', 'woocommerce' ); ?></a>
</div>
</td>
<td class="wc-shipping-zone-region"><?php esc_html_e( 'This zone is used for shipping addresses that aren&lsquo;t included in any other shipping zone. Adding shipping methods to this zone is optional.', 'woocommerce' ); ?></td>
<td class="wc-shipping-zone-region"><?php _e( 'This zone is <b>optionally</b> used for regions that are not included in any other shipping zone.', 'woocommerce' ); ?></td>
<td class="wc-shipping-zone-methods">
<ul>
<li class="wc-shipping-zone-methods-add-row"><a href="#" class="add_shipping_method tips" data-tip="<?php esc_attr_e( 'Add shipping method', 'woocommerce' ); ?>" data-disabled-tip="<?php esc_attr_e( 'Save changes to continue adding shipping methods to this zone', 'woocommerce' ); ?>"><?php _e( 'Add shipping method', 'woocommerce' ); ?></a></li>
<?php
$worldwide = new WC_Shipping_Zone( 0 );
$methods = $worldwide->get_shipping_methods();
@ -56,6 +52,7 @@ if ( ! defined( 'ABSPATH' ) ) {
?>
</ul>
</td>
<td class="remove"></td>
</tr>
</tbody>
</table>
@ -63,7 +60,7 @@ if ( ! defined( 'ABSPATH' ) ) {
<script type="text/html" id="tmpl-wc-shipping-zone-row-blank">
<?php if ( 0 === $method_count ) : ?>
<tr>
<td class="wc-shipping-zones-blank-state" colspan="4">
<td class="wc-shipping-zones-blank-state" colspan="5">
<p class="main"><?php _e( 'A shipping zone is a geographic region where a certain set of shipping methods and rates apply.', 'woocommerce' ); ?></p>
<p><?php _e( 'For example:', 'woocommerce' ); ?></p>
<ul>
@ -82,55 +79,15 @@ if ( ! defined( 'ABSPATH' ) ) {
<tr data-id="{{ data.zone_id }}">
<td width="1%" class="wc-shipping-zone-sort"></td>
<td class="wc-shipping-zone-name">
<div class="view">
<a href="admin.php?page=wc-settings&amp;tab=shipping&amp;zone_id={{ data.zone_id }}">{{ data.zone_name }}</a>
<div class="row-actions">
<a href="admin.php?page=wc-settings&amp;tab=shipping&amp;zone_id={{ data.zone_id }}"><?php _e( 'View', 'woocommerce' ); ?></a> | <a class="wc-shipping-zone-edit" href="#"><?php _e( 'Edit', 'woocommerce' ); ?></a> | <a href="#" class="wc-shipping-zone-delete"><?php _e( 'Remove', 'woocommerce' ); ?></a>
</div>
</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 class="row-actions">
<a class="wc-shipping-zone-cancel-edit" href="#"><?php _e( 'Cancel changes', 'woocommerce' ); ?></a>
</div>
</div>
</td>
<td class="wc-shipping-zone-region">
<div class="view">{{ data.formatted_zone_location }}</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( '&nbsp;&nbsp; ' . $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( '&nbsp;&nbsp;&nbsp;&nbsp; ' . $state_name ) . '</option>';
}
}
}
}
?>
</select>
<a class="wc-shipping-zone-postcodes-toggle" href="#"><?php _e( 'Limit to specific ZIP/postcodes', 'woocommerce' ); ?></a>
<div class="wc-shipping-zone-postcodes">
<textarea name="zone_postcodes[{{ data.zone_id }}]" data-attribute="zone_postcodes" placeholder="<?php esc_attr_e( 'List 1 postcode per line', 'woocommerce' ); ?>" class="input-text large-text" cols="25" rows="5"></textarea>
<span class="description"><?php _e( 'Postcodes containing wildcards (e.g. CB23*) and fully numeric ranges (e.g. <code>90210...99000</code>) are also supported.', 'woocommerce' ) ?></span>
</div>
</div>
{{ data.formatted_zone_location }}
</td>
<td class="wc-shipping-zone-methods">
<div>
<ul>
<li class="wc-shipping-zone-methods-add-row"><a href="#" class="add_shipping_method tips" data-tip="<?php esc_attr_e( 'Add shipping method', 'woocommerce' ); ?>" data-disabled-tip="<?php esc_attr_e( 'Save changes to continue adding shipping methods to this zone', 'woocommerce' ); ?>"><?php _e( 'Add shipping method', 'woocommerce' ); ?></a></li>
</ul>
</div>
<div><ul></ul></div>
</td>
<td class="remove"><a href="#" class="wc-shipping-zone-delete"><?php _e( 'Remove', 'woocommerce' ); ?></a></td>
</tr>
</script>

View File

@ -2875,51 +2875,16 @@ class WC_AJAX {
$zone_data = array_intersect_key( $data, array(
'zone_id' => 1,
'zone_name' => 1,
'zone_order' => 1,
'zone_locations' => 1,
'zone_postcodes' => 1,
'zone_order' => 1
) );
if ( isset( $zone_data['zone_id'] ) ) {
$zone = new WC_Shipping_Zone( $zone_data['zone_id'] );
if ( isset( $zone_data['zone_name'] ) ) {
$zone->set_zone_name( $zone_data['zone_name'] );
}
if ( isset( $zone_data['zone_order'] ) ) {
$zone->set_zone_order( $zone_data['zone_order'] );
}
if ( isset( $zone_data['zone_locations'] ) ) {
$zone->clear_locations( array( 'state', 'country', 'continent' ) );
$locations = array_filter( array_map( 'wc_clean', (array) $zone_data['zone_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;
}
}
}
if ( isset( $zone_data['zone_postcodes'] ) ) {
$zone->clear_locations( 'postcode' );
$postcodes = array_filter( array_map( 'strtoupper', array_map( 'wc_clean', explode( "\n", $zone_data['zone_postcodes'] ) ) ) );
foreach ( $postcodes as $postcode ) {
$zone->add_location( $postcode, 'postcode' );
}
}
$zone->save();
}
}
@ -2949,13 +2914,13 @@ class WC_AJAX {
exit;
}
$zone_id = absint( $_POST['zone_id'] );
$zone = WC_Shipping_Zones::get_zone( $zone_id );
$zone_id = wc_clean( $_POST['zone_id'] );
$zone = new WC_Shipping_Zone( $zone_id );
$instance_id = $zone->add_shipping_method( wc_clean( $_POST['method_id'] ) );
wp_send_json_success( array(
'instance_id' => $instance_id,
'zone_id' => $zone_id,
'zone_id' => $zone->get_id(),
'methods' => $zone->get_shipping_methods(),
) );
}
@ -2964,7 +2929,7 @@ class WC_AJAX {
* Handle submissions from assets/js/wc-shipping-zone-methods.js Backbone model.
*/
public static function shipping_zone_methods_save_changes() {
if ( ! isset( $_POST['wc_shipping_zones_nonce'], $_POST['zone_id'], $_POST['changes'] ) ) {
if ( ! isset( $_POST['wc_shipping_zones_nonce'], $_POST['zone_id'] ) ) {
wp_send_json_error( 'missing_fields' );
exit;
}
@ -2981,9 +2946,41 @@ class WC_AJAX {
global $wpdb;
$zone_id = absint( $_POST['zone_id'] );
$zone_id = wc_clean( $_POST['zone_id'] );
$zone = new WC_Shipping_Zone( $zone_id );
$changes = $_POST['changes'];
$changes = isset( $_POST['changes'] ) ? $_POST['changes'] : array();
if ( isset( $_POST['zone_name'] ) ) {
$zone->set_zone_name( wc_clean( $_POST['zone_name'] ) );
}
if ( isset( $_POST['zone_locations'] ) ) {
$zone->clear_locations( array( 'state', 'country', 'continent' ) );
$locations = array_filter( array_map( 'wc_clean', (array) $_POST['zone_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;
}
}
}
if ( isset( $_POST['zone_postcodes'] ) ) {
$zone->clear_locations( 'postcode' );
$postcodes = array_filter( array_map( 'strtoupper', array_map( 'wc_clean', explode( "\n", $_POST['zone_postcodes'] ) ) ) );
foreach ( $postcodes as $postcode ) {
$zone->add_location( $postcode, 'postcode' );
}
}
foreach ( $changes as $instance_id => $data ) {
$method_id = $wpdb->get_var( $wpdb->prepare( "SELECT method_id FROM {$wpdb->prefix}woocommerce_shipping_zone_methods WHERE instance_id = %d", $instance_id ) );
@ -3015,7 +3012,10 @@ class WC_AJAX {
}
}
$zone->save();
wp_send_json_success( array(
'zone_id' => $zone->get_id(),
'methods' => $zone->get_shipping_methods(),
) );
}

View File

@ -455,7 +455,7 @@ class WC_Shipping_Zone extends WC_Data {
global $wpdb;
if ( null === $this->get_id() ) {
return 0;
$this->save();
}
$instance_id = 0;