Refactored the link all variations, remove variation and add variation methods

This commit is contained in:
Claudio Sanches 2015-07-05 23:55:08 -03:00
parent ee437bb646
commit c3b5ad2337
4 changed files with 180 additions and 174 deletions

View File

@ -21,145 +21,6 @@ jQuery( function ( $ ) {
}
};
// Add a variation
$( '#variable_product_options' ).on( 'click', 'button.add_variation', function () {
$('.woocommerce_variations').block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
var loop = $('.woocommerce_variation').size();
var data = {
action: 'woocommerce_add_variation',
post_id: woocommerce_admin_meta_boxes_variations.post_id,
loop: loop,
security: woocommerce_admin_meta_boxes_variations.add_variation_nonce
};
$.post( woocommerce_admin_meta_boxes_variations.ajax_url, data, function ( response ) {
$( '.woocommerce_variations' ).append( response );
$( '.tips' ).tipTip({
'attribute': 'data-tip',
'fadeIn': 50,
'fadeOut': 50
});
$( 'input.variable_is_downloadable, input.variable_is_virtual, input.variable_manage_stock' ).change();
$( '.woocommerce_variations' ).unblock();
$( '#variable_product_options' ).trigger( 'woocommerce_variations_added' );
});
return false;
});
$( '#variable_product_options').on( 'click', 'button.link_all_variations', function () {
var answer = window.confirm( woocommerce_admin_meta_boxes_variations.i18n_link_all_variations );
if ( answer ) {
$( '#variable_product_options' ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
var data = {
action: 'woocommerce_link_all_variations',
post_id: woocommerce_admin_meta_boxes_variations.post_id,
security: woocommerce_admin_meta_boxes_variations.link_variation_nonce
};
$.post( woocommerce_admin_meta_boxes_variations.ajax_url, data, function ( response ) {
var count = parseInt( response, 10 );
if ( 1 === count ) {
window.alert( count + ' ' + woocommerce_admin_meta_boxes_variations.i18n_variation_added );
} else if ( 0 === count || count > 1 ) {
window.alert( count + ' ' + woocommerce_admin_meta_boxes_variations.i18n_variations_added );
} else {
window.alert( woocommerce_admin_meta_boxes_variations.i18n_no_variations_added );
}
if ( count > 0 ) {
var this_page = window.location.toString();
this_page = this_page.replace( 'post-new.php?', 'post.php?post=' + woocommerce_admin_meta_boxes_variations.post_id + '&action=edit&' );
$( '#variable_product_options' ).load( this_page + ' #variable_product_options_inner', function () {
$( '#variable_product_options' ).unblock();
$( '#variable_product_options' ).trigger( 'woocommerce_variations_added' );
$( '.tips' ).tipTip({
'attribute': 'data-tip',
'fadeIn': 50,
'fadeOut': 50
});
} );
} else {
$( '#variable_product_options' ).unblock();
}
});
}
return false;
});
$( '#variable_product_options' ).on( 'click', 'button.remove_variation', function ( e ) {
e.preventDefault();
var answer = window.confirm( woocommerce_admin_meta_boxes_variations.i18n_remove_variation );
if ( answer ) {
var el = $( this ).parent().parent();
var variation = $( this ).attr( 'rel' );
if ( variation > 0 ) {
$( el ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
var variation_ids = [];
variation_ids.push( variation );
var data = {
action: 'woocommerce_remove_variations',
variation_ids: variation_ids,
security: woocommerce_admin_meta_boxes_variations.delete_variations_nonce
};
$.post( woocommerce_admin_meta_boxes_variations.ajax_url, data, function() {
// Success
$( el ).fadeOut( '300', function () {
$( el ).remove();
});
});
} else {
$( el ).fadeOut( '300', function () {
$( el ).remove();
});
}
}
return false;
});
$( '.wc-metaboxes-wrapper' ).on( 'click', 'a.bulk_edit', function () {
var bulk_edit = $( 'select#field_to_edit' ).val(),
checkbox,
@ -428,13 +289,38 @@ jQuery( function ( $ ) {
var wc_meta_boxes_product_variations_ajax = {
/**
* Initialize products variations meta box
* Initialize variations ajax methods
*/
init: function() {
$( 'li.variations_tab a' ).on( 'click', this.initial_load );
$( '#variable_product_options' )
.on( 'click', 'button.add_variation', this.add_variation )
.on( 'click', 'button.remove_variation', this.remove_variation )
.on( 'click', 'button.link_all_variations', this.link_all_variations );
$( 'body' ).on( 'change', '#variable_product_options_inner .woocommerce_variations :input', this.input_changed );
},
/**
* Check if have some edition before leave the page
*
* @return {bool}
*/
check_for_editions: function() {
var wrapper = $( '#variable_product_options_inner .woocommerce_variations' );
if ( 'true' === wrapper.attr( 'data-edited' ) ) {
if ( window.confirm( woocommerce_admin_meta_boxes_variations.i18n_edited_variations ) ) {
wrapper.attr( 'data-edited', false );
} else {
return false;
}
}
return true;
},
/**
* Initial load variations
*
@ -478,13 +364,144 @@ jQuery( function ( $ ) {
},
type: 'POST',
success: function( response ) {
wrapper.empty().append( response ).attr( 'page', page );
wrapper.empty().append( response ).attr( 'data-page', page );
$( '#woocommerce-product-data' ).unblock();
}
});
},
/**
* Add variation
*
* @return {bool}
*/
add_variation: function() {
if ( ! wc_meta_boxes_product_variations_ajax.check_for_editions() ) {
return false;
}
$( '#woocommerce-product-data' ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
var data = {
action: 'woocommerce_add_variation',
post_id: woocommerce_admin_meta_boxes_variations.post_id,
loop: $( '.woocommerce_variation' ).size(),
security: woocommerce_admin_meta_boxes_variations.add_variation_nonce
};
$.post( woocommerce_admin_meta_boxes_variations.ajax_url, data, function() {
wc_meta_boxes_product_variations_pagenav.set_page( 1 );
$( '#woocommerce-product-data' ).unblock();
$( '#variable_product_options' ).trigger( 'woocommerce_variations_added' );
});
return false;
},
/**
* Remove variation
*
* @return {bool}
*/
remove_variation: function() {
if ( ! wc_meta_boxes_product_variations_ajax.check_for_editions() ) {
return false;
}
if ( window.confirm( woocommerce_admin_meta_boxes_variations.i18n_remove_variation ) ) {
var variation = $( this ).attr( 'rel' ),
variation_ids = [],
data = {
action: 'woocommerce_remove_variations'
};
$( '#woocommerce-product-data' ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
if ( 0 < variation ) {
variation_ids.push( variation );
data.variation_ids = variation_ids;
data.security = woocommerce_admin_meta_boxes_variations.delete_variations_nonce;
$.post( woocommerce_admin_meta_boxes_variations.ajax_url, data, function() {
var current = parseInt( $( '#variable_product_options_inner .woocommerce_variations' ).attr( 'data-page' ), 10 );
console.log( current );
wc_meta_boxes_product_variations_pagenav.set_page( current );
});
} else {
$( '#woocommerce-product-data' ).unblock();
}
}
return false;
},
/**
* Link all variations (or at least try :p)
*
* @return {bool}
*/
link_all_variations: function() {
if ( ! wc_meta_boxes_product_variations_ajax.check_for_editions() ) {
return false;
}
if ( window.confirm( woocommerce_admin_meta_boxes_variations.i18n_link_all_variations ) ) {
$( '#woocommerce-product-data' ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
var data = {
action: 'woocommerce_link_all_variations',
post_id: woocommerce_admin_meta_boxes_variations.post_id,
security: woocommerce_admin_meta_boxes_variations.link_variation_nonce
};
$.post( woocommerce_admin_meta_boxes_variations.ajax_url, data, function( response ) {
var count = parseInt( response, 10 );
if ( 1 === count ) {
window.alert( count + ' ' + woocommerce_admin_meta_boxes_variations.i18n_variation_added );
} else if ( 0 === count || count > 1 ) {
window.alert( count + ' ' + woocommerce_admin_meta_boxes_variations.i18n_variations_added );
} else {
window.alert( woocommerce_admin_meta_boxes_variations.i18n_no_variations_added );
}
if ( count > 0 ) {
wc_meta_boxes_product_variations_pagenav.set_page( 1 );
$( '#variable_product_options' ).trigger( 'woocommerce_variations_added' );
} else {
$( '#woocommerce-product-data' ).unblock();
}
});
}
return false;
},
/**
* Add new class when have changes in some input
*/
@ -509,32 +526,13 @@ jQuery( function ( $ ) {
$( '.variations-pagenav .last-page' ).on( 'click', this.last_page );
},
/**
* Check if have some edition before leave the page
*
* @return {bool}
*/
check_for_editions: function() {
var wrapper = $( '#variable_product_options_inner .woocommerce_variations' );
if ( 'true' === wrapper.attr( 'data-edited' ) ) {
if ( window.confirm( woocommerce_admin_meta_boxes_variations.i18n_edited_variations ) ) {
wrapper.attr( 'data-edited', false );
} else {
return false;
}
}
return true;
},
/**
* Check button if enabled and if don't have editions
*
* @return {bool}
*/
check_is_enabled: function( current ) {
return ! $( current ).hasClass( 'disabled' ) && wc_meta_boxes_product_variations_pagenav.check_for_editions();
return ! $( current ).hasClass( 'disabled' ) && wc_meta_boxes_product_variations_ajax.check_for_editions();
},
/**
@ -558,6 +556,13 @@ jQuery( function ( $ ) {
}
},
/**
* Set page
*/
set_page: function( page ) {
$( '.variations-pagenav .page-selector' ).val( page ).change();
},
/**
* Paginav pagination selector
*/
@ -565,11 +570,11 @@ jQuery( function ( $ ) {
var selected = parseInt( $( this ).val(), 10 ),
wrapper = $( '#variable_product_options_inner .woocommerce_variations' );
if ( wc_meta_boxes_product_variations_pagenav.check_for_editions() ) {
if ( wc_meta_boxes_product_variations_ajax.check_for_editions() ) {
wc_meta_boxes_product_variations_pagenav.change_classes();
wc_meta_boxes_product_variations_ajax.load_variations( selected );
} else {
$( this ).val( parseInt( wrapper.attr( 'page' ), 10 ) );
$( this ).val( parseInt( wrapper.data( 'page' ), 10 ) );
}
},
@ -580,7 +585,7 @@ jQuery( function ( $ ) {
*/
first_page: function() {
if ( wc_meta_boxes_product_variations_pagenav.check_is_enabled( this ) ) {
$( '.variations-pagenav .page-selector' ).val( 1 ).change();
wc_meta_boxes_product_variations_pagenav.set_page( 1 );
}
return false;
@ -594,10 +599,10 @@ jQuery( function ( $ ) {
prev_page: function() {
if ( wc_meta_boxes_product_variations_pagenav.check_is_enabled( this ) ) {
var wrapper = $( '#variable_product_options_inner .woocommerce_variations' ),
prev_page = parseInt( wrapper.attr( 'page' ), 10 ) - 1,
prev_page = parseInt( wrapper.data( 'page' ), 10 ) - 1,
new_page = ( 0 < prev_page ) ? prev_page : 1;
$( '.variations-pagenav .page-selector' ).val( new_page ).change();
wc_meta_boxes_product_variations_pagenav.set_page( new_page );
}
return false;
@ -612,10 +617,10 @@ jQuery( function ( $ ) {
if ( wc_meta_boxes_product_variations_pagenav.check_is_enabled( this ) ) {
var wrapper = $( '#variable_product_options_inner .woocommerce_variations' ),
total_pages = wrapper.data( 'total_pages' ),
next_page = parseInt( wrapper.attr( 'page' ), 10 ) + 1,
next_page = parseInt( wrapper.data( 'page' ), 10 ) + 1,
new_page = ( total_pages >= next_page ) ? next_page : total_pages;
$( '.variations-pagenav .page-selector' ).val( new_page ).change();
wc_meta_boxes_product_variations_pagenav.set_page( new_page );
}
return false;
@ -630,7 +635,7 @@ jQuery( function ( $ ) {
if ( wc_meta_boxes_product_variations_pagenav.check_is_enabled( this ) ) {
var last_page = $( '#variable_product_options_inner .woocommerce_variations' ).data( 'total_pages' );
$( '.variations-pagenav .page-selector' ).val( last_page ).change();
wc_meta_boxes_product_variations_pagenav.set_page( last_page );
}
return false;

File diff suppressed because one or more lines are too long

View File

@ -628,9 +628,10 @@ class WC_Meta_Box_Product_Data {
}
}
$variations_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'product_variation'", $post->ID ) );
$variations_count = absint( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'product_variation'", $post->ID ) ) );
$variations_per_page = absint( apply_filters( 'woocommerce_admin_meta_boxes_variations_per_page', 10 ) );
$variations_total_pages = ceil( $variations_count / $variations_per_page );
?>
<div id="variable_product_options" class="panel wc-metaboxes-wrapper"><div id="variable_product_options_inner">
@ -701,7 +702,7 @@ class WC_Meta_Box_Product_Data {
</span>
</div>
<div class="woocommerce_variations wc-metaboxes" data-attributes="<?php echo esc_attr( json_encode( $attributes ) ); ?>" data-product_id="<?php echo intval( $post->ID ); ?>" data-total_pages="<?php echo $variations_total_pages; ?>" page="1" data-edited="false">
<div class="woocommerce_variations wc-metaboxes" data-attributes="<?php echo esc_attr( json_encode( $attributes ) ); ?>" data-product_id="<?php echo intval( $post->ID ); ?>" data-total="<?php echo $variations_count; ?>" data-total_pages="<?php echo $variations_total_pages; ?>" data-page="1" data-edited="false">
</div>
<p class="toolbar">

View File

@ -2364,8 +2364,8 @@ class WC_AJAX {
'post_status' => array( 'private', 'publish' ),
'posts_per_page' => $per_page,
'paged' => $page,
'orderby' => 'menu_order',
'order' => 'asc',
'orderby' => 'ID',
'order' => 'DESC',
'post_parent' => $product_id
);