List and sort methods within zones

This commit is contained in:
Mike Jolley 2015-12-15 17:48:03 +00:00
parent f48f09ebdf
commit e25d876779
10 changed files with 93 additions and 30 deletions

File diff suppressed because one or more lines are too long

View File

@ -2134,7 +2134,7 @@ table.wc-shipping-zones, table.wc-shipping-zone-methods {
ul, p {
margin: 0;
}
td.wc-shipping-zone-sort {
td.wc-shipping-zone-sort, td.wc-shipping-zone-method-sort {
cursor: move;
text-align: center;
@ -2198,7 +2198,7 @@ table.wc-shipping-zones, table.wc-shipping-zone-methods {
}
}
.wc-shipping-zone-actions, .wc-shipping-zone-method-actions {
width: 4em;
width: 6em;
a {
@include ir();
@ -2223,6 +2223,11 @@ table.wc-shipping-zones, table.wc-shipping-zone-methods {
content: "\e603";
}
}
a.wc-shipping-zone-view {
&:before {
content: "\e010";
}
}
a.wc-shipping-zone-delete, a.wc-shipping-zone-method-delete {
&:before {
content: "\e013";

View File

@ -7,7 +7,7 @@
$row_template = wp.template( 'wc-shipping-zone-method-row' ),
// Backbone model
ShippingZone = Backbone.Model.extend({
ShippingMethod = Backbone.Model.extend({
changes: {},
logChanges: function( changedRows ) {
var changes = this.changes || {};
@ -21,21 +21,22 @@
},
save: function() {
if ( _.size( this.changes ) ) {
$.post( ajaxurl + '?action=woocommerce_shipping_methods_save_changes', {
wc_shipping_methods_nonce : data.wc_shipping_methods_nonce,
changes : this.changes
$.post( ajaxurl + '?action=woocommerce_shipping_zone_methods_save_changes', {
wc_shipping_zones_nonce : data.wc_shipping_zones_nonce,
changes : this.changes,
zone_id : data.zone_id
}, this.onSaveResponse, 'json' );
} else {
shippingmethod.trigger( 'saved:methods' );
shippingMethod.trigger( 'saved:methods' );
}
},
onSaveResponse: function( response, textStatus ) {
if ( 'success' === textStatus ) {
if ( response.success ) {
shippingmethod.set( 'methods', response.data.methods );
shippingmethod.trigger( 'change:methods' );
shippingmethod.changes = {};
shippingmethod.trigger( 'saved:methods' );
shippingMethod.set( 'methods', response.data.methods );
shippingMethod.trigger( 'change:methods' );
shippingMethod.changes = {};
shippingMethod.trigger( 'saved:methods' );
} else {
window.alert( data.strings.save_failed );
}
@ -44,7 +45,7 @@
} ),
// Backbone view
ShippingZoneView = Backbone.View.extend({
ShippingMethodView = Backbone.View.extend({
rowTemplate: $row_template,
initialize: function() {
this.listenTo( this.model, 'change:methods', this.setUnloadConfirmation );
@ -140,7 +141,7 @@
methods = _.indexBy( model.get( 'methods' ), 'instance_id' ),
changes = {};
_.each( methods, function( zone ) {
_.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 );
@ -154,15 +155,15 @@
}
}
} ),
shippingZone = new ShippingZone({
shippingMethod = new ShippingMethod({
methods: data.methods
} ),
shippingZoneView = new ShippingZoneView({
model: shippingZone,
shippingMethodView = new ShippingMethodView({
model: shippingMethod,
el: $tbody
} );
shippingZoneView.render();
shippingMethodView.render();
$tbody.sortable({
items: 'tr',

View File

@ -127,8 +127,15 @@
} else {
$table.find( 'tbody.wc-shipping-zone-rows' ).next( 'tbody' ).find( 'tr' ).removeClass( 'odd' );
}
this.initTooltips();
}
},
initTooltips: function() {
$( '#tiptip_holder' ).removeAttr( 'style' );
$( '#tiptip_arrow' ).removeAttr( 'style' );
$( '.tips' ).tipTip({ 'attribute': 'data-tip', 'fadeIn': 50, 'fadeOut': 50, 'delay': 50 });
},
onSubmit: function( event ) {
event.data.view.model.save();
event.preventDefault();

View File

@ -101,7 +101,7 @@ abstract class WC_Shipping_Method extends WC_Settings_API {
* Instance ID if used.
* @var int
*/
protected $instance_id = 0;
public $instance_id = 0;
/**
* Instance settings.

View File

@ -41,6 +41,7 @@ class WC_Admin_Shipping_Zones {
wp_localize_script( 'wc-shipping-zone-methods', 'shippingZoneMethodsLocalizeScript', array(
'methods' => $zone->get_shipping_methods(),
'zone_id' => $zone->get_zone_id(),
'wc_shipping_zones_nonce' => wp_create_nonce( 'wc_shipping_zones_nonce' ),
'strings' => array(
'unload_confirmation_msg' => __( 'Your changed data will be lost if you leave this page without saving.', 'woocommerce' ),
@ -99,7 +100,7 @@ class WC_Admin_Shipping_Zones {
?>
<div class="wrap woocommerce">
<h1><?php echo esc_html( $shipping_method->get_method_title() ); ?> <small class="wc-admin-breadcrumb">&lt; <a href=""<?php echo esc_url( admin_url( 'admin.php?page=shipping&zone_id=' . absint( $zone->get_zone_id() ) ) ); ?>"><?php _e( 'Shipping Methods', 'woocommerce' ); ?> (<?php echo esc_html( $zone->get_zone_name() ); ?>)</a> &lt; <a href="<?php echo esc_url( admin_url( 'admin.php?page=shipping' ) ); ?>"><?php _e( 'Shipping Zones', 'woocommerce' ); ?></a></small></h1>
<h1><?php echo esc_html( $shipping_method->get_method_title() ); ?> <small class="wc-admin-breadcrumb">&lt; <a href=""<?php echo esc_url( admin_url( 'admin.php?page=wc-shipping&zone_id=' . absint( $zone->get_zone_id() ) ) ); ?>"><?php _e( 'Shipping Methods', 'woocommerce' ); ?> (<?php echo esc_html( $zone->get_zone_name() ); ?>)</a> &lt; <a href="<?php echo esc_url( admin_url( 'admin.php?page=wc-shipping' ) ); ?>"><?php _e( 'Shipping Zones', 'woocommerce' ); ?></a></small></h1>
<form id="add-method" method="post">
<?php $shipping_method->admin_options(); ?>
<p class="submit"><input type="submit" class="button button-primary" name="save_method" value="<?php _e( 'Save changes', 'woocommerce' ); ?>" /></p>

View File

@ -1,19 +1,20 @@
<div class="wrap woocommerce">
<h1><?php _e( 'Shipping Methods', 'woocommerce' ); ?> (<?php echo esc_html( $zone->get_zone_name() ); ?>) <small class="wc-admin-breadcrumb">&lt; <a href="<?php echo esc_url( admin_url( 'admin.php?page=shipping' ) ); ?>"><?php _e( 'Shipping Zones', 'woocommerce' ); ?></a><small></h1>
<h1><?php _e( 'Shipping Methods', 'woocommerce' ); ?> (<?php echo esc_html( $zone->get_zone_name() ); ?>) <small class="wc-admin-breadcrumb">&lt; <a href="<?php echo esc_url( admin_url( 'admin.php?page=wc-shipping' ) ); ?>"><?php _e( 'Shipping Zones', 'woocommerce' ); ?></a><small></h1>
<p><?php printf( __( 'The following shipping methods will apply to customers within the %s zone.', 'woocommerce' ), esc_html( $zone->get_zone_name() ) ); ?><p>
<form>
<table class="wc-shipping-zone-methods widefat">
<thead>
<tr>
<th class="wc-shipping-zone-method-sort">&nbsp;</th>
<th class="wc-shipping-zone-method-name"><?php esc_html_e( 'Name', 'woocommerce' ); ?></th>
<th class="wc-shipping-zone-method-title"><?php esc_html_e( 'Title', 'woocommerce' ); ?></th>
<th class="wc-shipping-zone-method-type"><?php esc_html_e( 'Type', 'woocommerce' ); ?></th>
<th class="wc-shipping-zone-method-description"><?php esc_html_e( 'Description', 'woocommerce' ); ?></th>
<th class="wc-shipping-zone-method-actions">&nbsp;</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="4">
<td colspan="5">
<form method="get" class="wc-shipping-zone-method-selector">
<select name="method_type">
<?php
@ -43,8 +44,9 @@
<script type="text/html" id="tmpl-wc-shipping-zone-method-row">
<tr data-id="{{ data.instance_id }}">
<td width="1%" class="wc-shipping-zone-method-sort"></td>
<td class="wc-shipping-zone-method-name">{{ data.method_title }}</td>
<td class="wc-shipping-zone-method-description"></td>
<td class="wc-shipping-zone-method-title"><a href="admin.php?page=wc-shipping&amp;instance_id={{ data.instance_id }}">{{ data.title }}</a></td>
<td class="wc-shipping-zone-method-type">{{ data.method_title }}</td>
<td class="wc-shipping-zone-method-description">{{ data.method_description }}</td>
<td class="wc-shipping-zone-method-actions">
<a class="wc-shipping-zone-method-delete tips" data-tip="<?php esc_attr_e( 'Delete', 'woocommerce' ); ?>" href="#"><?php _e( 'Delete', 'woocommerce' ); ?></a><a class="wc-shipping-zone-method-settings tips" data-tip="<?php esc_attr_e( 'Settings', 'woocommerce' ); ?>" href="admin.php?page=wc-shipping&amp;instance_id={{ data.instance_id }}"><?php _e( 'Settings', 'woocommerce' ); ?></a>
</td>

View File

@ -38,7 +38,7 @@
<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="<?php echo esc_url( admin_url( 'admin.php?page=wc-shipping&zone_id=' ) ); ?>{{ data.zone_id }}">{{ data.zone_name }}</a></div>
<div class="view"><a href="admin.php?page=wc-shipping&amp;zone_id={{ data.zone_id }}">{{ data.zone_name }}</a></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 class="wc-shipping-zone-region">
@ -74,7 +74,7 @@
<ul></ul>
</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>
<a class="wc-shipping-zone-delete tips" data-tip="<?php _e( 'Delete', 'woocommerce' ); ?>" href="#"><?php _e( 'Delete', 'woocommerce' ); ?></a><a class="wc-shipping-zone-edit tips" href="#" data-tip="<?php _e( 'Edit', 'woocommerce' ); ?>"><?php _e( 'Edit', 'woocommerce' ); ?></a><a class="wc-shipping-zone-view tips" data-tip="<?php _e( 'View Zone', 'woocommerce' ); ?>" href="admin.php?page=wc-shipping&amp;zone_id={{ data.zone_id }}"><?php _e( 'View', 'woocommerce' ); ?></a>
</td>
</tr>
</script>

View File

@ -142,6 +142,7 @@ class WC_AJAX {
'bulk_edit_variations' => false,
'tax_rates_save_changes' => false,
'shipping_zones_save_changes' => false,
'shipping_zone_methods_save_changes' => false,
);
foreach ( $ajax_events as $ajax_event => $nopriv ) {
@ -3026,7 +3027,7 @@ class WC_AJAX {
*/
public static function shipping_zones_save_changes() {
if ( ! isset( $_POST['wc_shipping_zones_nonce'], $_POST['changes'] ) ) {
wp_send_json_error( 'missing_fields' . (isset( $_POST['wc_shipping_zones_nonce'] ) ? 1 : 0). (isset( $_POST['changes'] ) ? 1 : 0) );
wp_send_json_error( 'missing_fields' );
exit;
}
@ -3104,8 +3105,53 @@ class WC_AJAX {
}
wp_send_json_success( array(
'zones' => WC_Shipping_Zones::get_zones(),
'test' => print_r($_POST['changes'], true)
'zones' => WC_Shipping_Zones::get_zones()
) );
}
/**
* 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'] ) ) {
wp_send_json_error( 'missing_fields' );
exit;
}
if ( ! wp_verify_nonce( $_POST['wc_shipping_zones_nonce'], 'wc_shipping_zones_nonce' ) ) {
wp_send_json_error( 'bad_nonce' );
exit;
}
// Check User Caps
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( 'missing_capabilities' );
exit;
}
global $wpdb;
$zone_id = absint( $_POST['zone_id'] );
$zone = new WC_Shipping_Zone( $zone_id );
$changes = $_POST['changes'];
foreach ( $changes as $instance_id => $data ) {
if ( isset( $data['deleted'] ) ) {
$wpdb->delete( "{$wpdb->prefix}woocommerce_shipping_zone_methods", array( 'instance_id' => array( $instance_id ) ) );
continue;
}
$method_data = array_intersect_key( $data, array(
'method_order' => 1
) );
if ( isset( $method_data['method_order'] ) ) {
$wpdb->update( "{$wpdb->prefix}woocommerce_shipping_zone_methods", array( 'method_order' => absint( $method_data['method_order'] ) ), array( 'instance_id' => absint( $instance_id ) ) );
}
}
wp_send_json_success( array(
'methods' => $zone->get_shipping_methods()
) );
}
}

View File

@ -143,6 +143,7 @@ class WC_Shipping_Zone {
if ( in_array( $raw_method->method_id, array_keys( $allowed_classes ) ) ) {
$class_name = $allowed_classes[ $raw_method->method_id ];
$methods[ $raw_method->instance_id ] = new $class_name( $raw_method->instance_id );
$methods[ $raw_method->instance_id ]->method_order = absint( $raw_method->method_order );
}
}