Skip to content

Commit

Permalink
Move shipping classes to custom UI in shipping section
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley committed Jan 13, 2016
1 parent 8064b35 commit d821b87
Show file tree
Hide file tree
Showing 14 changed files with 384 additions and 22 deletions.
2 changes: 1 addition & 1 deletion assets/css/admin.css

Large diffs are not rendered by default.

18 changes: 15 additions & 3 deletions assets/css/admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2116,7 +2116,7 @@ table.wc_shipping {
}
}

table.wc-shipping-zones, table.wc-shipping-zone-methods {
table.wc-shipping-zones, table.wc-shipping-zone-methods, table.wc-shipping-classes {
td, th {
vertical-align: top;
line-height: 30px;
Expand Down Expand Up @@ -2245,6 +2245,9 @@ table.wc-shipping-zones, table.wc-shipping-zone-methods {
.wc-shipping-zone-methods {
width: 25%;
}
.wc-shipping-class-description,
.wc-shipping-class-name,
.wc-shipping-class-slug,
.wc-shipping-zone-name,
.wc-shipping-zone-region {
input, select, textarea {
Expand All @@ -2267,6 +2270,9 @@ table.wc-shipping-zones, table.wc-shipping-zone-methods {
}
}
}
.wc-shipping-class-count {
text-align: center;
}
.wc-shipping-zone-methods {
color: #999;
.method_disabled {
Expand All @@ -2292,7 +2298,7 @@ table.wc-shipping-zones, table.wc-shipping-zone-methods {
display: inline-block;
}
}
.wc-shipping-zone-actions, .wc-shipping-zone-method-actions {
.wc-shipping-zone-actions, .wc-shipping-zone-method-actions, .wc-shipping-class-actions {
width: 4em;

a {
Expand All @@ -2313,7 +2319,7 @@ table.wc-shipping-zones, table.wc-shipping-zone-methods {
}
}

a.wc-shipping-zone-delete, a.wc-shipping-zone-method-delete {
a.wc-shipping-zone-delete, a.wc-shipping-zone-method-delete, a.wc-shipping-class-delete {
&:before {
content: "\e013";
}
Expand All @@ -2324,6 +2330,12 @@ table.wc-shipping-zones, table.wc-shipping-zone-methods {
}
}

a.wc-shipping-class-edit {
&:before {
content: "\e603";
}
}

a.wc-shipping-zone-method-settings {
&:before {
content: "\e01c";
Expand Down
205 changes: 205 additions & 0 deletions assets/js/admin/wc-shipping-classes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/* global wc_enhanced_select_params, shippingClassesLocalizeScript, ajaxurl */
( function( $, data, wp, ajaxurl ) {
$( function() {
var $table = $( '.wc-shipping-classes' ),
$tbody = $( '.wc-shipping-class-rows' ),
$save_button = $( '.wc-shipping-class-save' ),
$row_template = wp.template( 'wc-shipping-class-row' ),
$blank_template = wp.template( 'wc-shipping-class-row-blank' );

// Backbone model
Shippingclass = Backbone.Model.extend({
changes: {},
logChanges: function( changedRows ) {
var changes = this.changes || {};

_.each( changedRows, function( row, id ) {
changes[ id ] = _.extend( changes[ id ] || { term_id : id }, row );
} );

this.changes = changes;
this.trigger( 'change:classes' );
},
save: function() {
if ( _.size( this.changes ) ) {
$.post( ajaxurl + '?action=woocommerce_shipping_classes_save_changes', {
wc_shipping_classes_nonce : data.wc_shipping_classes_nonce,
changes : this.changes
}, this.onSaveResponse, 'json' );
} else {
shippingclass.trigger( 'saved:classes' );
}
},
onSaveResponse: function( response, textStatus ) {
if ( 'success' === textStatus ) {
if ( response.success ) {
shippingclass.set( 'classes', response.data.shipping_classes );
shippingclass.trigger( 'change:classes' );
shippingclass.changes = {};
shippingclass.trigger( 'saved:classes' );
} else if ( response.data ) {
window.alert( response.data );
} else {
window.alert( data.strings.save_failed );
}
}
shippingclassView.unblock();
}
} ),

// Backbone view
ShippingclassView = Backbone.View.extend({
rowTemplate: $row_template,
initialize: function() {
this.listenTo( this.model, 'change:classes', this.setUnloadConfirmation );
this.listenTo( this.model, 'saved:classes', this.clearUnloadConfirmation );
this.listenTo( this.model, 'saved:classes', this.render );
$tbody.on( 'change', { view: this }, this.updateModelOnChange );
$( window ).on( 'beforeunload', { view: this }, this.unloadConfirmation );
$save_button.on( 'click', { view: this }, this.onSubmit );
$( document.body ).on( 'click', '.wc-shipping-class-add', { view: this }, this.onAddNewRow );
$( document.body ).on( 'click', '.wc-shipping-class-save-changes', { view: this }, this.onSubmit );
},
block: function() {
$( this.el ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
},
unblock: function() {
$( this.el ).unblock();
},
render: function() {
var classes = _.indexBy( this.model.get( 'classes' ), 'term_id' ),
view = this;

this.$el.empty();
this.unblock();

if ( _.size( classes ) ) {
// Populate $tbody with the current classes
$.each( classes, function( id, rowData ) {
view.$el.append( view.rowTemplate( rowData ) );

var $tr = view.$el.find( 'tr[data-id="' + rowData.term_id + '"]');

// Editing?
if ( rowData.editing ) {
$tr.addClass( 'editing' );
}
} );

// Make the rows function
this.$el.find('.view').show();
this.$el.find('.edit').hide();
this.$el.find( '.wc-shipping-class-edit' ).on( 'click', { view: this }, this.onEditRow );
this.$el.find( '.wc-shipping-class-delete' ).on( 'click', { view: this }, this.onDeleteRow );
this.$el.find( '.wc-shipping-class-postcodes-toggle' ).on( 'click', { view: this }, this.onTogglePostcodes );
this.$el.find('.editing .wc-shipping-class-edit').trigger('click');

// Stripe
if ( 0 === _.size( classes ) % 2) {
$table.find( 'tbody.wc-shipping-class-rows' ).next( 'tbody' ).find( 'tr' ).addClass( 'odd' );
} else {
$table.find( 'tbody.wc-shipping-class-rows' ).next( 'tbody' ).find( 'tr' ).removeClass( 'odd' );
}
} else {
view.$el.append( $blank_template );
}
},
onSubmit: function( event ) {
event.data.view.block();
event.data.view.model.save();
event.preventDefault();
},
onAddNewRow: function( event ) {
event.preventDefault();

var view = event.data.view,
model = view.model,
classes = _.indexBy( model.get( 'classes' ), 'term_id' ),
changes = {},
size = _.size( classes ),
newRow = _.extend( {}, data.default_class, {
term_id: 'new-' + size + '-' + Date.now(),
editing: true,
newRow : true
} );

classes[ newRow.term_id ] = newRow;
changes[ newRow.term_id ] = newRow;

model.set( 'classes', classes );
model.logChanges( changes );

view.render();
},
onEditRow: function( event ) {
event.preventDefault();
$( this ).closest('tr').addClass('editing');
$( this ).closest('tr').find('.view').hide();
$( this ).closest('tr').find('.edit').show();
event.data.view.model.trigger( 'change:classes' );
},
onDeleteRow: function( event ) {
var view = event.data.view,
model = view.model,
classes = _.indexBy( model.get( 'classes' ), 'term_id' ),
changes = {},
term_id = $( this ).closest('tr').data('id');

event.preventDefault();

delete classes[ term_id ];
changes[ term_id ] = _.extend( changes[ term_id ] || {}, { deleted : 'deleted' } );
model.set( 'classes', classes );
model.logChanges( changes );
view.render();
},
setUnloadConfirmation: function() {
this.needsUnloadConfirm = true;
$save_button.removeAttr( 'disabled' );
},
clearUnloadConfirmation: function() {
this.needsUnloadConfirm = false;
$save_button.attr( 'disabled', 'disabled' );
},
unloadConfirmation: function( event ) {
if ( event.data.view.needsUnloadConfirm ) {
event.returnValue = data.strings.unload_confirmation_msg;
window.event.returnValue = data.strings.unload_confirmation_msg;
return data.strings.unload_confirmation_msg;
}
},
updateModelOnChange: function( event ) {
var model = event.data.view.model,
$target = $( event.target ),
term_id = $target.closest( 'tr' ).data( 'id' ),
attribute = $target.data( 'attribute' ),
value = $target.val(),
classes = _.indexBy( model.get( 'classes' ), 'term_id' ),
changes = {};

if ( classes[ term_id ][ attribute ] !== value ) {
changes[ term_id ] = {};
changes[ term_id ][ attribute ] = value;
classes[ term_id ][ attribute ] = value;
}

model.logChanges( changes );
}
} ),
shippingclass = new Shippingclass({
classes: data.classes
} ),
shippingclassView = new ShippingclassView({
model: shippingclass,
el: $tbody
} );

shippingclassView.render();
});
})( jQuery, shippingClassesLocalizeScript, wp, ajaxurl );
1 change: 1 addition & 0 deletions assets/js/admin/wc-shipping-classes.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions includes/admin/class-wc-admin-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function admin_scripts() {
wp_register_script( 'wc-backbone-modal', WC()->plugin_url() . '/assets/js/admin/backbone-modal' . $suffix . '.js', array( 'underscore', 'backbone', 'wp-util' ), 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-backbone-modal' ), WC_VERSION );
wp_register_script( 'wc-shipping-zone-methods', WC()->plugin_url() . '/assets/js/admin/wc-shipping-zone-methods' . $suffix . '.js', array( 'jquery', 'wp-util', 'underscore', 'backbone', 'jquery-ui-sortable' ), WC_VERSION );
wp_register_script( 'wc-shipping-classes', WC()->plugin_url() . '/assets/js/admin/wc-shipping-classes' . $suffix . '.js', array( 'jquery', 'wp-util', 'underscore', 'backbone' ), 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 );
Expand Down
4 changes: 0 additions & 4 deletions includes/admin/class-wc-admin-help.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ public function add_tabs() {
'title' => __( 'Product Categories, Tags, Shipping Classes, & Attributes', 'woocommerce' ),
'url' => '//fast.wistia.net/embed/iframe/f0j5gzqigg?videoFoam=true'
),
'edit-product_shipping_class' => array(
'title' => __( 'Product Categories, Tags, Shipping Classes, & Attributes', 'woocommerce' ),
'url' => '//fast.wistia.net/embed/iframe/f0j5gzqigg?videoFoam=true'
),
'product_attributes' => array(
'title' => __( 'Product Categories, Tags, Shipping Classes, & Attributes', 'woocommerce' ),
'url' => '//fast.wistia.net/embed/iframe/f0j5gzqigg?videoFoam=true'
Expand Down
8 changes: 0 additions & 8 deletions includes/admin/class-wc-admin-taxonomies.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public function __construct() {

// Taxonomy page descriptions
add_action( 'product_cat_pre_add_form', array( $this, 'product_cat_description' ) );
add_action( 'product_shipping_class_pre_add_form', array( $this, 'shipping_class_description' ) );

$attribute_taxonomies = wc_get_attribute_taxonomies();

Expand Down Expand Up @@ -276,13 +275,6 @@ public function product_cat_description() {
echo wpautop( __( 'Product categories for your store can be managed here. To change the order of categories on the front-end you can drag and drop to sort them. To see more categories listed click the "screen options" link at the top of the page.', 'woocommerce' ) );
}

/**
* Description for shipping class page to aid users.
*/
public function shipping_class_description() {
echo wpautop( __( 'Shipping classes can be used to group products of similar type. These groups can then be used by certain shipping methods to provide different rates to different products.', 'woocommerce' ) );
}

/**
* Description for shipping class page to aid users.
*/
Expand Down
Loading

0 comments on commit d821b87

Please sign in to comment.