Fix sorting and saving for methods
This commit is contained in:
parent
e13ba4f029
commit
9aa34330ac
File diff suppressed because one or more lines are too long
|
@ -2175,7 +2175,7 @@ table.wc-shipping-zones, table.wc-shipping-zone-methods {
|
|||
}
|
||||
}
|
||||
}
|
||||
tbody.wc-shipping-zone-rows {
|
||||
tbody.wc-shipping-zone-rows, .wc-shipping-zone-method-rows {
|
||||
tr:nth-child(odd) {
|
||||
td {
|
||||
background: #f9f9f9;
|
||||
|
|
|
@ -32,6 +32,9 @@
|
|||
}
|
||||
},
|
||||
addMethod: function() {
|
||||
if ( _.size( this.changes ) ) {
|
||||
this.save();
|
||||
}
|
||||
$.post( ajaxurl + '?action=woocommerce_shipping_zone_add_method', {
|
||||
wc_shipping_zones_nonce : data.wc_shipping_zones_nonce,
|
||||
method_id : $('select[name="add_method_id"]').val(),
|
||||
|
@ -186,7 +189,7 @@
|
|||
|
||||
_.each( methods, function( method ) {
|
||||
var old_position = parseInt( method.method_order, 10 );
|
||||
var new_position = parseInt( $table.find( 'tr[data-id="' + method.instance_id + '"]').index(), 10 );
|
||||
var new_position = parseInt( $table.find( 'tr[data-id="' + method.instance_id + '"]').index() + 1, 10 );
|
||||
|
||||
if ( old_position !== new_position ) {
|
||||
changes[ method.instance_id ] = _.extend( changes[ method.instance_id ] || {}, { method_order : new_position } );
|
||||
|
|
|
@ -1 +1 @@
|
|||
!function(a,b,c,d){a(function(){var e=a(".wc-shipping-zone-methods"),f=a(".wc-shipping-zone-method-rows"),g=a(".wc-shipping-zone-method-save"),h=c.template("wc-shipping-zone-method-row"),i=c.template("wc-shipping-zone-method-row-blank"),j=Backbone.Model.extend({changes:{},logChanges:function(a){var b=this.changes||{};_.each(a,function(a,c){b[c]=_.extend(b[c]||{instance_id:c},a)}),this.changes=b,this.trigger("change:methods")},save:function(){_.size(this.changes)?a.post(d+"?action=woocommerce_shipping_zone_methods_save_changes",{wc_shipping_zones_nonce:b.wc_shipping_zones_nonce,changes:this.changes,zone_id:b.zone_id},this.onSaveResponse,"json"):l.trigger("saved:methods")},addMethod:function(){a.post(d+"?action=woocommerce_shipping_zone_add_method",{wc_shipping_zones_nonce:b.wc_shipping_zones_nonce,method_id:a('select[name="add_method_id"]').val(),zone_id:b.zone_id},this.onAddResponse,"json")},onSaveResponse:function(a,c){"success"===c&&(a.success?(l.set("methods",a.data.methods),l.trigger("change:methods"),l.changes={},l.trigger("saved:methods")):window.alert(b.strings.save_failed))},onAddResponse:function(a,c){"success"===c&&(a.success&&a.data.instance_id?(l.set("methods",a.data.methods),l.trigger("change:methods"),l.changes={},l.trigger("saved:methods")):window.alert(b.strings.add_method_failed))}}),k=Backbone.View.extend({rowTemplate:h,initialize:function(){this.listenTo(this.model,"change:methods",this.setUnloadConfirmation),this.listenTo(this.model,"saved:methods",this.clearUnloadConfirmation),this.listenTo(this.model,"saved:methods",this.render),f.on("change",{view:this},this.updateModelOnChange),f.on("sortupdate",{view:this},this.updateModelOnSort),a(window).on("beforeunload",{view:this},this.unloadConfirmation),g.on("click",{view:this},this.onSubmit),a(".wc-shipping-zone-add-method").on("click",{view:this},this.onAdd)},block:function(){a(this.el).block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},unblock:function(){a(this.el).unblock()},render:function(){var b=_.indexBy(this.model.get("methods"),"instance_id"),c=this;this.$el.empty(),this.unblock(),_.size(b)?(b=_.sortBy(b,function(a){return parseInt(a.method_order,10)}),a.each(b,function(a,b){c.$el.append(c.rowTemplate(b))}),this.$el.find(".wc-shipping-zone-method-delete").on("click",{view:this},this.onDeleteRow)):c.$el.append(i),this.initTooltips()},initTooltips:function(){a("#tiptip_holder").removeAttr("style"),a("#tiptip_arrow").removeAttr("style"),a(".tips").tipTip({attribute:"data-tip",fadeIn:50,fadeOut:50,delay:50})},onSubmit:function(a){a.data.view.block(),a.data.view.model.save(),a.preventDefault()},onAdd:function(a){a.data.view.block(),a.data.view.model.addMethod(),a.preventDefault()},onDeleteRow:function(b){var c=b.data.view,d=c.model,e=_.indexBy(d.get("methods"),"instance_id"),f={},g=a(this).closest("tr").data("id");b.preventDefault(),delete e[g],f[g]=_.extend(f[g]||{},{deleted:"deleted"}),d.set("methods",e),d.logChanges(f),c.render()},setUnloadConfirmation:function(){this.needsUnloadConfirm=!0,g.removeAttr("disabled")},clearUnloadConfirmation:function(){this.needsUnloadConfirm=!1,g.attr("disabled","disabled")},unloadConfirmation:function(a){return a.data.view.needsUnloadConfirm?(a.returnValue=b.strings.unload_confirmation_msg,window.event.returnValue=b.strings.unload_confirmation_msg,b.strings.unload_confirmation_msg):void 0},updateModelOnChange:function(b){var c=b.data.view.model,d=a(b.target),e=d.closest("tr").data("id"),f=d.data("attribute"),g=d.val(),h=_.indexBy(c.get("methods"),"instance_id"),i={};h[e][f]!==g&&(i[e]={},i[e][f]=g,h[e][f]=g),c.logChanges(i)},updateModelOnSort:function(a){var b=a.data.view,c=b.model,d=_.indexBy(c.get("methods"),"instance_id"),f={};_.each(d,function(a){var b=parseInt(a.method_order,10),c=parseInt(e.find('tr[data-id="'+a.instance_id+'"]').index(),10);b!==c&&(f[a.instance_id]=_.extend(f[a.instance_id]||{},{method_order:c}))}),_.size(f)&&c.logChanges(f)}}),l=new j({methods:b.methods}),m=new k({model:l,el:f});m.render(),f.sortable({items:"tr",cursor:"move",axis:"y",handle:"td.wc-shipping-zone-method-sort",scrollSensitivity:40})})}(jQuery,shippingZoneMethodsLocalizeScript,wp,ajaxurl);
|
||||
!function(a,b,c,d){a(function(){var e=a(".wc-shipping-zone-methods"),f=a(".wc-shipping-zone-method-rows"),g=a(".wc-shipping-zone-method-save"),h=c.template("wc-shipping-zone-method-row"),i=c.template("wc-shipping-zone-method-row-blank"),j=Backbone.Model.extend({changes:{},logChanges:function(a){var b=this.changes||{};_.each(a,function(a,c){b[c]=_.extend(b[c]||{instance_id:c},a)}),this.changes=b,this.trigger("change:methods")},save:function(){_.size(this.changes)?a.post(d+"?action=woocommerce_shipping_zone_methods_save_changes",{wc_shipping_zones_nonce:b.wc_shipping_zones_nonce,changes:this.changes,zone_id:b.zone_id},this.onSaveResponse,"json"):l.trigger("saved:methods")},addMethod:function(){_.size(this.changes)&&this.save(),a.post(d+"?action=woocommerce_shipping_zone_add_method",{wc_shipping_zones_nonce:b.wc_shipping_zones_nonce,method_id:a('select[name="add_method_id"]').val(),zone_id:b.zone_id},this.onAddResponse,"json")},onSaveResponse:function(a,c){"success"===c&&(a.success?(l.set("methods",a.data.methods),l.trigger("change:methods"),l.changes={},l.trigger("saved:methods")):window.alert(b.strings.save_failed))},onAddResponse:function(a,c){"success"===c&&(a.success&&a.data.instance_id?(l.set("methods",a.data.methods),l.trigger("change:methods"),l.changes={},l.trigger("saved:methods")):window.alert(b.strings.add_method_failed))}}),k=Backbone.View.extend({rowTemplate:h,initialize:function(){this.listenTo(this.model,"change:methods",this.setUnloadConfirmation),this.listenTo(this.model,"saved:methods",this.clearUnloadConfirmation),this.listenTo(this.model,"saved:methods",this.render),f.on("change",{view:this},this.updateModelOnChange),f.on("sortupdate",{view:this},this.updateModelOnSort),a(window).on("beforeunload",{view:this},this.unloadConfirmation),g.on("click",{view:this},this.onSubmit),a(".wc-shipping-zone-add-method").on("click",{view:this},this.onAdd)},block:function(){a(this.el).block({message:null,overlayCSS:{background:"#fff",opacity:.6}})},unblock:function(){a(this.el).unblock()},render:function(){var b=_.indexBy(this.model.get("methods"),"instance_id"),c=this;this.$el.empty(),this.unblock(),_.size(b)?(b=_.sortBy(b,function(a){return parseInt(a.method_order,10)}),a.each(b,function(a,b){c.$el.append(c.rowTemplate(b))}),this.$el.find(".wc-shipping-zone-method-delete").on("click",{view:this},this.onDeleteRow)):c.$el.append(i),this.initTooltips()},initTooltips:function(){a("#tiptip_holder").removeAttr("style"),a("#tiptip_arrow").removeAttr("style"),a(".tips").tipTip({attribute:"data-tip",fadeIn:50,fadeOut:50,delay:50})},onSubmit:function(a){a.data.view.block(),a.data.view.model.save(),a.preventDefault()},onAdd:function(a){a.data.view.block(),a.data.view.model.addMethod(),a.preventDefault()},onDeleteRow:function(b){var c=b.data.view,d=c.model,e=_.indexBy(d.get("methods"),"instance_id"),f={},g=a(this).closest("tr").data("id");b.preventDefault(),delete e[g],f[g]=_.extend(f[g]||{},{deleted:"deleted"}),d.set("methods",e),d.logChanges(f),c.render()},setUnloadConfirmation:function(){this.needsUnloadConfirm=!0,g.removeAttr("disabled")},clearUnloadConfirmation:function(){this.needsUnloadConfirm=!1,g.attr("disabled","disabled")},unloadConfirmation:function(a){return a.data.view.needsUnloadConfirm?(a.returnValue=b.strings.unload_confirmation_msg,window.event.returnValue=b.strings.unload_confirmation_msg,b.strings.unload_confirmation_msg):void 0},updateModelOnChange:function(b){var c=b.data.view.model,d=a(b.target),e=d.closest("tr").data("id"),f=d.data("attribute"),g=d.val(),h=_.indexBy(c.get("methods"),"instance_id"),i={};h[e][f]!==g&&(i[e]={},i[e][f]=g,h[e][f]=g),c.logChanges(i)},updateModelOnSort:function(a){var b=a.data.view,c=b.model,d=_.indexBy(c.get("methods"),"instance_id"),f={};_.each(d,function(a){var b=parseInt(a.method_order,10),c=parseInt(e.find('tr[data-id="'+a.instance_id+'"]').index()+1,10);b!==c&&(f[a.instance_id]=_.extend(f[a.instance_id]||{},{method_order:c}))}),_.size(f)&&c.logChanges(f)}}),l=new j({methods:b.methods}),m=new k({model:l,el:f});m.render(),f.sortable({items:"tr",cursor:"move",axis:"y",handle:"td.wc-shipping-zone-method-sort",scrollSensitivity:40})})}(jQuery,shippingZoneMethodsLocalizeScript,wp,ajaxurl);
|
|
@ -3075,8 +3075,8 @@ class WC_AJAX {
|
|||
}
|
||||
|
||||
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'] ) );
|
||||
$zone->clear_locations();
|
||||
foreach ( $locations as $location ) {
|
||||
// Each posted location will be in the format type:code
|
||||
$location_parts = explode( ':', $location );
|
||||
|
@ -3095,6 +3095,7 @@ class WC_AJAX {
|
|||
}
|
||||
|
||||
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' );
|
||||
|
|
|
@ -257,10 +257,18 @@ class WC_Shipping_Zone {
|
|||
|
||||
/**
|
||||
* Clear all locations for this zone.
|
||||
* @param array|string $types of location to clear
|
||||
*/
|
||||
public function clear_locations() {
|
||||
$this->data['zone_locations'] = array();
|
||||
$this->_locations_changed = true;
|
||||
public function clear_locations( $types = array( 'postcode', 'state', 'country', 'continent' ) ) {
|
||||
if ( ! is_array( $types ) ) {
|
||||
$types = array( $types );
|
||||
}
|
||||
foreach ( $this->data['zone_locations'] as $key => $values ) {
|
||||
if ( in_array( $values->type, $types ) ) {
|
||||
unset( $this->data['zone_locations'][ $key ] );
|
||||
$this->_locations_changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -7,7 +7,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* Flat Rate Shipping Method.
|
||||
*
|
||||
* @class WC_Shipping_Flat_Rate
|
||||
* @version 2.4.0
|
||||
* @version 2.6.0
|
||||
* @package WooCommerce/Classes/Shipping
|
||||
* @author WooThemes
|
||||
*/
|
||||
|
@ -121,7 +121,7 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
|
|||
*/
|
||||
public function calculate_shipping( $package = array() ) {
|
||||
$rate = array(
|
||||
'id' => $this->id,
|
||||
'id' => $this->id . $this->instance_id,
|
||||
'label' => $this->title,
|
||||
'cost' => 0,
|
||||
);
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -10,7 +9,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* A simple shipping method allowing local delivery as a shipping method.
|
||||
*
|
||||
* @class WC_Shipping_Local_Delivery
|
||||
* @version 2.3.0
|
||||
* @version 2.6.0
|
||||
* @package WooCommerce/Classes/Shipping
|
||||
* @author WooThemes
|
||||
*/
|
||||
|
@ -19,10 +18,15 @@ class WC_Shipping_Local_Delivery extends WC_Shipping_Local_Pickup {
|
|||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->id = 'local_delivery';
|
||||
$this->method_title = __( 'Local Delivery', 'woocommerce' );
|
||||
$this->method_description = __( 'Local delivery is a simple shipping method for delivering orders locally.', 'woocommerce' );
|
||||
public function __construct( $instance_id = 0 ) {
|
||||
$this->id = 'local_delivery';
|
||||
$this->instance_id = absint( $instance_id );
|
||||
$this->method_title = __( 'Local Delivery', 'woocommerce' );
|
||||
$this->method_description = __( 'Local delivery is a simple shipping method for delivering orders locally.', 'woocommerce' );
|
||||
$this->supports = array(
|
||||
'shipping-zones',
|
||||
'instance-settings'
|
||||
);
|
||||
$this->init();
|
||||
}
|
||||
|
||||
|
@ -71,20 +75,18 @@ class WC_Shipping_Local_Delivery extends WC_Shipping_Local_Pickup {
|
|||
break;
|
||||
}
|
||||
|
||||
$rate = array(
|
||||
'id' => $this->id,
|
||||
$this->add_rate( array(
|
||||
'id' => $this->id . $this->instance_id,
|
||||
'label' => $this->title,
|
||||
'cost' => $shipping_total
|
||||
);
|
||||
|
||||
$this->add_rate( $rate );
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Init form fields.
|
||||
*/
|
||||
public function init_form_fields() {
|
||||
$this->form_fields = array(
|
||||
$this->instance_form_fields = array(
|
||||
'enabled' => array(
|
||||
'title' => __( 'Enable', 'woocommerce' ),
|
||||
'type' => 'checkbox',
|
||||
|
@ -118,35 +120,6 @@ class WC_Shipping_Local_Delivery extends WC_Shipping_Local_Pickup {
|
|||
'default' => '',
|
||||
'desc_tip' => true,
|
||||
'placeholder' => wc_format_localized_price( 0 )
|
||||
),
|
||||
'codes' => array(
|
||||
'title' => __( 'Allowed ZIP/Post Codes', 'woocommerce' ),
|
||||
'type' => 'text',
|
||||
'desc_tip' => __( 'What ZIP/post codes are available for local delivery?', 'woocommerce' ),
|
||||
'default' => '',
|
||||
'description' => __( 'Separate codes with a comma. Accepts wildcards, e.g. <code>P*</code> will match a postcode of PE30. Also accepts a pattern, e.g. <code>NG1___</code> would match NG1 1AA but not NG10 1AA', 'woocommerce' ),
|
||||
'placeholder' => 'e.g. 12345, 56789'
|
||||
),
|
||||
'availability' => array(
|
||||
'title' => __( 'Method availability', 'woocommerce' ),
|
||||
'type' => 'select',
|
||||
'default' => 'all',
|
||||
'class' => 'availability wc-enhanced-select',
|
||||
'options' => array(
|
||||
'all' => __( 'All allowed countries', 'woocommerce' ),
|
||||
'specific' => __( 'Specific Countries', 'woocommerce' )
|
||||
)
|
||||
),
|
||||
'countries' => array(
|
||||
'title' => __( 'Specific Countries', 'woocommerce' ),
|
||||
'type' => 'multiselect',
|
||||
'class' => 'wc-enhanced-select',
|
||||
'css' => 'width: 450px;',
|
||||
'default' => '',
|
||||
'options' => WC()->countries->get_shipping_countries(),
|
||||
'custom_attributes' => array(
|
||||
'data-placeholder' => __( 'Select some countries', 'woocommerce' )
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
<?php
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -10,7 +9,7 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
* A simple shipping method allowing free pickup as a shipping method.
|
||||
*
|
||||
* @class WC_Shipping_Local_Pickup
|
||||
* @version 2.3.0
|
||||
* @version 2.6.0
|
||||
* @package WooCommerce/Classes/Shipping
|
||||
* @author WooThemes
|
||||
*/
|
||||
|
@ -19,10 +18,15 @@ class WC_Shipping_Local_Pickup extends WC_Shipping_Method {
|
|||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->id = 'local_pickup';
|
||||
$this->method_title = __( 'Local Pickup', 'woocommerce' );
|
||||
$this->method_description = __( 'Local pickup is a simple method which allows customers to pick up orders themselves.', 'woocommerce' );
|
||||
public function __construct( $instance_id = 0 ) {
|
||||
$this->id = 'local_pickup';
|
||||
$this->instance_id = absint( $instance_id );
|
||||
$this->method_title = __( 'Local Pickup', 'woocommerce' );
|
||||
$this->method_description = __( 'Allow customers to pick up orders themselves. By default, when using local pickup store base taxes will apply regardless of customer address.', 'woocommerce' );
|
||||
$this->supports = array(
|
||||
'shipping-zones',
|
||||
'instance-settings'
|
||||
);
|
||||
$this->init();
|
||||
}
|
||||
|
||||
|
@ -50,18 +54,17 @@ class WC_Shipping_Local_Pickup extends WC_Shipping_Method {
|
|||
* calculate_shipping function.
|
||||
*/
|
||||
public function calculate_shipping() {
|
||||
$rate = array(
|
||||
'id' => $this->id,
|
||||
$this->add_rate( array(
|
||||
'id' => $this->id . $this->instance_id,
|
||||
'label' => $this->title,
|
||||
);
|
||||
$this->add_rate( $rate );
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* init_form_fields function.
|
||||
*/
|
||||
public function init_form_fields() {
|
||||
$this->form_fields = array(
|
||||
$this->instance_form_fields = array(
|
||||
'enabled' => array(
|
||||
'title' => __( 'Enable', 'woocommerce' ),
|
||||
'type' => 'checkbox',
|
||||
|
@ -74,35 +77,6 @@ class WC_Shipping_Local_Pickup extends WC_Shipping_Method {
|
|||
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
|
||||
'default' => __( 'Local Pickup', 'woocommerce' ),
|
||||
'desc_tip' => true,
|
||||
),
|
||||
'codes' => array(
|
||||
'title' => __( 'Allowed ZIP/Post Codes', 'woocommerce' ),
|
||||
'type' => 'text',
|
||||
'desc_tip' => __( 'What ZIP/post codes are available for local pickup?', 'woocommerce' ),
|
||||
'default' => '',
|
||||
'description' => __( 'Separate codes with a comma. Accepts wildcards, e.g. <code>P*</code> will match a postcode of PE30. Also accepts a pattern, e.g. <code>NG1___</code> would match NG1 1AA but not NG10 1AA', 'woocommerce' ),
|
||||
'placeholder' => 'e.g. 12345, 56789'
|
||||
),
|
||||
'availability' => array(
|
||||
'title' => __( 'Method availability', 'woocommerce' ),
|
||||
'type' => 'select',
|
||||
'default' => 'all',
|
||||
'class' => 'availability wc-enhanced-select',
|
||||
'options' => array(
|
||||
'all' => __( 'All allowed countries', 'woocommerce' ),
|
||||
'specific' => __( 'Specific Countries', 'woocommerce' )
|
||||
)
|
||||
),
|
||||
'countries' => array(
|
||||
'title' => __( 'Specific Countries', 'woocommerce' ),
|
||||
'type' => 'multiselect',
|
||||
'class' => 'wc-enhanced-select',
|
||||
'css' => 'width: 450px;',
|
||||
'default' => '',
|
||||
'options' => WC()->countries->get_shipping_countries(),
|
||||
'custom_attributes' => array(
|
||||
'data-placeholder' => __( 'Select some countries', 'woocommerce' )
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue