2015-07-07 20:35:21 +00:00
|
|
|
/* global wp, woocommerce_admin_meta_boxes_variations, woocommerce_admin, accounting */
|
2015-07-06 23:05:09 +00:00
|
|
|
jQuery( function( $ ) {
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
/**
|
|
|
|
* Variations actions
|
|
|
|
*/
|
|
|
|
var wc_meta_boxes_product_variations_actions = {
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
/**
|
|
|
|
* Initialize variations actions
|
|
|
|
*/
|
|
|
|
init: function() {
|
|
|
|
$( '#variable_product_options' )
|
|
|
|
.on( 'change', 'input.variable_is_downloadable', this.variable_is_downloadable )
|
|
|
|
.on( 'change', 'input.variable_is_virtual', this.variable_is_virtual )
|
|
|
|
.on( 'change', 'input.variable_manage_stock', this.variable_manage_stock );
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
$( 'input.variable_is_downloadable, input.variable_is_virtual, input.variable_manage_stock' ).change();
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-07 01:50:35 +00:00
|
|
|
$( '#woocommerce-product-data' ).on( 'woocommerce_variations_loaded', this.variations_loaded );
|
2015-07-06 23:05:09 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if variation is downloadable and show/hide elements
|
|
|
|
*/
|
|
|
|
variable_is_downloadable: function() {
|
|
|
|
$( this ).closest( '.woocommerce_variation' ).find( '.show_if_variation_downloadable' ).hide();
|
|
|
|
|
|
|
|
if ( $( this ).is( ':checked' ) ) {
|
|
|
|
$( this ).closest( '.woocommerce_variation' ).find( '.show_if_variation_downloadable' ).show();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if variation is virtual and show/hide elements
|
|
|
|
*/
|
|
|
|
variable_is_virtual: function() {
|
|
|
|
$( this ).closest( '.woocommerce_variation' ).find( '.hide_if_variation_virtual' ).show();
|
2014-06-19 15:17:43 +00:00
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
if ( $( this ).is( ':checked' ) ) {
|
|
|
|
$( this ).closest( '.woocommerce_variation' ).find( '.hide_if_variation_virtual' ).hide();
|
|
|
|
}
|
|
|
|
},
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
/**
|
|
|
|
* Check if variation manage stock and show/hide elements
|
|
|
|
*/
|
|
|
|
variable_manage_stock: function() {
|
|
|
|
$( this ).closest( '.woocommerce_variation' ).find( '.show_if_variation_manage_stock' ).hide();
|
|
|
|
|
|
|
|
if ( $( this ).is( ':checked' ) ) {
|
|
|
|
$( this ).closest( '.woocommerce_variation' ).find( '.show_if_variation_manage_stock' ).show();
|
|
|
|
}
|
2015-07-07 01:50:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run actions when variations is loaded
|
|
|
|
*/
|
|
|
|
variations_loaded: function() {
|
|
|
|
$( 'input.variable_is_downloadable, input.variable_is_virtual, input.variable_manage_stock', $( this ) ).change();
|
2015-07-07 19:39:59 +00:00
|
|
|
$( '.woocommerce_variation', $( this ) ).each( function( index, el ) {
|
|
|
|
var $el = $( el ),
|
|
|
|
date_from = $( '.sale_price_dates_from', $el ).val(),
|
|
|
|
date_to = $( '.sale_price_dates_to', $el ).val();
|
|
|
|
|
|
|
|
if ( '' !== date_from || '' !== date_to ) {
|
|
|
|
$( 'a.sale_schedule', $el ).click();
|
|
|
|
}
|
|
|
|
});
|
2015-07-07 01:50:35 +00:00
|
|
|
$( '.woocommerce_variations .variation-needs-update', $( this ) ).removeClass( 'variation-needs-update' );
|
|
|
|
$( 'button.save-variation-changes', $( this ) ).attr( 'disabled', 'disabled' );
|
2014-06-19 15:17:43 +00:00
|
|
|
}
|
2015-07-06 23:05:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Variations media actions
|
|
|
|
*/
|
|
|
|
var wc_meta_boxes_product_variations_media = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wp.media frame object
|
|
|
|
*
|
|
|
|
* @type {object}
|
|
|
|
*/
|
|
|
|
variable_image_frame: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Variation image ID
|
|
|
|
*
|
|
|
|
* @type {int}
|
|
|
|
*/
|
|
|
|
setting_variation_image_id: null,
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
/**
|
|
|
|
* Variation image object
|
|
|
|
*
|
|
|
|
* @type {object}
|
|
|
|
*/
|
|
|
|
setting_variation_image: null,
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
/**
|
|
|
|
* wp.media post ID
|
|
|
|
*
|
|
|
|
* @type {int}
|
|
|
|
*/
|
|
|
|
wp_media_post_id: wp.media.model.settings.post.id,
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
/**
|
|
|
|
* Initialize media actions
|
|
|
|
*/
|
|
|
|
init: function() {
|
|
|
|
$( '#variable_product_options' ).on( 'click', '.upload_image_button', this.add_image );
|
|
|
|
$( 'a.add_media' ).on( 'click', this.restore_wp_media_post_id );
|
|
|
|
},
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
/**
|
|
|
|
* Added new image
|
|
|
|
*
|
|
|
|
* @param {object} event
|
|
|
|
*/
|
|
|
|
add_image: function( event ) {
|
|
|
|
var $button = $( this ),
|
|
|
|
post_id = $button.attr( 'rel' ),
|
|
|
|
$parent = $button.closest( '.upload_image' );
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
wc_meta_boxes_product_variations_media.setting_variation_image = $parent;
|
|
|
|
wc_meta_boxes_product_variations_media.setting_variation_image_id = post_id;
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
event.preventDefault();
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
if ( $button.is( '.remove' ) ) {
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
$( '.upload_image_id', wc_meta_boxes_product_variations_media.setting_variation_image ).val( '' ).change();
|
|
|
|
wc_meta_boxes_product_variations_media.setting_variation_image.find( 'img' ).eq( 0 ).attr( 'src', woocommerce_admin_meta_boxes_variations.woocommerce_placeholder_img_src );
|
|
|
|
wc_meta_boxes_product_variations_media.setting_variation_image.find( '.upload_image_button' ).removeClass( 'remove' );
|
2013-08-06 10:41:20 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
// If the media frame already exists, reopen it.
|
|
|
|
if ( wc_meta_boxes_product_variations_media.variable_image_frame ) {
|
|
|
|
wc_meta_boxes_product_variations_media.variable_image_frame.uploader.uploader.param( 'post_id', wc_meta_boxes_product_variations_media.setting_variation_image_id );
|
|
|
|
wc_meta_boxes_product_variations_media.variable_image_frame.open();
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
wp.media.model.settings.post.id = wc_meta_boxes_product_variations_media.setting_variation_image_id;
|
|
|
|
}
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
// Create the media frame.
|
|
|
|
wc_meta_boxes_product_variations_media.variable_image_frame = wp.media.frames.variable_image = wp.media({
|
|
|
|
// Set the title of the modal.
|
|
|
|
title: woocommerce_admin_meta_boxes_variations.i18n_choose_image,
|
|
|
|
button: {
|
|
|
|
text: woocommerce_admin_meta_boxes_variations.i18n_set_image
|
|
|
|
},
|
|
|
|
states: [
|
|
|
|
new wp.media.controller.Library({
|
|
|
|
title: woocommerce_admin_meta_boxes_variations.i18n_choose_image,
|
|
|
|
filterable: 'all'
|
|
|
|
})
|
|
|
|
]
|
|
|
|
});
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
// When an image is selected, run a callback.
|
|
|
|
wc_meta_boxes_product_variations_media.variable_image_frame.on( 'select', function () {
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
var attachment = wc_meta_boxes_product_variations_media.variable_image_frame.state().get( 'selection' ).first().toJSON(),
|
|
|
|
url = attachment.sizes.thumbnail ? attachment.sizes.thumbnail.url : attachment.url;
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
$( '.upload_image_id', wc_meta_boxes_product_variations_media.setting_variation_image ).val( attachment.id ).change();
|
|
|
|
wc_meta_boxes_product_variations_media.setting_variation_image.find( '.upload_image_button' ).addClass( 'remove' );
|
|
|
|
wc_meta_boxes_product_variations_media.setting_variation_image.find( 'img' ).eq( 0 ).attr( 'src', url );
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
wp.media.model.settings.post.id = wc_meta_boxes_product_variations_media.wp_media_post_id;
|
|
|
|
});
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
// Finally, open the modal.
|
|
|
|
wc_meta_boxes_product_variations_media.variable_image_frame.open();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restore wp.media post ID.
|
|
|
|
*/
|
|
|
|
restore_wp_media_post_id: function() {
|
|
|
|
wp.media.model.settings.post.id = wc_meta_boxes_product_variations_media.wp_media_post_id;
|
|
|
|
}
|
|
|
|
};
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-02 20:42:22 +00:00
|
|
|
/**
|
2015-07-06 01:00:38 +00:00
|
|
|
* Product variations metabox ajax methods
|
2015-07-02 20:42:22 +00:00
|
|
|
*/
|
2015-07-06 01:00:38 +00:00
|
|
|
var wc_meta_boxes_product_variations_ajax = {
|
2015-07-02 20:42:22 +00:00
|
|
|
|
|
|
|
/**
|
2015-07-06 02:55:08 +00:00
|
|
|
* Initialize variations ajax methods
|
2015-07-02 20:42:22 +00:00
|
|
|
*/
|
|
|
|
init: function() {
|
2015-07-05 23:19:45 +00:00
|
|
|
$( 'li.variations_tab a' ).on( 'click', this.initial_load );
|
2015-07-06 02:55:08 +00:00
|
|
|
|
|
|
|
$( '#variable_product_options' )
|
2015-07-06 04:50:20 +00:00
|
|
|
.on( 'click', 'button.save-variation-changes', this.save_variations )
|
2015-07-06 02:55:08 +00:00
|
|
|
.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 );
|
|
|
|
|
2015-07-06 04:50:20 +00:00
|
|
|
$( 'body' ).on( 'change', '#variable_product_options .woocommerce_variations :input', this.input_changed );
|
2015-07-07 01:50:35 +00:00
|
|
|
|
|
|
|
$( '.wc-metaboxes-wrapper' ).on( 'click', 'a.bulk_edit', this.bulk_edit );
|
2015-07-02 20:42:22 +00:00
|
|
|
},
|
|
|
|
|
2015-07-06 02:55:08 +00:00
|
|
|
/**
|
|
|
|
* Check if have some edition before leave the page
|
|
|
|
*
|
|
|
|
* @return {bool}
|
|
|
|
*/
|
|
|
|
check_for_editions: function() {
|
2015-07-06 22:07:01 +00:00
|
|
|
var need_update = $( '#variable_product_options .woocommerce_variations .variation-needs-update' );
|
2015-07-06 02:55:08 +00:00
|
|
|
|
2015-07-07 20:48:24 +00:00
|
|
|
if ( 0 < need_update.length ) {
|
2015-07-06 02:55:08 +00:00
|
|
|
if ( window.confirm( woocommerce_admin_meta_boxes_variations.i18n_edited_variations ) ) {
|
2015-07-06 22:07:01 +00:00
|
|
|
need_update.removeClass( 'variation-needs-update' );
|
|
|
|
$( 'button.save-variation-changes' ).removeAttr( 'disabled' );
|
2015-07-06 02:55:08 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2015-07-06 04:50:20 +00:00
|
|
|
/**
|
|
|
|
* Block edit screen
|
|
|
|
*/
|
|
|
|
block: function() {
|
|
|
|
$( '#woocommerce-product-data' ).block({
|
|
|
|
message: null,
|
|
|
|
overlayCSS: {
|
|
|
|
background: '#fff',
|
|
|
|
opacity: 0.6
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unblock edit screen
|
|
|
|
*/
|
|
|
|
unblock: function() {
|
|
|
|
$( '#woocommerce-product-data' ).unblock();
|
|
|
|
},
|
|
|
|
|
2015-07-02 20:42:22 +00:00
|
|
|
/**
|
|
|
|
* Initial load variations
|
|
|
|
*
|
|
|
|
* @return {bool}
|
|
|
|
*/
|
|
|
|
initial_load: function() {
|
2015-07-06 04:50:20 +00:00
|
|
|
if ( 0 === $( '#variable_product_options .woocommerce_variations .woocommerce_variation' ).length ) {
|
2015-07-07 23:22:13 +00:00
|
|
|
wc_meta_boxes_product_variations_pagenav.go_to_page();
|
2015-07-05 23:19:45 +00:00
|
|
|
}
|
2015-07-02 20:42:22 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load variations via Ajax
|
|
|
|
*
|
|
|
|
* @param {int} page (default: 1)
|
|
|
|
* @param {int} per_page (default: 10)
|
|
|
|
*/
|
|
|
|
load_variations: function( page, per_page ) {
|
|
|
|
page = page || 1;
|
2015-07-06 01:00:38 +00:00
|
|
|
per_page = per_page || woocommerce_admin_meta_boxes_variations.variations_per_page;
|
2015-07-02 20:42:22 +00:00
|
|
|
|
2015-07-06 04:50:20 +00:00
|
|
|
var wrapper = $( '#variable_product_options .woocommerce_variations' );
|
2015-07-02 20:42:22 +00:00
|
|
|
|
2015-07-06 04:50:20 +00:00
|
|
|
wc_meta_boxes_product_variations_ajax.block();
|
2015-07-02 20:42:22 +00:00
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: woocommerce_admin_meta_boxes_variations.ajax_url,
|
|
|
|
data: {
|
|
|
|
action: 'woocommerce_load_variations',
|
|
|
|
security: woocommerce_admin_meta_boxes_variations.load_variations_nonce,
|
|
|
|
product_id: wrapper.data( 'product_id' ),
|
|
|
|
attributes: wrapper.data( 'attributes' ),
|
|
|
|
page: page,
|
|
|
|
per_page: per_page
|
|
|
|
},
|
|
|
|
type: 'POST',
|
|
|
|
success: function( response ) {
|
2015-07-06 02:55:08 +00:00
|
|
|
wrapper.empty().append( response ).attr( 'data-page', page );
|
2015-07-06 01:00:38 +00:00
|
|
|
|
2015-07-07 01:50:35 +00:00
|
|
|
$( '#woocommerce-product-data' ).trigger( 'woocommerce_variations_loaded' );
|
|
|
|
|
2015-07-06 04:50:20 +00:00
|
|
|
wc_meta_boxes_product_variations_ajax.unblock();
|
2015-07-02 20:42:22 +00:00
|
|
|
}
|
|
|
|
});
|
2015-07-06 01:31:56 +00:00
|
|
|
},
|
|
|
|
|
2015-07-06 04:50:20 +00:00
|
|
|
/**
|
|
|
|
* Save variations
|
|
|
|
*
|
|
|
|
* @return {bool}
|
|
|
|
*/
|
|
|
|
save_variations: function() {
|
2015-07-06 22:07:01 +00:00
|
|
|
var button = $( this ),
|
|
|
|
wrapper = $( '#variable_product_options .woocommerce_variations' ),
|
|
|
|
need_update = $( '.variation-needs-update', wrapper );
|
2015-07-06 04:50:20 +00:00
|
|
|
|
2015-07-06 22:07:01 +00:00
|
|
|
// Save only with products need update.
|
|
|
|
if ( 0 < need_update.length ) {
|
|
|
|
wc_meta_boxes_product_variations_ajax.block();
|
2015-07-06 04:50:20 +00:00
|
|
|
|
2015-07-06 22:07:01 +00:00
|
|
|
$.ajax({
|
|
|
|
url: woocommerce_admin_meta_boxes_variations.ajax_url,
|
|
|
|
data: {
|
|
|
|
action: 'woocommerce_save_variations',
|
|
|
|
security: woocommerce_admin_meta_boxes_variations.save_variations_nonce,
|
|
|
|
product_id: wrapper.data( 'product_id' ),
|
|
|
|
data: $( ':input', need_update ).serialize()
|
|
|
|
},
|
|
|
|
type: 'POST',
|
|
|
|
success: function() {
|
|
|
|
// Allow change page, delete and add new variations
|
|
|
|
need_update.removeClass( 'variation-needs-update' );
|
|
|
|
button.attr( 'disabled', 'disabled' );
|
2015-07-06 04:50:20 +00:00
|
|
|
|
2015-07-06 22:07:01 +00:00
|
|
|
wc_meta_boxes_product_variations_ajax.unblock();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-07-06 04:50:20 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2015-07-06 02:55:08 +00:00
|
|
|
/**
|
|
|
|
* Add variation
|
|
|
|
*
|
|
|
|
* @return {bool}
|
|
|
|
*/
|
|
|
|
add_variation: function() {
|
|
|
|
if ( ! wc_meta_boxes_product_variations_ajax.check_for_editions() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-06 04:50:20 +00:00
|
|
|
wc_meta_boxes_product_variations_ajax.block();
|
2015-07-06 02:55:08 +00:00
|
|
|
|
|
|
|
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() {
|
2015-07-07 23:22:13 +00:00
|
|
|
wc_meta_boxes_product_variations_pagenav.go_to_page( 1, 1 );
|
2015-07-06 02:55:08 +00:00
|
|
|
|
2015-07-06 04:50:20 +00:00
|
|
|
wc_meta_boxes_product_variations_ajax.unblock();
|
2015-07-06 02:55:08 +00:00
|
|
|
$( '#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'
|
|
|
|
};
|
|
|
|
|
2015-07-06 04:50:20 +00:00
|
|
|
wc_meta_boxes_product_variations_ajax.block();
|
2015-07-06 02:55:08 +00:00
|
|
|
|
|
|
|
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() {
|
2015-07-06 04:50:20 +00:00
|
|
|
var current = parseInt( $( '#variable_product_options .woocommerce_variations' ).attr( 'data-page' ), 10 );
|
2015-07-06 02:55:08 +00:00
|
|
|
|
2015-07-07 23:22:13 +00:00
|
|
|
wc_meta_boxes_product_variations_pagenav.go_to_page( current, -1 );
|
2015-07-06 02:55:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
2015-07-06 04:50:20 +00:00
|
|
|
wc_meta_boxes_product_variations_ajax.unblock();
|
2015-07-06 02:55:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 ) ) {
|
2015-07-06 04:50:20 +00:00
|
|
|
wc_meta_boxes_product_variations_ajax.block();
|
2015-07-06 02:55:08 +00:00
|
|
|
|
|
|
|
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 ) {
|
2015-07-07 23:22:13 +00:00
|
|
|
wc_meta_boxes_product_variations_pagenav.go_to_page( 1, count );
|
2015-07-06 02:55:08 +00:00
|
|
|
|
|
|
|
$( '#variable_product_options' ).trigger( 'woocommerce_variations_added' );
|
|
|
|
} else {
|
2015-07-06 04:50:20 +00:00
|
|
|
wc_meta_boxes_product_variations_ajax.unblock();
|
2015-07-06 02:55:08 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2015-07-06 01:31:56 +00:00
|
|
|
/**
|
|
|
|
* Add new class when have changes in some input
|
|
|
|
*/
|
|
|
|
input_changed: function() {
|
2015-07-06 22:07:01 +00:00
|
|
|
$( this ).closest( '.woocommerce_variation' ).addClass( 'variation-needs-update' );
|
|
|
|
$( '.save-variation-changes' ).removeAttr( 'disabled' );
|
2015-07-07 01:50:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bulk edit actions
|
|
|
|
*/
|
|
|
|
bulk_edit: function() {
|
2015-07-07 20:48:24 +00:00
|
|
|
if ( ! wc_meta_boxes_product_variations_ajax.check_for_editions() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-07 01:50:35 +00:00
|
|
|
var bulk_edit = $( 'select#field_to_edit' ).val(),
|
|
|
|
product_id = $( '#variable_product_options .woocommerce_variations' ).data( 'product_id' ),
|
|
|
|
data = {},
|
2015-07-07 23:22:13 +00:00
|
|
|
changes = 0,
|
2015-07-07 01:50:35 +00:00
|
|
|
value;
|
|
|
|
|
|
|
|
switch ( bulk_edit ) {
|
|
|
|
case 'delete_all' :
|
|
|
|
if ( window.confirm( woocommerce_admin_meta_boxes_variations.i18n_delete_all_variations ) ) {
|
|
|
|
if ( window.confirm( woocommerce_admin_meta_boxes_variations.i18n_last_warning ) ) {
|
|
|
|
data.allowed = true;
|
2015-07-07 23:22:13 +00:00
|
|
|
changes = parseInt( $( '#variable_product_options .woocommerce_variations' ).attr( 'data-total' ), 10 ) * -1;
|
2015-07-07 01:50:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'variable_regular_price_increase' :
|
|
|
|
case 'variable_regular_price_decrease' :
|
|
|
|
case 'variable_sale_price_increase' :
|
|
|
|
case 'variable_sale_price_decrease' :
|
2015-07-07 20:35:21 +00:00
|
|
|
value = window.prompt( woocommerce_admin_meta_boxes_variations.i18n_enter_a_value_fixed_or_percent );
|
|
|
|
|
|
|
|
if ( value != null ) {
|
|
|
|
if ( value.indexOf( '%' ) >= 0 ) {
|
|
|
|
data.value = accounting.unformat( value.replace( /\%/, '' ), woocommerce_admin.mon_decimal_point ) + '%';
|
|
|
|
} else {
|
|
|
|
data.value = accounting.unformat( value, woocommerce_admin.mon_decimal_point );
|
|
|
|
}
|
|
|
|
}
|
2015-07-07 01:50:35 +00:00
|
|
|
break;
|
|
|
|
case 'variable_regular_price' :
|
|
|
|
case 'variable_sale_price' :
|
|
|
|
case 'variable_stock' :
|
|
|
|
case 'variable_weight' :
|
|
|
|
case 'variable_length' :
|
|
|
|
case 'variable_width' :
|
|
|
|
case 'variable_height' :
|
|
|
|
case 'variable_download_limit' :
|
|
|
|
case 'variable_download_expiry' :
|
|
|
|
value = window.prompt( woocommerce_admin_meta_boxes_variations.i18n_enter_a_value );
|
|
|
|
|
|
|
|
if ( value != null ) {
|
|
|
|
data.value = value;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'variable_sale_schedule' :
|
2015-07-07 19:39:59 +00:00
|
|
|
data.date_from = window.prompt( woocommerce_admin_meta_boxes_variations.i18n_scheduled_sale_start );
|
|
|
|
data.date_to = window.prompt( woocommerce_admin_meta_boxes_variations.i18n_scheduled_sale_end );
|
|
|
|
|
|
|
|
if ( null === data.date_from ) {
|
|
|
|
data.date_from = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( null === data.date_to ) {
|
|
|
|
data.date_to = false;
|
|
|
|
}
|
2015-07-07 01:50:35 +00:00
|
|
|
break;
|
|
|
|
default :
|
|
|
|
$( 'select#field_to_edit' ).trigger( bulk_edit );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
wc_meta_boxes_product_variations_ajax.block();
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: woocommerce_admin_meta_boxes_variations.ajax_url,
|
|
|
|
data: {
|
|
|
|
action: 'woocommerce_bulk_edit_variations',
|
|
|
|
security: woocommerce_admin_meta_boxes_variations.bulk_edit_variations_nonce,
|
|
|
|
product_id: product_id,
|
|
|
|
bulk_action: bulk_edit,
|
|
|
|
data: data
|
|
|
|
},
|
|
|
|
type: 'POST',
|
|
|
|
success: function() {
|
2015-07-07 23:22:13 +00:00
|
|
|
wc_meta_boxes_product_variations_pagenav.go_to_page( 1, changes );
|
2015-07-07 01:50:35 +00:00
|
|
|
}
|
|
|
|
});
|
2015-07-02 20:42:22 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-06 01:00:38 +00:00
|
|
|
/**
|
|
|
|
* Product variations pagenav
|
|
|
|
*/
|
|
|
|
var wc_meta_boxes_product_variations_pagenav = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize products variations meta box
|
|
|
|
*/
|
|
|
|
init: function() {
|
2015-07-07 23:22:13 +00:00
|
|
|
$( document.body )
|
|
|
|
.on( 'change', '.variations-pagenav .page-selector', this.page_selector )
|
|
|
|
.on( 'click', '.variations-pagenav .first-page', this.first_page )
|
|
|
|
.on( 'click', '.variations-pagenav .prev-page', this.prev_page )
|
|
|
|
.on( 'click', '.variations-pagenav .next-page', this.next_page )
|
|
|
|
.on( 'click', '.variations-pagenav .last-page', this.last_page );
|
2015-07-06 04:09:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the pagenav fields
|
|
|
|
*
|
|
|
|
* @param {int} qty
|
|
|
|
*/
|
|
|
|
set_paginav: function( qty ) {
|
2015-07-06 04:50:20 +00:00
|
|
|
var wrapper = $( '#variable_product_options .woocommerce_variations' ),
|
2015-07-06 04:09:36 +00:00
|
|
|
total = parseInt( wrapper.attr( 'data-total' ), 10 ),
|
|
|
|
new_qty = total + qty,
|
2015-07-07 23:22:13 +00:00
|
|
|
page_nav = $( '.variations-pagenav' ),
|
|
|
|
displaying_num = $( '.displaying-num', page_nav ),
|
2015-07-06 04:09:36 +00:00
|
|
|
total_pages = Math.ceil( new_qty / woocommerce_admin_meta_boxes_variations.variations_per_page ),
|
|
|
|
options = '';
|
|
|
|
|
|
|
|
// Set the new total of variations
|
|
|
|
wrapper.attr( 'data-total', new_qty );
|
|
|
|
|
|
|
|
if ( 1 === new_qty ) {
|
|
|
|
displaying_num.text( woocommerce_admin_meta_boxes_variations.i18n_item.replace( '%qty%', new_qty ) );
|
|
|
|
} else {
|
|
|
|
displaying_num.text( woocommerce_admin_meta_boxes_variations.i18n_items.replace( '%qty%', new_qty ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the new total of pages
|
|
|
|
wrapper.attr( 'data-total_pages', total_pages );
|
|
|
|
|
2015-07-07 23:22:13 +00:00
|
|
|
$( '.total-pages', page_nav ).text( total_pages );
|
2015-07-06 04:09:36 +00:00
|
|
|
|
|
|
|
// Set the new pagenav options
|
|
|
|
for ( var i = 1; i <= total_pages; i++ ) {
|
|
|
|
options += '<option value="' + i + '">' + i + '</option>';
|
|
|
|
}
|
|
|
|
|
2015-07-07 23:22:13 +00:00
|
|
|
$( '.page-selector', page_nav ).empty().html( options );
|
|
|
|
|
|
|
|
// Show hide pagenav
|
|
|
|
if ( 0 === new_qty ) {
|
|
|
|
page_nav.closest( '.toolbar' ).hide();
|
|
|
|
} else {
|
|
|
|
page_nav.closest( '.toolbar' ).show();
|
|
|
|
}
|
2015-07-06 01:00:38 +00:00
|
|
|
},
|
|
|
|
|
2015-07-06 01:31:56 +00:00
|
|
|
/**
|
|
|
|
* Check button if enabled and if don't have editions
|
|
|
|
*
|
|
|
|
* @return {bool}
|
|
|
|
*/
|
|
|
|
check_is_enabled: function( current ) {
|
2015-07-06 02:55:08 +00:00
|
|
|
return ! $( current ).hasClass( 'disabled' ) && wc_meta_boxes_product_variations_ajax.check_for_editions();
|
2015-07-06 01:31:56 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change "disabled" class on pagenav
|
|
|
|
*/
|
|
|
|
change_classes: function( selected, total ) {
|
2015-07-06 04:09:36 +00:00
|
|
|
var first_page = $( '.variations-pagenav .first-page' ),
|
|
|
|
prev_page = $( '.variations-pagenav .prev-page' ),
|
|
|
|
next_page = $( '.variations-pagenav .next-page' ),
|
|
|
|
last_page = $( '.variations-pagenav .last-page' );
|
|
|
|
|
2015-07-06 01:00:38 +00:00
|
|
|
if ( 1 === selected ) {
|
2015-07-06 04:09:36 +00:00
|
|
|
first_page.addClass( 'disabled' );
|
|
|
|
prev_page.addClass( 'disabled' );
|
2015-07-06 01:00:38 +00:00
|
|
|
} else {
|
2015-07-06 04:09:36 +00:00
|
|
|
first_page.removeClass( 'disabled' );
|
|
|
|
prev_page.removeClass( 'disabled' );
|
2015-07-06 01:00:38 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 01:31:56 +00:00
|
|
|
if ( total === selected ) {
|
2015-07-06 04:09:36 +00:00
|
|
|
next_page.addClass( 'disabled' );
|
|
|
|
last_page.addClass( 'disabled' );
|
2015-07-06 01:00:38 +00:00
|
|
|
} else {
|
2015-07-06 04:09:36 +00:00
|
|
|
next_page.removeClass( 'disabled' );
|
|
|
|
last_page.removeClass( 'disabled' );
|
2015-07-06 01:00:38 +00:00
|
|
|
}
|
2015-07-06 01:31:56 +00:00
|
|
|
},
|
|
|
|
|
2015-07-06 02:55:08 +00:00
|
|
|
/**
|
|
|
|
* Set page
|
|
|
|
*/
|
|
|
|
set_page: function( page ) {
|
2015-07-07 23:22:13 +00:00
|
|
|
$( '.variations-pagenav .page-selector:first' ).val( page ).change();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigate on variations pages
|
|
|
|
*
|
|
|
|
* @param {int} page
|
|
|
|
* @param {int} qty
|
|
|
|
*/
|
|
|
|
go_to_page: function( page, qty ) {
|
|
|
|
page = page || 1;
|
|
|
|
qty = qty || 0;
|
|
|
|
|
|
|
|
wc_meta_boxes_product_variations_pagenav.set_paginav( qty );
|
|
|
|
wc_meta_boxes_product_variations_pagenav.set_page( page );
|
2015-07-06 02:55:08 +00:00
|
|
|
},
|
|
|
|
|
2015-07-06 01:31:56 +00:00
|
|
|
/**
|
|
|
|
* Paginav pagination selector
|
|
|
|
*/
|
|
|
|
page_selector: function() {
|
|
|
|
var selected = parseInt( $( this ).val(), 10 ),
|
2015-07-06 04:50:20 +00:00
|
|
|
wrapper = $( '#variable_product_options .woocommerce_variations' );
|
2015-07-06 01:00:38 +00:00
|
|
|
|
2015-07-06 02:55:08 +00:00
|
|
|
if ( wc_meta_boxes_product_variations_ajax.check_for_editions() ) {
|
2015-07-06 04:09:36 +00:00
|
|
|
wc_meta_boxes_product_variations_pagenav.change_classes( selected, parseInt( wrapper.attr( 'data-total_pages' ), 10 ) );
|
2015-07-06 01:31:56 +00:00
|
|
|
wc_meta_boxes_product_variations_ajax.load_variations( selected );
|
|
|
|
} else {
|
2015-07-06 02:55:08 +00:00
|
|
|
$( this ).val( parseInt( wrapper.data( 'page' ), 10 ) );
|
2015-07-06 01:31:56 +00:00
|
|
|
}
|
2015-07-06 01:00:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Go to first page
|
|
|
|
*
|
|
|
|
* @return {bool}
|
|
|
|
*/
|
|
|
|
first_page: function() {
|
2015-07-06 01:31:56 +00:00
|
|
|
if ( wc_meta_boxes_product_variations_pagenav.check_is_enabled( this ) ) {
|
2015-07-06 02:55:08 +00:00
|
|
|
wc_meta_boxes_product_variations_pagenav.set_page( 1 );
|
2015-07-06 01:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Go to previous page
|
|
|
|
*
|
|
|
|
* @return {bool}
|
|
|
|
*/
|
|
|
|
prev_page: function() {
|
2015-07-06 01:31:56 +00:00
|
|
|
if ( wc_meta_boxes_product_variations_pagenav.check_is_enabled( this ) ) {
|
2015-07-06 04:50:20 +00:00
|
|
|
var wrapper = $( '#variable_product_options .woocommerce_variations' ),
|
2015-07-06 02:55:08 +00:00
|
|
|
prev_page = parseInt( wrapper.data( 'page' ), 10 ) - 1,
|
2015-07-06 01:00:38 +00:00
|
|
|
new_page = ( 0 < prev_page ) ? prev_page : 1;
|
|
|
|
|
2015-07-06 02:55:08 +00:00
|
|
|
wc_meta_boxes_product_variations_pagenav.set_page( new_page );
|
2015-07-06 01:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Go to next page
|
|
|
|
*
|
|
|
|
* @return {bool}
|
|
|
|
*/
|
|
|
|
next_page: function() {
|
2015-07-06 01:31:56 +00:00
|
|
|
if ( wc_meta_boxes_product_variations_pagenav.check_is_enabled( this ) ) {
|
2015-07-06 04:50:20 +00:00
|
|
|
var wrapper = $( '#variable_product_options .woocommerce_variations' ),
|
2015-07-06 04:09:36 +00:00
|
|
|
total_pages = parseInt( wrapper.data( 'total_pages' ), 10 ),
|
2015-07-06 02:55:08 +00:00
|
|
|
next_page = parseInt( wrapper.data( 'page' ), 10 ) + 1,
|
2015-07-06 01:00:38 +00:00
|
|
|
new_page = ( total_pages >= next_page ) ? next_page : total_pages;
|
|
|
|
|
2015-07-06 02:55:08 +00:00
|
|
|
wc_meta_boxes_product_variations_pagenav.set_page( new_page );
|
2015-07-06 01:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Go to last page
|
|
|
|
*
|
|
|
|
* @return {bool}
|
|
|
|
*/
|
|
|
|
last_page: function() {
|
2015-07-06 01:31:56 +00:00
|
|
|
if ( wc_meta_boxes_product_variations_pagenav.check_is_enabled( this ) ) {
|
2015-07-06 04:50:20 +00:00
|
|
|
var last_page = $( '#variable_product_options .woocommerce_variations' ).data( 'total_pages' );
|
2015-07-06 01:00:38 +00:00
|
|
|
|
2015-07-06 02:55:08 +00:00
|
|
|
wc_meta_boxes_product_variations_pagenav.set_page( last_page );
|
2015-07-06 01:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
wc_meta_boxes_product_variations_actions.init();
|
|
|
|
wc_meta_boxes_product_variations_media.init();
|
2015-07-06 01:00:38 +00:00
|
|
|
wc_meta_boxes_product_variations_ajax.init();
|
|
|
|
wc_meta_boxes_product_variations_pagenav.init();
|
2015-07-02 20:42:22 +00:00
|
|
|
|
2014-02-14 20:24:48 +00:00
|
|
|
});
|