2015-07-07 20:35:21 +00:00
|
|
|
/* global wp, woocommerce_admin_meta_boxes_variations, woocommerce_admin, accounting */
|
2017-10-31 19:27:57 +00:00
|
|
|
jQuery( function( $ ) {
|
2017-10-13 12:23:26 +00:00
|
|
|
'use strict';
|
2017-10-31 19:27:57 +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 )
|
2015-07-29 22:56:33 +00:00
|
|
|
.on( 'change', 'input.variable_manage_stock', this.variable_manage_stock )
|
2015-07-31 11:39:23 +00:00
|
|
|
.on( 'click', 'button.notice-dismiss', this.notice_dismiss )
|
2015-09-03 15:17:08 +00:00
|
|
|
.on( 'click', 'h3 .sort', this.set_menu_order )
|
|
|
|
.on( 'reload', this.reload );
|
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();
|
2015-07-07 01:50:35 +00:00
|
|
|
$( '#woocommerce-product-data' ).on( 'woocommerce_variations_loaded', this.variations_loaded );
|
2015-07-15 15:26:38 +00:00
|
|
|
$( document.body ).on( 'woocommerce_variations_added', this.variation_added );
|
2015-07-06 23:05:09 +00:00
|
|
|
},
|
|
|
|
|
2015-09-03 15:17:08 +00:00
|
|
|
/**
|
|
|
|
* Reload UI
|
|
|
|
*
|
|
|
|
* @param {Object} event
|
|
|
|
* @param {Int} qty
|
|
|
|
*/
|
|
|
|
reload: function() {
|
|
|
|
wc_meta_boxes_product_variations_ajax.load_variations( 1 );
|
2016-11-03 11:28:58 +00:00
|
|
|
wc_meta_boxes_product_variations_pagenav.set_paginav( 0 );
|
2015-09-03 15:17:08 +00:00
|
|
|
},
|
|
|
|
|
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();
|
2019-03-18 12:56:49 +00:00
|
|
|
$( this ).closest( '.woocommerce_variation' ).find( '.variable_stock_status' ).show();
|
2015-07-06 23:05:09 +00:00
|
|
|
|
|
|
|
if ( $( this ).is( ':checked' ) ) {
|
|
|
|
$( this ).closest( '.woocommerce_variation' ).find( '.show_if_variation_manage_stock' ).show();
|
2019-03-18 12:56:49 +00:00
|
|
|
$( this ).closest( '.woocommerce_variation' ).find( '.variable_stock_status' ).hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent level.
|
|
|
|
if ( $( 'input#_manage_stock:checked' ).length ) {
|
|
|
|
$( this ).closest( '.woocommerce_variation' ).find( '.variable_stock_status' ).hide();
|
2015-07-06 23:05:09 +00:00
|
|
|
}
|
2015-07-07 01:50:35 +00:00
|
|
|
},
|
|
|
|
|
2015-07-29 22:56:33 +00:00
|
|
|
/**
|
|
|
|
* Notice dismiss
|
|
|
|
*/
|
|
|
|
notice_dismiss: function() {
|
|
|
|
$( this ).closest( 'div.notice' ).remove();
|
|
|
|
},
|
|
|
|
|
2015-07-07 01:50:35 +00:00
|
|
|
/**
|
|
|
|
* Run actions when variations is loaded
|
2015-08-18 16:00:38 +00:00
|
|
|
*
|
|
|
|
* @param {Object} event
|
|
|
|
* @param {Int} needsUpdate
|
2015-07-07 01:50:35 +00:00
|
|
|
*/
|
2015-08-18 16:00:38 +00:00
|
|
|
variations_loaded: function( event, needsUpdate ) {
|
|
|
|
needsUpdate = needsUpdate || false;
|
|
|
|
|
2015-07-15 15:26:38 +00:00
|
|
|
var wrapper = $( '#woocommerce-product-data' );
|
|
|
|
|
2015-08-18 16:00:38 +00:00
|
|
|
if ( ! needsUpdate ) {
|
|
|
|
// Show/hide downloadable, virtual and stock fields
|
|
|
|
$( 'input.variable_is_downloadable, input.variable_is_virtual, input.variable_manage_stock', wrapper ).change();
|
2015-07-08 18:05:52 +00:00
|
|
|
|
2015-08-18 16:00:38 +00:00
|
|
|
// Open sale schedule fields when have some sale price date
|
|
|
|
$( '.woocommerce_variation', wrapper ).each( function( index, el ) {
|
|
|
|
var $el = $( el ),
|
|
|
|
date_from = $( '.sale_price_dates_from', $el ).val(),
|
|
|
|
date_to = $( '.sale_price_dates_to', $el ).val();
|
2015-07-07 19:39:59 +00:00
|
|
|
|
2015-08-18 16:00:38 +00:00
|
|
|
if ( '' !== date_from || '' !== date_to ) {
|
|
|
|
$( 'a.sale_schedule', $el ).click();
|
|
|
|
}
|
|
|
|
});
|
2015-07-08 18:05:52 +00:00
|
|
|
|
2015-08-18 16:00:38 +00:00
|
|
|
// Remove variation-needs-update classes
|
|
|
|
$( '.woocommerce_variations .variation-needs-update', wrapper ).removeClass( 'variation-needs-update' );
|
2015-07-08 18:05:52 +00:00
|
|
|
|
2015-08-18 16:00:38 +00:00
|
|
|
// Disable cancel and save buttons
|
|
|
|
$( 'button.cancel-variation-changes, button.save-variation-changes', wrapper ).attr( 'disabled', 'disabled' );
|
|
|
|
}
|
2015-07-08 18:05:52 +00:00
|
|
|
|
|
|
|
// Init TipTip
|
|
|
|
$( '#tiptip_holder' ).removeAttr( 'style' );
|
|
|
|
$( '#tiptip_arrow' ).removeAttr( 'style' );
|
2019-05-13 11:36:21 +00:00
|
|
|
$( '.woocommerce_variations .tips, .woocommerce_variations .help_tip, .woocommerce_variations .woocommerce-help-tip', wrapper )
|
|
|
|
.tipTip({
|
|
|
|
'attribute': 'data-tip',
|
|
|
|
'fadeIn': 50,
|
|
|
|
'fadeOut': 50,
|
|
|
|
'delay': 200
|
2015-07-08 18:23:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Datepicker fields
|
2017-02-22 10:31:20 +00:00
|
|
|
$( '.sale_price_dates_fields', wrapper ).find( 'input' ).datepicker({
|
|
|
|
defaultDate: '',
|
|
|
|
dateFormat: 'yy-mm-dd',
|
|
|
|
numberOfMonths: 1,
|
|
|
|
showButtonPanel: true,
|
2017-07-13 11:15:58 +00:00
|
|
|
onSelect: function() {
|
2017-02-22 10:31:20 +00:00
|
|
|
var option = $( this ).is( '.sale_price_dates_from' ) ? 'minDate' : 'maxDate',
|
|
|
|
dates = $( this ).closest( '.sale_price_dates_fields' ).find( 'input' ),
|
2017-07-13 11:15:58 +00:00
|
|
|
date = $( this ).datepicker( 'getDate' );
|
2017-02-22 10:31:20 +00:00
|
|
|
|
|
|
|
dates.not( this ).datepicker( 'option', option, date );
|
|
|
|
$( this ).change();
|
|
|
|
}
|
2015-07-08 18:05:52 +00:00
|
|
|
});
|
2015-07-08 18:31:39 +00:00
|
|
|
|
2015-07-31 11:39:23 +00:00
|
|
|
// Allow sorting
|
2015-08-18 16:00:38 +00:00
|
|
|
$( '.woocommerce_variations', wrapper ).sortable({
|
|
|
|
items: '.woocommerce_variation',
|
|
|
|
cursor: 'move',
|
|
|
|
axis: 'y',
|
|
|
|
handle: '.sort',
|
|
|
|
scrollSensitivity: 40,
|
2015-07-31 11:39:23 +00:00
|
|
|
forcePlaceholderSize: true,
|
2015-08-18 16:00:38 +00:00
|
|
|
helper: 'clone',
|
|
|
|
opacity: 0.65,
|
|
|
|
stop: function() {
|
2015-07-31 11:39:23 +00:00
|
|
|
wc_meta_boxes_product_variations_actions.variation_row_indexes();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-07-08 18:31:39 +00:00
|
|
|
$( document.body ).trigger( 'wc-enhanced-select-init' );
|
2015-07-15 15:26:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run actions when added a variation
|
|
|
|
*
|
2015-08-18 16:00:38 +00:00
|
|
|
* @param {Object} event
|
2015-07-15 15:26:38 +00:00
|
|
|
* @param {Int} qty
|
|
|
|
*/
|
|
|
|
variation_added: function( event, qty ) {
|
|
|
|
if ( 1 === qty ) {
|
2015-08-18 16:00:38 +00:00
|
|
|
wc_meta_boxes_product_variations_actions.variations_loaded( null, true );
|
2015-07-15 15:26:38 +00:00
|
|
|
}
|
2015-07-31 11:39:23 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lets the user manually input menu order to move items around pages
|
|
|
|
*/
|
|
|
|
set_menu_order: function( event ) {
|
|
|
|
event.preventDefault();
|
|
|
|
var $menu_order = $( this ).closest( '.woocommerce_variation' ).find('.variation_menu_order');
|
|
|
|
var value = window.prompt( woocommerce_admin_meta_boxes_variations.i18n_enter_menu_order, $menu_order.val() );
|
|
|
|
|
|
|
|
if ( value != null ) {
|
|
|
|
// Set value, save changes and reload view
|
|
|
|
$menu_order.val( parseInt( value, 10 ) ).change();
|
|
|
|
wc_meta_boxes_product_variations_ajax.save_variations();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set menu order
|
|
|
|
*/
|
|
|
|
variation_row_indexes: function() {
|
2015-09-07 17:51:10 +00:00
|
|
|
var wrapper = $( '#variable_product_options' ).find( '.woocommerce_variations' ),
|
2015-07-31 11:39:23 +00:00
|
|
|
current_page = parseInt( wrapper.attr( 'data-page' ), 10 ),
|
|
|
|
offset = parseInt( ( current_page - 1 ) * woocommerce_admin_meta_boxes_variations.variations_per_page, 10 );
|
|
|
|
|
|
|
|
$( '.woocommerce_variations .woocommerce_variation' ).each( function ( index, el ) {
|
2019-05-13 11:36:21 +00:00
|
|
|
$( '.variation_menu_order', el )
|
|
|
|
.val( parseInt( $( el )
|
|
|
|
.index( '.woocommerce_variations .woocommerce_variation' ), 10 ) + 1 + offset )
|
|
|
|
.change();
|
2015-07-31 11:39:23 +00:00
|
|
|
});
|
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
|
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @type {Object}
|
2015-07-06 23:05:09 +00:00
|
|
|
*/
|
|
|
|
variable_image_frame: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Variation image ID
|
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @type {Int}
|
2015-07-06 23:05:09 +00:00
|
|
|
*/
|
|
|
|
setting_variation_image_id: null,
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
/**
|
|
|
|
* Variation image object
|
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @type {Object}
|
2015-07-06 23:05:09 +00:00
|
|
|
*/
|
|
|
|
setting_variation_image: null,
|
2013-08-06 10:41:20 +00:00
|
|
|
|
2015-07-06 23:05:09 +00:00
|
|
|
/**
|
|
|
|
* wp.media post ID
|
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @type {Int}
|
2015-07-06 23:05:09 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @param {Object} event
|
2015-07-06 23:05:09 +00:00
|
|
|
*/
|
|
|
|
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();
|
2019-05-13 11:36:21 +00:00
|
|
|
wc_meta_boxes_product_variations_media.setting_variation_image.find( 'img' ).eq( 0 )
|
|
|
|
.attr( 'src', woocommerce_admin_meta_boxes_variations.woocommerce_placeholder_img_src );
|
2015-07-06 23:05:09 +00:00
|
|
|
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 ) {
|
2019-05-13 11:36:21 +00:00
|
|
|
wc_meta_boxes_product_variations_media.variable_image_frame.uploader.uploader
|
|
|
|
.param( 'post_id', wc_meta_boxes_product_variations_media.setting_variation_image_id );
|
2015-07-06 23:05:09 +00:00
|
|
|
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
|
|
|
|
2019-05-13 11:36:21 +00:00
|
|
|
var attachment = wc_meta_boxes_product_variations_media.variable_image_frame.state()
|
|
|
|
.get( 'selection' ).first().toJSON(),
|
2015-11-13 00:55:25 +00:00
|
|
|
url = attachment.sizes && 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-08 19:10:45 +00:00
|
|
|
.on( 'click', 'button.cancel-variation-changes', this.cancel_variations )
|
2019-05-13 11:32:30 +00:00
|
|
|
.on( 'click', '.remove_variation', this.remove_variation )
|
|
|
|
.on( 'click','.downloadable_files a.delete', this.input_changed );
|
2015-07-06 02:55:08 +00:00
|
|
|
|
2015-07-09 01:50:32 +00:00
|
|
|
$( document.body )
|
|
|
|
.on( 'change', '#variable_product_options .woocommerce_variations :input', this.input_changed )
|
|
|
|
.on( 'change', '.variations-defaults select', this.defaults_changed );
|
2015-07-08 20:18:57 +00:00
|
|
|
|
2019-01-09 16:31:53 +00:00
|
|
|
var postForm = $( 'form#post' );
|
|
|
|
|
|
|
|
postForm.on( 'submit', this.save_on_submit );
|
|
|
|
|
|
|
|
$( 'input:submit', postForm ).bind( 'click keypress', function() {
|
|
|
|
postForm.data( 'callerid', this.id );
|
|
|
|
});
|
2015-07-07 01:50:35 +00:00
|
|
|
|
2015-07-10 23:04:16 +00:00
|
|
|
$( '.wc-metaboxes-wrapper' ).on( 'click', 'a.do_variation_action', this.do_variation_action );
|
2015-07-02 20:42:22 +00:00
|
|
|
},
|
|
|
|
|
2015-07-06 02:55:08 +00:00
|
|
|
/**
|
2015-07-08 19:10:45 +00:00
|
|
|
* Check if have some changes before leave the page
|
2015-07-06 02:55:08 +00:00
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @return {Bool}
|
2015-07-06 02:55:08 +00:00
|
|
|
*/
|
2015-07-08 19:10:45 +00:00
|
|
|
check_for_changes: function() {
|
2015-09-07 17:51:10 +00:00
|
|
|
var need_update = $( '#variable_product_options' ).find( '.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-08 20:45:19 +00:00
|
|
|
wc_meta_boxes_product_variations_ajax.save_changes();
|
2015-07-06 02:55:08 +00:00
|
|
|
} else {
|
2015-07-08 20:45:19 +00:00
|
|
|
need_update.removeClass( 'variation-needs-update' );
|
2015-07-06 02:55:08 +00:00
|
|
|
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
|
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @return {Bool}
|
2015-07-02 20:42:22 +00:00
|
|
|
*/
|
|
|
|
initial_load: function() {
|
2015-09-07 17:51:10 +00:00
|
|
|
if ( 0 === $( '#variable_product_options' ).find( '.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
|
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @param {Int} page (default: 1)
|
|
|
|
* @param {Int} per_page (default: 10)
|
2015-07-02 20:42:22 +00:00
|
|
|
*/
|
|
|
|
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-09-07 17:51:10 +00:00
|
|
|
var wrapper = $( '#variable_product_options' ).find( '.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,
|
2015-07-09 20:31:48 +00:00
|
|
|
product_id: woocommerce_admin_meta_boxes_variations.post_id,
|
2015-07-02 20:42:22 +00:00
|
|
|
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-08 16:55:29 +00:00
|
|
|
/**
|
|
|
|
* Ger variations fields and convert to object
|
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @param {Object} fields
|
2015-07-08 16:55:29 +00:00
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @return {Object}
|
2015-07-08 16:55:29 +00:00
|
|
|
*/
|
|
|
|
get_variations_fields: function( fields ) {
|
2020-03-17 15:55:09 +00:00
|
|
|
var data = $( ':input', fields ).serializeJSON();
|
2020-01-31 08:23:28 +00:00
|
|
|
|
2015-07-09 01:50:32 +00:00
|
|
|
$( '.variations-defaults select' ).each( function( index, element ) {
|
|
|
|
var select = $( element );
|
|
|
|
data[ select.attr( 'name' ) ] = select.val();
|
|
|
|
});
|
|
|
|
|
2015-07-08 16:55:29 +00:00
|
|
|
return data;
|
|
|
|
},
|
|
|
|
|
2015-07-06 04:50:20 +00:00
|
|
|
/**
|
2015-07-08 20:18:57 +00:00
|
|
|
* Save variations changes
|
2015-07-06 04:50:20 +00:00
|
|
|
*
|
2015-09-08 13:30:39 +00:00
|
|
|
* @param {Function} callback Called once saving is complete
|
2015-07-06 04:50:20 +00:00
|
|
|
*/
|
2015-07-08 20:18:57 +00:00
|
|
|
save_changes: function( callback ) {
|
2015-09-07 17:51:10 +00:00
|
|
|
var wrapper = $( '#variable_product_options' ).find( '.woocommerce_variations' ),
|
2015-07-08 16:55:29 +00:00
|
|
|
need_update = $( '.variation-needs-update', wrapper ),
|
|
|
|
data = {};
|
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
|
|
|
|
Save product type before saving variations
WooCommerce prior to WC 2.4 saved the product type before any variations were saved because
WC_Meta_Box_Product_Data::save_variations() was called by WC_Meta_Box_Product_Data::save().
However, in WC 2.4 the variations are saved independently of other data about the containing
variable product, including product type. Because the product type hasn't been saved yet,
extensions that need to save their own variation level meta data can't know when saving
variations if the product is of the type they want to act on. They also can't check `$_POST`
to find out when saving variations, because 'product-type' isn't passed to that as it's
variable level meta data, not variation level meta data.
This patch passes the product type along with the variation level meta data when saving variations.
It then uses that to save the product type if the variable product has not yet been saved (and
therefore the product type has never been stored, which means calling get_product() would instantiate
a 'simple' product, as that is the default product type). This can lead to fatal errors if callbacks
expect the product type to be variable and attempt to call methods that only exist on those product
types, like variable_product_sync().
It will also update the product type if it was previously saved but has since changed. This prevents
fatal errors like that mentioned above but caused by switching from one product type, like a simple
product, to another, like a variable product.
2015-07-31 18:42:21 +00:00
|
|
|
data = wc_meta_boxes_product_variations_ajax.get_variations_fields( need_update );
|
|
|
|
data.action = 'woocommerce_save_variations';
|
|
|
|
data.security = woocommerce_admin_meta_boxes_variations.save_variations_nonce;
|
|
|
|
data.product_id = woocommerce_admin_meta_boxes_variations.post_id;
|
|
|
|
data['product-type'] = $( '#product-type' ).val();
|
2015-07-08 16:55:29 +00:00
|
|
|
|
2015-07-06 22:07:01 +00:00
|
|
|
$.ajax({
|
|
|
|
url: woocommerce_admin_meta_boxes_variations.ajax_url,
|
2015-07-08 16:55:29 +00:00
|
|
|
data: data,
|
2015-07-06 22:07:01 +00:00
|
|
|
type: 'POST',
|
2015-07-27 17:16:52 +00:00
|
|
|
success: function( response ) {
|
2015-07-06 22:07:01 +00:00
|
|
|
// Allow change page, delete and add new variations
|
|
|
|
need_update.removeClass( 'variation-needs-update' );
|
2015-07-08 20:18:57 +00:00
|
|
|
$( 'button.cancel-variation-changes, button.save-variation-changes' ).attr( 'disabled', 'disabled' );
|
|
|
|
|
2015-07-10 21:22:05 +00:00
|
|
|
$( '#woocommerce-product-data' ).trigger( 'woocommerce_variations_saved' );
|
|
|
|
|
2015-07-08 20:18:57 +00:00
|
|
|
if ( typeof callback === 'function' ) {
|
2015-07-27 17:16:52 +00:00
|
|
|
callback( response );
|
2015-07-08 20:18:57 +00:00
|
|
|
}
|
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-08 20:18:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save variations
|
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @return {Bool}
|
2015-07-08 20:18:57 +00:00
|
|
|
*/
|
|
|
|
save_variations: function() {
|
2015-07-09 20:31:48 +00:00
|
|
|
$( '#variable_product_options' ).trigger( 'woocommerce_variations_save_variations_button' );
|
|
|
|
|
2015-07-27 17:16:52 +00:00
|
|
|
wc_meta_boxes_product_variations_ajax.save_changes( function( error ) {
|
2015-09-07 17:51:10 +00:00
|
|
|
var wrapper = $( '#variable_product_options' ).find( '.woocommerce_variations' ),
|
2015-07-27 17:16:52 +00:00
|
|
|
current = wrapper.attr( 'data-page' );
|
|
|
|
|
2015-09-07 17:51:10 +00:00
|
|
|
$( '#variable_product_options' ).find( '#woocommerce_errors' ).remove();
|
2015-07-27 17:16:52 +00:00
|
|
|
|
|
|
|
if ( error ) {
|
|
|
|
wrapper.before( error );
|
|
|
|
}
|
2015-07-10 21:22:05 +00:00
|
|
|
|
2015-07-10 23:16:35 +00:00
|
|
|
$( '.variations-defaults select' ).each( function() {
|
|
|
|
$( this ).attr( 'data-current', $( this ).val() );
|
|
|
|
});
|
|
|
|
|
2015-07-10 21:22:05 +00:00
|
|
|
wc_meta_boxes_product_variations_pagenav.go_to_page( current );
|
2015-07-08 21:59:51 +00:00
|
|
|
});
|
2015-07-06 04:50:20 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2015-07-08 20:18:57 +00:00
|
|
|
/**
|
|
|
|
* Save on post form submit
|
|
|
|
*/
|
2015-09-08 13:30:39 +00:00
|
|
|
save_on_submit: function( e ) {
|
|
|
|
var need_update = $( '#variable_product_options' ).find( '.woocommerce_variations .variation-needs-update' );
|
2015-07-09 20:31:48 +00:00
|
|
|
|
2015-09-08 13:30:39 +00:00
|
|
|
if ( 0 < need_update.length ) {
|
|
|
|
e.preventDefault();
|
|
|
|
$( '#variable_product_options' ).trigger( 'woocommerce_variations_save_variations_on_submit' );
|
|
|
|
wc_meta_boxes_product_variations_ajax.save_changes( wc_meta_boxes_product_variations_ajax.save_on_submit_done );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* After saved, continue with form submission
|
|
|
|
*/
|
|
|
|
save_on_submit_done: function() {
|
2019-01-09 16:31:53 +00:00
|
|
|
var postForm = $( 'form#post' ),
|
|
|
|
callerid = postForm.data( 'callerid' );
|
|
|
|
|
|
|
|
if ( 'publish' === callerid ) {
|
|
|
|
postForm.append('<input type="hidden" name="publish" value="1" />').submit();
|
2018-09-10 09:23:12 +00:00
|
|
|
} else {
|
2019-01-09 16:31:53 +00:00
|
|
|
postForm.append('<input type="hidden" name="save-post" value="1" />').submit();
|
2018-09-10 09:23:12 +00:00
|
|
|
}
|
2015-07-08 20:18:57 +00:00
|
|
|
},
|
|
|
|
|
2015-07-08 19:10:45 +00:00
|
|
|
/**
|
|
|
|
* Discart changes.
|
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @return {Bool}
|
2015-07-08 19:10:45 +00:00
|
|
|
*/
|
|
|
|
cancel_variations: function() {
|
2015-09-07 17:51:10 +00:00
|
|
|
var current = parseInt( $( '#variable_product_options' ).find( '.woocommerce_variations' ).attr( 'data-page' ), 10 );
|
2015-07-08 19:10:45 +00:00
|
|
|
|
2019-05-13 11:36:21 +00:00
|
|
|
$( '#variable_product_options' ).find( '.woocommerce_variations .variation-needs-update' )
|
|
|
|
.removeClass( 'variation-needs-update' );
|
2015-07-10 23:16:35 +00:00
|
|
|
$( '.variations-defaults select' ).each( function() {
|
|
|
|
$( this ).val( $( this ).attr( 'data-current' ) );
|
|
|
|
});
|
|
|
|
|
2015-07-08 19:10:45 +00:00
|
|
|
wc_meta_boxes_product_variations_pagenav.go_to_page( current );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2015-07-06 02:55:08 +00:00
|
|
|
/**
|
|
|
|
* Add variation
|
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @return {Bool}
|
2015-07-06 02:55:08 +00:00
|
|
|
*/
|
|
|
|
add_variation: function() {
|
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,
|
2016-03-01 13:08:25 +00:00
|
|
|
loop: $( '.woocommerce_variation' ).length,
|
2015-07-06 02:55:08 +00:00
|
|
|
security: woocommerce_admin_meta_boxes_variations.add_variation_nonce
|
|
|
|
};
|
|
|
|
|
2015-07-10 21:31:38 +00:00
|
|
|
$.post( woocommerce_admin_meta_boxes_variations.ajax_url, data, function( response ) {
|
|
|
|
var variation = $( response );
|
|
|
|
variation.addClass( 'variation-needs-update' );
|
|
|
|
|
2019-03-25 13:30:04 +00:00
|
|
|
$( '.woocommerce-notice-invalid-variation' ).remove();
|
2015-09-07 17:51:10 +00:00
|
|
|
$( '#variable_product_options' ).find( '.woocommerce_variations' ).prepend( variation );
|
2015-07-10 21:31:38 +00:00
|
|
|
$( 'button.cancel-variation-changes, button.save-variation-changes' ).removeAttr( 'disabled' );
|
2015-07-10 21:22:05 +00:00
|
|
|
$( '#variable_product_options' ).trigger( 'woocommerce_variations_added', 1 );
|
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;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove variation
|
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @return {Bool}
|
2015-07-06 02:55:08 +00:00
|
|
|
*/
|
|
|
|
remove_variation: function() {
|
2015-07-08 20:45:19 +00:00
|
|
|
wc_meta_boxes_product_variations_ajax.check_for_changes();
|
2015-07-06 02:55:08 +00:00
|
|
|
|
|
|
|
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-09-07 17:51:10 +00:00
|
|
|
var wrapper = $( '#variable_product_options' ).find( '.woocommerce_variations' ),
|
2015-07-10 21:57:38 +00:00
|
|
|
current_page = parseInt( wrapper.attr( 'data-page' ), 10 ),
|
2019-05-13 11:36:21 +00:00
|
|
|
total_pages = Math.ceil( (
|
|
|
|
parseInt( wrapper.attr( 'data-total' ), 10 ) - 1
|
|
|
|
) / woocommerce_admin_meta_boxes_variations.variations_per_page ),
|
2015-07-10 21:57:38 +00:00
|
|
|
page = 1;
|
2015-07-06 02:55:08 +00:00
|
|
|
|
2015-07-09 20:31:48 +00:00
|
|
|
$( '#woocommerce-product-data' ).trigger( 'woocommerce_variations_removed' );
|
2015-07-10 21:57:38 +00:00
|
|
|
|
2015-08-12 14:39:55 +00:00
|
|
|
if ( current_page === total_pages || current_page <= total_pages ) {
|
2015-07-10 21:57:38 +00:00
|
|
|
page = current_page;
|
|
|
|
} else if ( current_page > total_pages && 0 !== total_pages ) {
|
|
|
|
page = total_pages;
|
|
|
|
}
|
|
|
|
|
|
|
|
wc_meta_boxes_product_variations_pagenav.go_to_page( page, -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)
|
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @return {Bool}
|
2015-07-06 02:55:08 +00:00
|
|
|
*/
|
|
|
|
link_all_variations: function() {
|
2015-07-08 20:45:19 +00:00
|
|
|
wc_meta_boxes_product_variations_ajax.check_for_changes();
|
2015-07-06 02:55:08 +00:00
|
|
|
|
|
|
|
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-10 21:22:05 +00:00
|
|
|
$( '#variable_product_options' ).trigger( 'woocommerce_variations_added', count );
|
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;
|
|
|
|
},
|
|
|
|
|
2015-07-06 01:31:56 +00:00
|
|
|
/**
|
|
|
|
* Add new class when have changes in some input
|
|
|
|
*/
|
|
|
|
input_changed: function() {
|
2015-07-09 01:50:32 +00:00
|
|
|
$( this )
|
|
|
|
.closest( '.woocommerce_variation' )
|
|
|
|
.addClass( 'variation-needs-update' );
|
|
|
|
|
|
|
|
$( 'button.cancel-variation-changes, button.save-variation-changes' ).removeAttr( 'disabled' );
|
2015-07-09 20:31:48 +00:00
|
|
|
|
|
|
|
$( '#variable_product_options' ).trigger( 'woocommerce_variations_input_changed' );
|
2015-07-09 01:50:32 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Added new .variation-needs-update class when defaults is changed
|
|
|
|
*/
|
|
|
|
defaults_changed: function() {
|
|
|
|
$( this )
|
|
|
|
.closest( '#variable_product_options' )
|
|
|
|
.find( '.woocommerce_variation:first' )
|
|
|
|
.addClass( 'variation-needs-update' );
|
|
|
|
|
2015-07-08 19:10:45 +00:00
|
|
|
$( 'button.cancel-variation-changes, button.save-variation-changes' ).removeAttr( 'disabled' );
|
2015-07-09 20:31:48 +00:00
|
|
|
|
|
|
|
$( '#variable_product_options' ).trigger( 'woocommerce_variations_defaults_changed' );
|
2015-07-07 01:50:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-07-10 23:04:16 +00:00
|
|
|
* Actions
|
2015-07-07 01:50:35 +00:00
|
|
|
*/
|
2015-07-10 23:04:16 +00:00
|
|
|
do_variation_action: function() {
|
|
|
|
var do_variation_action = $( 'select.variation_actions' ).val(),
|
2015-07-07 01:50:35 +00:00
|
|
|
data = {},
|
2015-07-07 23:22:13 +00:00
|
|
|
changes = 0,
|
2015-07-07 01:50:35 +00:00
|
|
|
value;
|
|
|
|
|
2015-07-10 23:04:16 +00:00
|
|
|
switch ( do_variation_action ) {
|
|
|
|
case 'add_variation' :
|
|
|
|
wc_meta_boxes_product_variations_ajax.add_variation();
|
|
|
|
return;
|
2015-07-08 22:34:21 +00:00
|
|
|
case 'link_all_variations' :
|
|
|
|
wc_meta_boxes_product_variations_ajax.link_all_variations();
|
|
|
|
return;
|
2015-07-07 01:50:35 +00:00
|
|
|
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;
|
2019-05-13 11:36:21 +00:00
|
|
|
changes = parseInt( $( '#variable_product_options' ).find( '.woocommerce_variations' )
|
|
|
|
.attr( 'data-total' ), 10 ) * -1;
|
2015-07-07 01:50:35 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-14 17:08:59 +00:00
|
|
|
break;
|
2015-07-07 01:50:35 +00:00
|
|
|
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 );
|
|
|
|
}
|
2017-08-31 11:01:25 +00:00
|
|
|
} else {
|
|
|
|
return;
|
2015-07-07 20:35:21 +00:00
|
|
|
}
|
2015-07-14 17:08:59 +00:00
|
|
|
break;
|
2015-07-07 01:50:35 +00:00
|
|
|
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;
|
2017-08-31 11:01:25 +00:00
|
|
|
} else {
|
|
|
|
return;
|
2015-07-07 01:50:35 +00:00
|
|
|
}
|
2015-07-14 17:08:59 +00:00
|
|
|
break;
|
2015-07-07 01:50:35 +00:00
|
|
|
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;
|
|
|
|
}
|
2017-08-31 11:01:25 +00:00
|
|
|
|
|
|
|
if ( false === data.date_to && false === data.date_from ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-14 17:08:59 +00:00
|
|
|
break;
|
2015-07-07 01:50:35 +00:00
|
|
|
default :
|
2015-07-10 23:04:16 +00:00
|
|
|
$( 'select.variation_actions' ).trigger( do_variation_action );
|
2015-07-21 22:36:30 +00:00
|
|
|
data = $( 'select.variation_actions' ).triggerHandler( do_variation_action + '_ajax_data', data );
|
2015-07-14 17:08:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'delete_all' === do_variation_action && data.allowed ) {
|
2015-09-07 17:51:10 +00:00
|
|
|
$( '#variable_product_options' ).find( '.variation-needs-update' ).removeClass( 'variation-needs-update' );
|
2015-07-14 17:08:59 +00:00
|
|
|
} else {
|
|
|
|
wc_meta_boxes_product_variations_ajax.check_for_changes();
|
2015-07-07 01:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wc_meta_boxes_product_variations_ajax.block();
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: woocommerce_admin_meta_boxes_variations.ajax_url,
|
|
|
|
data: {
|
2015-11-17 15:25:48 +00:00
|
|
|
action: 'woocommerce_bulk_edit_variations',
|
|
|
|
security: woocommerce_admin_meta_boxes_variations.bulk_edit_variations_nonce,
|
|
|
|
product_id: woocommerce_admin_meta_boxes_variations.post_id,
|
|
|
|
product_type: $( '#product-type' ).val(),
|
|
|
|
bulk_action: do_variation_action,
|
|
|
|
data: data
|
2015-07-07 01:50:35 +00:00
|
|
|
},
|
|
|
|
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 )
|
2015-07-10 21:22:05 +00:00
|
|
|
.on( 'woocommerce_variations_added', this.update_single_quantity )
|
2015-07-07 23:22:13 +00:00
|
|
|
.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
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2015-07-10 21:22:05 +00:00
|
|
|
* Set variations count
|
2015-07-06 04:09:36 +00:00
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @param {Int} qty
|
2015-07-10 21:22:05 +00:00
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @return {Int}
|
2015-07-06 04:09:36 +00:00
|
|
|
*/
|
2015-07-10 21:22:05 +00:00
|
|
|
update_variations_count: function( qty ) {
|
2015-09-07 17:51:10 +00:00
|
|
|
var wrapper = $( '#variable_product_options' ).find( '.woocommerce_variations' ),
|
2015-07-10 21:57:38 +00:00
|
|
|
total = parseInt( wrapper.attr( 'data-total' ), 10 ) + qty,
|
2015-07-10 21:22:05 +00:00
|
|
|
displaying_num = $( '.variations-pagenav .displaying-num' );
|
2015-07-06 04:09:36 +00:00
|
|
|
|
|
|
|
// Set the new total of variations
|
2015-07-10 21:57:38 +00:00
|
|
|
wrapper.attr( 'data-total', total );
|
2015-07-06 04:09:36 +00:00
|
|
|
|
2015-07-10 21:57:38 +00:00
|
|
|
if ( 1 === total ) {
|
|
|
|
displaying_num.text( woocommerce_admin_meta_boxes_variations.i18n_variation_count_single.replace( '%qty%', total ) );
|
2015-07-06 04:09:36 +00:00
|
|
|
} else {
|
2015-07-10 21:57:38 +00:00
|
|
|
displaying_num.text( woocommerce_admin_meta_boxes_variations.i18n_variation_count_plural.replace( '%qty%', total ) );
|
2015-07-06 04:09:36 +00:00
|
|
|
}
|
|
|
|
|
2015-07-10 21:57:38 +00:00
|
|
|
return total;
|
2015-07-10 21:22:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update variations quantity when add a new variation
|
|
|
|
*
|
|
|
|
* @param {Object} event
|
2015-07-15 15:26:38 +00:00
|
|
|
* @param {Int} qty
|
2015-07-10 21:22:05 +00:00
|
|
|
*/
|
|
|
|
update_single_quantity: function( event, qty ) {
|
|
|
|
if ( 1 === qty ) {
|
|
|
|
var page_nav = $( '.variations-pagenav' );
|
|
|
|
|
|
|
|
wc_meta_boxes_product_variations_pagenav.update_variations_count( qty );
|
|
|
|
|
|
|
|
if ( page_nav.is( ':hidden' ) ) {
|
2015-07-10 23:04:16 +00:00
|
|
|
$( 'option, optgroup', '.variation_actions' ).show();
|
|
|
|
$( '.variation_actions' ).val( 'add_variation' );
|
2015-09-07 17:51:10 +00:00
|
|
|
$( '#variable_product_options' ).find( '.toolbar' ).show();
|
2015-07-10 21:22:05 +00:00
|
|
|
page_nav.show();
|
|
|
|
$( '.pagination-links', page_nav ).hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the pagenav fields
|
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @param {Int} qty
|
2015-07-10 21:22:05 +00:00
|
|
|
*/
|
|
|
|
set_paginav: function( qty ) {
|
2015-09-07 17:51:10 +00:00
|
|
|
var wrapper = $( '#variable_product_options' ).find( '.woocommerce_variations' ),
|
2015-07-10 21:22:05 +00:00
|
|
|
new_qty = wc_meta_boxes_product_variations_pagenav.update_variations_count( qty ),
|
2015-09-07 17:51:10 +00:00
|
|
|
toolbar = $( '#variable_product_options' ).find( '.toolbar' ),
|
2015-07-10 23:04:16 +00:00
|
|
|
variation_action = $( '.variation_actions' ),
|
2015-07-10 21:22:05 +00:00
|
|
|
page_nav = $( '.variations-pagenav' ),
|
|
|
|
displaying_links = $( '.pagination-links', page_nav ),
|
|
|
|
total_pages = Math.ceil( new_qty / woocommerce_admin_meta_boxes_variations.variations_per_page ),
|
|
|
|
options = '';
|
|
|
|
|
2015-07-06 04:09:36 +00:00
|
|
|
// 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 );
|
|
|
|
|
2015-07-08 19:20:30 +00:00
|
|
|
// Show/hide pagenav
|
2015-07-07 23:22:13 +00:00
|
|
|
if ( 0 === new_qty ) {
|
2015-07-10 03:49:52 +00:00
|
|
|
toolbar.not( '.toolbar-top, .toolbar-buttons' ).hide();
|
|
|
|
page_nav.hide();
|
2015-07-10 23:04:16 +00:00
|
|
|
$( 'option, optgroup', variation_action ).hide();
|
|
|
|
$( '.variation_actions' ).val( 'add_variation' );
|
2015-09-03 14:38:42 +00:00
|
|
|
$( 'option[data-global="true"]', variation_action ).show();
|
2015-07-10 03:49:52 +00:00
|
|
|
|
2015-07-07 23:22:13 +00:00
|
|
|
} else {
|
2015-07-10 03:49:52 +00:00
|
|
|
toolbar.show();
|
|
|
|
page_nav.show();
|
2015-07-10 23:04:16 +00:00
|
|
|
$( 'option, optgroup', variation_action ).show();
|
|
|
|
$( '.variation_actions' ).val( 'add_variation' );
|
2015-07-08 19:20:30 +00:00
|
|
|
|
|
|
|
// Show/hide links
|
|
|
|
if ( 1 === total_pages ) {
|
|
|
|
displaying_links.hide();
|
|
|
|
} else {
|
|
|
|
displaying_links.show();
|
|
|
|
}
|
2015-07-07 23:22:13 +00:00
|
|
|
}
|
2015-07-06 01:00:38 +00:00
|
|
|
},
|
|
|
|
|
2015-07-06 01:31:56 +00:00
|
|
|
/**
|
2015-07-08 19:10:45 +00:00
|
|
|
* Check button if enabled and if don't have changes
|
2015-07-06 01:31:56 +00:00
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @return {Bool}
|
2015-07-06 01:31:56 +00:00
|
|
|
*/
|
|
|
|
check_is_enabled: function( current ) {
|
2015-07-08 20:45:19 +00:00
|
|
|
return ! $( current ).hasClass( 'disabled' );
|
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-08 00:43:34 +00:00
|
|
|
$( '.variations-pagenav .page-selector' ).val( page ).first().change();
|
2015-07-07 23:22:13 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigate on variations pages
|
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @param {Int} page
|
|
|
|
* @param {Int} qty
|
2015-07-07 23:22:13 +00:00
|
|
|
*/
|
|
|
|
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-09-07 17:51:10 +00:00
|
|
|
wrapper = $( '#variable_product_options' ).find( '.woocommerce_variations' );
|
2015-07-06 01:00:38 +00:00
|
|
|
|
2015-07-29 22:25:17 +00:00
|
|
|
$( '.variations-pagenav .page-selector' ).val( selected );
|
|
|
|
|
2015-07-08 20:45:19 +00:00
|
|
|
wc_meta_boxes_product_variations_ajax.check_for_changes();
|
|
|
|
wc_meta_boxes_product_variations_pagenav.change_classes( selected, parseInt( wrapper.attr( 'data-total_pages' ), 10 ) );
|
|
|
|
wc_meta_boxes_product_variations_ajax.load_variations( selected );
|
2015-07-06 01:00:38 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Go to first page
|
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @return {Bool}
|
2015-07-06 01:00:38 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @return {Bool}
|
2015-07-06 01:00:38 +00:00
|
|
|
*/
|
|
|
|
prev_page: function() {
|
2015-07-06 01:31:56 +00:00
|
|
|
if ( wc_meta_boxes_product_variations_pagenav.check_is_enabled( this ) ) {
|
2015-09-07 17:51:10 +00:00
|
|
|
var wrapper = $( '#variable_product_options' ).find( '.woocommerce_variations' ),
|
2015-07-07 23:34:54 +00:00
|
|
|
prev_page = parseInt( wrapper.attr( '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
|
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @return {Bool}
|
2015-07-06 01:00:38 +00:00
|
|
|
*/
|
|
|
|
next_page: function() {
|
2015-07-06 01:31:56 +00:00
|
|
|
if ( wc_meta_boxes_product_variations_pagenav.check_is_enabled( this ) ) {
|
2015-09-07 17:51:10 +00:00
|
|
|
var wrapper = $( '#variable_product_options' ).find( '.woocommerce_variations' ),
|
2015-07-07 23:34:54 +00:00
|
|
|
total_pages = parseInt( wrapper.attr( 'data-total_pages' ), 10 ),
|
|
|
|
next_page = parseInt( wrapper.attr( '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
|
|
|
|
*
|
2015-07-15 15:26:38 +00:00
|
|
|
* @return {Bool}
|
2015-07-06 01:00:38 +00:00
|
|
|
*/
|
|
|
|
last_page: function() {
|
2015-07-06 01:31:56 +00:00
|
|
|
if ( wc_meta_boxes_product_variations_pagenav.check_is_enabled( this ) ) {
|
2015-09-07 17:51:10 +00:00
|
|
|
var last_page = $( '#variable_product_options' ).find( '.woocommerce_variations' ).attr( '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
|
|
|
|
2017-10-31 19:27:57 +00:00
|
|
|
});
|