woocommerce/assets/js/admin/write-panels.js

1365 lines
44 KiB
JavaScript
Raw Normal View History

2011-08-09 15:16:18 +00:00
jQuery( function($){
2012-11-27 16:22:47 +00:00
// Scroll to first checked category - https://github.com/scribu/wp-category-checklist-tree/blob/d1c3c1f449e1144542efa17dde84a9f52ade1739/category-checklist-tree.php
$(function(){
$('[id$="-all"] > ul.categorychecklist').each(function() {
var $list = $(this);
var $firstChecked = $list.find(':checked').first();
if ( !$firstChecked.length )
return;
2012-08-01 12:43:46 +00:00
var pos_first = $list.find('input').position().top;
var pos_checked = $firstChecked.position().top;
$list.closest('.tabs-panel').scrollTop(pos_checked - pos_first + 5);
});
});
2012-11-27 16:22:47 +00:00
2011-09-19 14:57:17 +00:00
// Prevent enter submitting post form
$("#upsell_product_data").bind("keypress", function(e) {
2011-09-28 12:34:41 +00:00
if (e.keyCode == 13) return false;
2011-09-19 14:57:17 +00:00
});
2012-08-01 12:43:46 +00:00
2012-07-22 12:50:45 +00:00
// Type box
$('.type_box').appendTo( '#woocommerce-product-data h3.hndle span' );
2012-08-01 12:43:46 +00:00
2012-07-22 12:50:45 +00:00
$(function(){
2012-08-01 12:43:46 +00:00
// Prevent inputs in meta box headings opening/closing contents
$('#woocommerce-product-data h3.hndle').unbind('click.postboxes');
2012-07-22 12:50:45 +00:00
jQuery('#woocommerce-product-data').on('click', 'h3.hndle', function(event){
2012-08-01 12:43:46 +00:00
2012-07-22 12:50:45 +00:00
// If the user clicks on some form input inside the h3 the box should not be toggled
2012-08-01 12:43:46 +00:00
if ( $(event.target).filter('input, option, label, select').length )
2012-07-22 12:50:45 +00:00
return;
2012-08-01 12:43:46 +00:00
2012-07-22 12:50:45 +00:00
$('#woocommerce-product-data').toggleClass('closed');
});
});
2012-08-01 12:43:46 +00:00
// Order emails
$('#order-emails a.show-order-emails').click(function () {
if ($('#order-emails-select').is(":hidden")) {
$('#order-emails-select').slideDown('fast');
$(this).hide();
}
return false;
});
$('#order-emails a.hide-order-emails').click(function () {
$('input[name="order_email[]"]').each( function() { $(this).attr('checked', false) } );
if ($('#order-emails-select').is(":visible")) {
$('#order-emails-select').slideUp('fast');
$('#order-emails a.show-order-emails').show();
}
return false;
});
2012-07-22 12:50:45 +00:00
// Catalog Visibility
$('#catalog-visibility .edit-catalog-visibility').click(function () {
if ($('#catalog-visibility-select').is(":hidden")) {
$('#catalog-visibility-select').slideDown('fast');
$(this).hide();
}
return false;
});
$('#catalog-visibility .save-post-visibility').click(function () {
$('#catalog-visibility-select').slideUp('fast');
$('#catalog-visibility .edit-catalog-visibility').show();
2012-08-01 12:43:46 +00:00
2012-07-22 12:50:45 +00:00
var value = $('input[name=_visibility]:checked').val();
var label = $('input[name=_visibility]:checked').attr('data-label');
2012-08-01 12:43:46 +00:00
2012-07-22 12:50:45 +00:00
if ( $('input[name=_featured]').is(':checked') ) {
label = label + ', ' + woocommerce_writepanel_params.featured_label
$('input[name=_featured]').attr('checked', 'checked');
}
2012-08-01 12:43:46 +00:00
2012-07-22 12:50:45 +00:00
$('#catalog-visibility-display').text( label );
return false;
});
$('#catalog-visibility .cancel-post-visibility').click(function () {
$('#catalog-visibility-select').slideUp('fast');
$('#catalog-visibility .edit-catalog-visibility').show();
2012-08-01 12:43:46 +00:00
2013-01-04 15:30:31 +00:00
var current_visibility = $('#current_visibility').val();
2012-07-22 12:50:45 +00:00
var current_featured = $('#current_featured').val();
2012-08-01 12:43:46 +00:00
2012-07-22 12:50:45 +00:00
$('input[name=_visibility]').removeAttr('checked');
$('input[name=_visibility][value=' + current_visibility + ']').attr('checked', 'checked');
2012-08-01 12:43:46 +00:00
2012-07-22 12:50:45 +00:00
var label = $('input[name=_visibility]:checked').attr('data-label');
if ( current_featured == 'yes' ) {
label = label + ', ' + woocommerce_writepanel_params.featured_label
$('input[name=_featured]').attr('checked', 'checked');
} else {
$('input[name=_featured]').removeAttr('checked');
}
2012-08-01 12:43:46 +00:00
2012-07-22 12:50:45 +00:00
$('#catalog-visibility-display').text( label );
return false;
});
2012-08-01 12:43:46 +00:00
2011-08-09 15:16:18 +00:00
// TABS
2012-08-15 18:28:08 +00:00
$('ul.wc-tabs').show();
$('div.panel-wrap').each(function(){
2012-12-05 15:05:50 +00:00
$(this).find('div.panel:not(:first)').hide();
2011-08-09 15:16:18 +00:00
});
2012-08-15 18:28:08 +00:00
$('ul.wc-tabs a').click(function(){
var panel_wrap = $(this).closest('div.panel-wrap');
2012-08-15 18:28:08 +00:00
$('ul.wc-tabs li', panel_wrap).removeClass('active');
$(this).parent().addClass('active');
$('div.panel', panel_wrap).hide();
$( $(this).attr('href') ).show();
2011-08-09 15:16:18 +00:00
return false;
});
2012-08-01 12:43:46 +00:00
// Chosen selects
jQuery("select.chosen_select").chosen();
2012-08-01 12:43:46 +00:00
jQuery("select.chosen_select_nostd").chosen({
allow_single_deselect: 'true'
});
2012-08-01 12:43:46 +00:00
// Ajax Chosen Product Selectors
jQuery("select.ajax_chosen_select_products").ajaxChosen({
method: 'GET',
url: woocommerce_writepanel_params.ajax_url,
dataType: 'json',
2012-07-31 17:26:25 +00:00
afterTypeDelay: 100,
data: {
action: 'woocommerce_json_search_products',
security: woocommerce_writepanel_params.search_products_nonce
}
}, function (data) {
2012-08-01 12:43:46 +00:00
var terms = {};
2012-08-01 12:43:46 +00:00
$.each(data, function (i, val) {
terms[i] = val;
});
2012-08-01 12:43:46 +00:00
return terms;
2012-08-01 12:43:46 +00:00
});
jQuery("select.ajax_chosen_select_products_and_variations").ajaxChosen({
method: 'GET',
url: woocommerce_writepanel_params.ajax_url,
dataType: 'json',
2012-03-13 12:18:15 +00:00
afterTypeDelay: 100,
data: {
action: 'woocommerce_json_search_products_and_variations',
security: woocommerce_writepanel_params.search_products_nonce
}
}, function (data) {
2012-08-01 12:43:46 +00:00
2012-07-31 17:26:25 +00:00
var terms = {};
2012-08-01 12:43:46 +00:00
2012-07-31 17:26:25 +00:00
$.each(data, function (i, val) {
terms[i] = val;
});
2012-08-01 12:43:46 +00:00
2012-07-31 17:26:25 +00:00
return terms;
});
2011-08-09 15:16:18 +00:00
// ORDERS
jQuery('#woocommerce-order-actions input, #woocommerce-order-actions a').click(function(){
window.onbeforeunload = '';
});
2012-08-01 12:43:46 +00:00
$('a.edit_address').click(function(event){
2011-11-25 19:31:06 +00:00
$(this).hide();
2012-12-13 00:47:40 +00:00
$(this).closest('.order_data_column').find('div.address').hide();
$(this).closest('.order_data_column').find('div.edit_address').show();
event.preventDefault();
2011-11-25 19:31:06 +00:00
});
2012-11-27 16:22:47 +00:00
2012-09-18 18:07:13 +00:00
$('#order_items_list').on( 'init_row', 'tr.item', function() {
var $row = $(this);
var $qty = $row.find('input.quantity');
var qty = $qty.val();
2012-11-27 16:22:47 +00:00
2012-09-18 18:07:13 +00:00
var line_subtotal = $row.find('input.line_subtotal').val();
var line_total = $row.find('input.line_total').val();
var line_tax = $row.find('input.line_tax').val();
var line_subtotal_tax = $row.find('input.line_subtotal_tax').val();
2012-11-27 16:22:47 +00:00
2012-09-18 18:07:13 +00:00
if ( qty ) {
unit_subtotal = accounting.toFixed( ( line_subtotal / qty ), 2 );
unit_subtotal_tax = accounting.toFixed( ( line_subtotal_tax / qty ), 2 );
unit_total = accounting.toFixed( ( line_total / qty ), 2 );
unit_total_tax = accounting.toFixed( ( line_tax / qty ), 2 );
} else {
unit_subtotal = unit_subtotal_tax = unit_total = unit_total_tax = 0;
}
2012-11-27 16:22:47 +00:00
2012-09-18 18:07:13 +00:00
$qty.attr( 'data-o_qty', qty );
$row.attr( 'data-unit_subtotal', unit_subtotal );
$row.attr( 'data-unit_subtotal_tax', unit_subtotal_tax );
$row.attr( 'data-unit_total', unit_total );
$row.attr( 'data-unit_total_tax', unit_total_tax );
});
2012-11-27 16:22:47 +00:00
2012-09-18 18:07:13 +00:00
// When the page is loaded, store the unit costs
$('#order_items_list tr.item').each( function() {
$(this).trigger('init_row');
} );
2012-11-27 16:22:47 +00:00
2012-09-18 18:07:13 +00:00
// When the qty is changed, increase or decrease costs
$('#order_items_list').on( 'change', 'input.quantity', function() {
var $row = $(this).closest('tr.item');
var qty = $(this).val();
2012-11-27 16:22:47 +00:00
2012-09-18 18:07:13 +00:00
var unit_subtotal = $row.attr('data-unit_subtotal');
var unit_subtotal_tax = $row.attr('data-unit_subtotal_tax');
var unit_total = $row.attr('data-unit_total');
var unit_total_tax = $row.attr('data-unit_total_tax');
var o_qty = $(this).attr('data-o_qty');
2012-11-27 16:22:47 +00:00
2012-09-18 18:07:13 +00:00
var subtotal = accounting.formatNumber( unit_subtotal * qty, 2, '' );
var tax = accounting.formatNumber( unit_subtotal_tax * qty, 2, '' );
var total = accounting.formatNumber( unit_total * qty, 2, '' );
var total_tax = accounting.formatNumber( unit_total_tax * qty, 2, '' );
2012-11-27 16:22:47 +00:00
2012-09-18 18:07:13 +00:00
$row.find('input.line_subtotal').val( subtotal );
$row.find('input.line_total').val( total );
$row.find('input.line_subtotal_tax').val( tax );
$row.find('input.line_tax').val( total_tax );
$(this).trigger('quantity_changed');
2012-09-18 18:07:13 +00:00
});
2012-11-27 16:22:47 +00:00
2012-09-18 18:07:13 +00:00
// When subtotal is changed, update the unit costs
$('#order_items_list').on( 'change', 'input.line_subtotal', function() {
var $row = $(this).closest('tr.item');
var $qty = $row.find('input.quantity');
var qty = $qty.val();
var value = ( qty ) ? accounting.toFixed( ( $(this).val() / qty ), 2 ) : 0;
2012-11-27 16:22:47 +00:00
2012-09-18 18:07:13 +00:00
$row.attr( 'data-unit_subtotal', value );
});
2012-11-27 16:22:47 +00:00
2012-09-18 18:07:13 +00:00
// When total is changed, update the unit costs + discount amount
$('#order_items_list').on( 'change', 'input.line_total', function() {
var $row = $(this).closest('tr.item');
var $qty = $row.find('input.quantity');
var qty = $qty.val();
var value = ( qty ) ? accounting.toFixed( ( $(this).val() / qty ), 2 ) : 0;
2012-11-27 16:22:47 +00:00
2012-09-18 18:07:13 +00:00
$row.attr( 'data-unit_total', value );
});
2012-08-01 12:43:46 +00:00
2012-09-18 18:07:13 +00:00
// When total is changed, update the unit costs + discount amount
$('#order_items_list').on( 'change', 'input.line_subtotal_tax', function() {
var $row = $(this).closest('tr.item');
var $qty = $row.find('input.quantity');
var qty = $qty.val();
var value = ( qty ) ? accounting.toFixed( ( $(this).val() / qty ), 2 ) : 0;
2012-11-27 16:22:47 +00:00
2012-09-18 18:07:13 +00:00
$row.attr( 'data-unit_subtotal_tax', value );
});
2012-11-27 16:22:47 +00:00
2012-09-18 18:07:13 +00:00
// When total is changed, update the unit costs + discount amount
$('#order_items_list').on( 'change', 'input.line_tax', function() {
var $row = $(this).closest('tr.item');
var $qty = $row.find('input.quantity');
var qty = $qty.val();
var value = ( qty ) ? accounting.toFixed( ( $(this).val() / qty ), 2 ) : 0;
2012-11-27 16:22:47 +00:00
2012-09-18 18:07:13 +00:00
$row.attr( 'data-unit_total_tax', value );
});
2012-11-27 16:22:47 +00:00
// Display a total for taxes
2012-09-20 13:43:52 +00:00
$('#woocommerce-order-totals').on( 'change', '#_order_tax, #_order_shipping_tax, #_cart_discount, #_order_discount', function() {
2012-11-27 16:22:47 +00:00
2012-09-20 13:43:52 +00:00
var $this = $(this);
var fields = $this.closest('.totals').find('input');
var total = 0;
2012-11-27 16:22:47 +00:00
2012-09-20 13:43:52 +00:00
fields.each(function(){
2012-11-12 18:53:40 +00:00
if ( $(this).val() )
total = total + parseFloat( $(this).val() );
2012-09-20 13:43:52 +00:00
});
var formatted_total = accounting.formatMoney( total, {
symbol : woocommerce_writepanel_params.currency_format_symbol,
decimal : woocommerce_writepanel_params.currency_format_decimal_sep,
thousand : woocommerce_writepanel_params.currency_format_thousand_sep,
precision : woocommerce_writepanel_params.currency_format_num_decimals,
format : woocommerce_writepanel_params.currency_format
} );
2012-11-27 16:22:47 +00:00
2012-09-20 13:43:52 +00:00
$this.closest('.totals_group').find('span.inline_total').text( formatted_total );
} );
2012-11-27 16:22:47 +00:00
2012-09-20 13:43:52 +00:00
$('span.inline_total').closest('.totals_group').find('input').change();
2012-11-27 16:22:47 +00:00
2012-09-18 18:07:13 +00:00
// Calculate totals
2012-12-28 13:02:12 +00:00
$('button.calc_line_taxes').click(function(){
2012-01-13 21:25:39 +00:00
// Block write panel
$('.woocommerce_order_items_wrapper').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
2012-08-01 12:43:46 +00:00
2012-01-13 21:25:39 +00:00
var answer = confirm(woocommerce_writepanel_params.calc_line_taxes);
2012-08-01 12:43:46 +00:00
2012-01-13 21:25:39 +00:00
if (answer) {
2012-08-01 12:43:46 +00:00
2012-11-12 14:34:10 +00:00
var $items = $('#order_items_list').find('tr.item, tr.fee');
2012-08-01 12:43:46 +00:00
var shipping_country = $('#_shipping_country').val();
var billing_country = $('#_billing_country').val();
if (shipping_country) {
var country = shipping_country;
2012-01-13 21:25:39 +00:00
var state = $('#_shipping_state').val();
var postcode = $('#_shipping_postcode').val();
2012-09-23 16:16:39 +00:00
var city = $('#_shipping_city').val();
} else if(billing_country) {
var country = billing_country;
2012-01-13 21:25:39 +00:00
var state = $('#_billing_state').val();
var postcode = $('#_billing_postcode').val();
2012-09-23 16:16:39 +00:00
var city = $('#_billing_city').val();
} else {
var country = woocommerce_writepanel_params.base_country;
var state = '';
var postcode = '';
var city = '';
2012-01-13 21:25:39 +00:00
}
2012-11-27 16:22:47 +00:00
2012-11-12 18:53:40 +00:00
// Get items and values
var calculate_items = {};
2012-11-27 16:22:47 +00:00
2012-11-12 18:53:40 +00:00
$items.each( function() {
2012-11-27 16:22:47 +00:00
2012-01-13 21:25:39 +00:00
var $row = $(this);
2012-11-27 16:22:47 +00:00
2012-11-12 18:53:40 +00:00
var item_id = $row.find('input.order_item_id').val();
var line_subtotal = $row.find('input.line_subtotal').val();
var line_total = $row.find('input.line_total').val();
var tax_class = $row.find('select.tax_class').val();
2012-11-27 16:22:47 +00:00
2012-11-12 18:53:40 +00:00
calculate_items[ item_id ] = {};
calculate_items[ item_id ].line_subtotal = line_subtotal;
calculate_items[ item_id ].line_total = line_total;
calculate_items[ item_id ].tax_class = tax_class;
} );
2012-11-27 16:22:47 +00:00
2012-11-12 18:53:40 +00:00
var data = {
action: 'woocommerce_calc_line_taxes',
2012-11-13 14:54:34 +00:00
order_id: woocommerce_writepanel_params.post_id,
2012-11-12 18:53:40 +00:00
items: calculate_items,
shipping: accounting.unformat( $('#_order_shipping').val() ),
country: country,
state: state,
postcode: postcode,
city: city,
security: woocommerce_writepanel_params.calc_totals_nonce
};
$.post( woocommerce_writepanel_params.ajax_url, data, function( response ) {
2012-12-13 01:27:34 +00:00
2013-01-22 16:48:28 +00:00
if ( response ) {
2012-11-27 16:22:47 +00:00
2013-01-22 16:48:28 +00:00
$items.each( function() {
var $row = $(this);
var item_id = $row.find('input.order_item_id').val();
2012-11-27 16:22:47 +00:00
2013-01-22 16:48:28 +00:00
$row.find('input.line_tax').val( response['item_taxes'][ item_id ]['line_tax'] ).change();
$row.find('input.line_subtotal_tax').val( response['item_taxes'][ item_id ]['line_subtotal_tax'] ).change();
$('#tax_rows').empty().append( response['tax_row_html'] );
} );
$('#_order_tax').val( response['item_tax'] ).change();
$('#_order_shipping_tax').val( response['shipping_tax'] ).change();
2012-11-27 16:22:47 +00:00
2013-01-22 16:48:28 +00:00
}
2012-11-27 16:22:47 +00:00
2012-11-12 18:53:40 +00:00
$('.woocommerce_order_items_wrapper').unblock();
2012-01-13 21:25:39 +00:00
});
} else {
$('.woocommerce_order_items_wrapper').unblock();
}
return false;
}).hover(function() {
2012-11-13 14:54:34 +00:00
$('#order_items_list input.line_subtotal_tax, #order_items_list input.line_tax, #_order_shipping_tax, #_order_tax, .tax_rows_group').css('background-color', '#e3d2dd');
2012-01-13 21:25:39 +00:00
}, function() {
2012-11-13 14:54:34 +00:00
$('#order_items_list input.line_subtotal_tax, #order_items_list input.line_tax, #_order_shipping_tax, #_order_tax, .tax_rows_group').css('background-color', '');
2012-01-13 21:25:39 +00:00
});
2012-08-01 12:43:46 +00:00
2012-12-28 13:02:12 +00:00
$('button.calc_totals').click( function(){
2012-01-13 21:25:39 +00:00
// Block write panel
$('#woocommerce-order-totals').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
2012-08-01 12:43:46 +00:00
2012-01-13 21:25:39 +00:00
var answer = confirm(woocommerce_writepanel_params.calc_totals);
2012-08-01 12:43:46 +00:00
2012-01-13 21:25:39 +00:00
if (answer) {
2012-08-01 12:43:46 +00:00
2012-01-13 21:25:39 +00:00
// Get row totals
2012-06-10 08:08:11 +00:00
var line_subtotals = 0;
var line_subtotal_taxes = 0;
var line_totals = 0;
var cart_discount = 0;
var cart_tax = 0;
2013-02-25 11:39:15 +00:00
var order_shipping = $('#_order_shipping').val() || '0';
var order_shipping_tax = $('#_order_shipping_tax').val() || '0';
var order_discount = $('#_order_discount').val() || '0';
2012-08-01 12:43:46 +00:00
order_shipping = accounting.unformat( order_shipping.replace(',', '.') );
order_shipping_tax = accounting.unformat( order_shipping_tax.replace(',', '.') );
order_discount = accounting.unformat( order_discount.replace(',', '.') );
2012-01-13 21:25:39 +00:00
$('#order_items_list tr.item').each(function(){
2012-08-01 12:43:46 +00:00
2013-02-25 11:39:15 +00:00
var line_subtotal = $(this).find('input.line_subtotal').val() || '0';
var line_subtotal_tax = $(this).find('input.line_subtotal_tax').val() || '0';
var line_total = $(this).find('input.line_total').val() || '0';
var line_tax = $(this).find('input.line_tax').val() || '0';
2012-08-01 12:43:46 +00:00
line_subtotal = accounting.unformat( line_subtotal.replace(',', '.') );
line_subtotal_tax = accounting.unformat( line_subtotal_tax.replace(',', '.') );
line_total = accounting.unformat( line_total.replace(',', '.') );
line_tax = accounting.unformat( line_tax.replace(',', '.') );
2012-09-18 18:07:13 +00:00
line_subtotals = line_subtotals + line_subtotal;
line_subtotal_taxes = line_subtotal_taxes + line_subtotal_tax;
line_totals = line_totals + line_total;
2012-08-01 12:43:46 +00:00
2012-09-18 18:07:13 +00:00
if ( woocommerce_writepanel_params.round_at_subtotal=='no' ) {
line_tax = accounting.toFixed( line_tax, 2 );
2012-01-13 21:25:39 +00:00
}
2012-08-01 12:43:46 +00:00
2012-11-12 14:34:10 +00:00
cart_tax = cart_tax + parseFloat( line_tax );
});
2012-11-27 16:22:47 +00:00
2012-11-13 14:54:34 +00:00
// Tax
if (woocommerce_writepanel_params.round_at_subtotal=='yes') {
cart_tax = accounting.toFixed( cart_tax, 2 );
}
// Cart discount
var cart_discount = ( line_subtotals + line_subtotal_taxes ) - ( line_totals + cart_tax );
if ( cart_discount < 0 ) cart_discount = 0;
cart_discount = accounting.toFixed( cart_discount, 2 );
2012-11-27 16:22:47 +00:00
2012-11-12 14:34:10 +00:00
$('#order_items_list tr.fee').each(function(){
2013-02-25 11:39:15 +00:00
var line_total = $(this).find('input.line_total').val() || '0';;
var line_tax = $(this).find('input.line_tax').val() || '0';;
2012-11-12 14:34:10 +00:00
line_total = accounting.unformat( line_total.replace(',', '.') );
line_tax = accounting.unformat( line_tax.replace(',', '.') );
2012-11-12 14:34:10 +00:00
line_totals = line_totals + line_total;
if ( woocommerce_writepanel_params.round_at_subtotal=='no' ) {
line_tax = accounting.toFixed( line_tax, 2 );
}
2012-08-01 12:43:46 +00:00
2012-11-12 14:34:10 +00:00
cart_tax = cart_tax + parseFloat( line_tax );
2012-01-13 21:25:39 +00:00
});
2012-08-01 12:43:46 +00:00
2012-01-13 21:25:39 +00:00
// Tax
if (woocommerce_writepanel_params.round_at_subtotal=='yes') {
2013-02-14 11:26:36 +00:00
cart_tax = parseFloat( accounting.toFixed( cart_tax, 2 ) );
2012-01-13 21:25:39 +00:00
}
2012-08-01 12:43:46 +00:00
2012-01-13 21:25:39 +00:00
// Total
var order_total = line_totals + cart_tax + order_shipping + order_shipping_tax - order_discount;
2012-09-18 18:07:13 +00:00
order_total = accounting.toFixed( order_total, 2 );
cart_tax = accounting.toFixed( cart_tax, 2 );
2012-11-27 16:22:47 +00:00
2012-01-13 21:25:39 +00:00
// Set fields
2012-11-12 14:34:10 +00:00
$('#_cart_discount').val( cart_discount ).change();
$('#_order_tax').val( cart_tax ).change();
$('#_order_total').val( order_total ).change();
2012-08-01 12:43:46 +00:00
2012-01-13 21:25:39 +00:00
$('#woocommerce-order-totals').unblock();
2011-08-09 15:16:18 +00:00
2012-01-13 21:25:39 +00:00
} else {
$('#woocommerce-order-totals').unblock();
2011-08-09 15:16:18 +00:00
}
return false;
2012-01-13 21:25:39 +00:00
}).hover(function() {
2012-11-13 14:54:34 +00:00
$('#woocommerce-order-totals .calculated').css('background-color', '#e3d2dd');
2012-01-13 21:25:39 +00:00
}, function() {
$('#woocommerce-order-totals .calculated').css('background-color', '');
2011-08-09 15:16:18 +00:00
});
2012-11-27 16:22:47 +00:00
2012-11-12 13:41:54 +00:00
// Add a line item
2012-10-19 14:05:23 +00:00
$('#woocommerce-order-items button.add_order_item').click(function(){
2012-11-27 16:22:47 +00:00
var add_item_ids = $('select#add_item_id').val();
2012-08-01 12:43:46 +00:00
if ( add_item_ids ) {
2012-08-01 12:43:46 +00:00
count = add_item_ids.length;
2012-08-01 12:43:46 +00:00
$('table.woocommerce_order_items').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
2012-08-01 12:43:46 +00:00
$.each( add_item_ids, function( index, value ) {
2012-08-01 12:43:46 +00:00
var data = {
action: 'woocommerce_add_order_item',
item_to_add: value,
2012-10-19 14:05:23 +00:00
order_id: woocommerce_writepanel_params.post_id,
2012-10-18 17:56:28 +00:00
security: woocommerce_writepanel_params.order_item_nonce
};
2012-08-01 12:43:46 +00:00
2012-10-19 14:05:23 +00:00
$.post( woocommerce_writepanel_params.ajax_url, data, function( response ) {
2012-08-01 12:43:46 +00:00
$('table.woocommerce_order_items tbody#order_items_list').append( response );
if (!--count) {
$('select#add_item_id, #add_item_id_chzn .chzn-choices').css('border-color', '').val('');
jQuery(".tips").tipTip({
'attribute' : 'data-tip',
'fadeIn' : 50,
2012-07-22 12:50:45 +00:00
'fadeOut' : 50,
'delay' : 200
});
$('select#add_item_id').trigger("liszt:updated");
$('table.woocommerce_order_items').unblock();
2012-08-01 12:43:46 +00:00
}
2012-11-27 16:22:47 +00:00
2012-09-18 18:07:13 +00:00
$('#order_items_list tr.new_row').trigger('init_row').removeClass('new_row');
});
2012-08-01 12:43:46 +00:00
2011-08-09 15:16:18 +00:00
});
} else {
$('select#add_item_id, #add_item_id_chzn .chzn-choices').css('border-color', 'red');
2011-08-09 15:16:18 +00:00
}
2012-11-12 13:41:54 +00:00
return false;
2011-08-09 15:16:18 +00:00
});
2012-11-27 16:22:47 +00:00
2012-11-12 13:41:54 +00:00
// Add a fee
$('#woocommerce-order-items button.add_order_fee').click(function(){
2012-11-27 16:22:47 +00:00
2012-11-12 13:41:54 +00:00
$('table.woocommerce_order_items').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
var data = {
action: 'woocommerce_add_order_fee',
order_id: woocommerce_writepanel_params.post_id,
security: woocommerce_writepanel_params.order_item_nonce
};
$.post( woocommerce_writepanel_params.ajax_url, data, function( response ) {
$('table.woocommerce_order_items tbody#order_items_list').append( response );
$('table.woocommerce_order_items').unblock();
});
return false;
});
// Add some meta to a line item
2012-12-28 13:02:12 +00:00
$('#order_items_list').on('click', 'button.add_order_item_meta', function(){
2012-11-27 16:22:47 +00:00
2012-10-18 17:56:28 +00:00
var $button = $(this);
var $item = $button.closest('tr.item');
2012-11-27 16:22:47 +00:00
2012-10-18 17:56:28 +00:00
var data = {
order_item_id: $item.attr( 'data-order_item_id' ),
2012-10-18 17:56:28 +00:00
action: 'woocommerce_add_order_item_meta',
security: woocommerce_writepanel_params.order_item_nonce
};
2012-11-27 16:22:47 +00:00
2012-10-18 17:56:28 +00:00
$('table.woocommerce_order_items').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
2012-11-27 16:22:47 +00:00
2012-10-18 17:56:28 +00:00
$.ajax( {
url: woocommerce_writepanel_params.ajax_url,
data: data,
type: 'POST',
success: function( response ) {
$item.find('tbody.meta_items').append( response );
$('table.woocommerce_order_items').unblock();
}
} );
2012-08-01 12:43:46 +00:00
2011-08-19 20:11:04 +00:00
return false;
2011-08-09 15:16:18 +00:00
});
2012-08-01 12:43:46 +00:00
2012-12-28 13:02:12 +00:00
$('#order_items_list').on('click', 'button.remove_order_item_meta', function(){
2012-10-18 17:56:28 +00:00
var answer = confirm( woocommerce_writepanel_params.remove_item_meta )
if ( answer ) {
var $row = $(this).closest('tr');
2012-11-27 16:22:47 +00:00
2012-10-18 17:56:28 +00:00
var data = {
meta_id: $row.attr( 'data-meta_id' ),
action: 'woocommerce_remove_order_item_meta',
security: woocommerce_writepanel_params.order_item_nonce
};
2012-11-27 16:22:47 +00:00
2012-10-18 17:56:28 +00:00
$('table.woocommerce_order_items').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
2012-11-27 16:22:47 +00:00
2012-10-18 17:56:28 +00:00
$.ajax( {
url: woocommerce_writepanel_params.ajax_url,
data: data,
type: 'POST',
success: function( response ) {
$row.hide();
$('table.woocommerce_order_items').unblock();
}
} );
2011-08-19 20:11:04 +00:00
}
return false;
});
2012-11-27 16:22:47 +00:00
2012-11-10 16:18:20 +00:00
// Bulk actions for line items
2012-11-12 12:30:37 +00:00
$('#woocommerce-order-items').on( 'click', 'input.check-column', function() {
if ( $(this).is(':checked') )
$('#woocommerce-order-items').find('.check-column input').attr('checked', 'checked');
else
$('#woocommerce-order-items').find('.check-column input').removeAttr('checked');
} );
2012-11-27 16:22:47 +00:00
2012-11-10 16:18:20 +00:00
$('#woocommerce-order-items').on( 'click', '.do_bulk_action', function() {
2012-11-27 16:22:47 +00:00
2012-11-10 16:18:20 +00:00
var action = $(this).closest('.bulk_actions').find('select').val();
var selected_rows = $('#woocommerce-order-items').find('.check-column input:checked');
2012-11-12 12:22:35 +00:00
var item_ids = [];
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
$(selected_rows).each( function() {
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
var $item = $(this).closest('tr.item, tr.fee');
item_ids.push( $item.attr( 'data-order_item_id' ) );
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
} );
2012-11-27 16:22:47 +00:00
if ( item_ids.length == 0 ) {
alert( woocommerce_writepanel_params.i18n_select_items );
return;
}
2012-11-10 16:18:20 +00:00
if ( action == 'delete' ) {
2012-11-27 16:22:47 +00:00
2012-11-10 16:18:20 +00:00
var answer = confirm( woocommerce_writepanel_params.remove_item_notice );
2012-11-27 16:22:47 +00:00
2012-11-10 16:18:20 +00:00
if ( answer ) {
2012-11-27 16:22:47 +00:00
2012-11-10 16:18:20 +00:00
$('table.woocommerce_order_items').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
var data = {
order_item_ids: item_ids,
action: 'woocommerce_remove_order_item',
security: woocommerce_writepanel_params.order_item_nonce
};
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
$.ajax( {
url: woocommerce_writepanel_params.ajax_url,
data: data,
type: 'POST',
success: function( response ) {
2012-11-12 14:34:10 +00:00
$(selected_rows).each( function() {
2013-02-26 14:36:18 +00:00
$(this).closest('tr.item, tr.fee').remove();
2012-11-12 14:34:10 +00:00
} );
2012-11-12 12:22:35 +00:00
$('table.woocommerce_order_items').unblock();
}
2012-11-10 16:18:20 +00:00
} );
2012-11-27 16:22:47 +00:00
2012-11-10 16:18:20 +00:00
}
2012-11-27 16:22:47 +00:00
2012-11-10 16:25:50 +00:00
} else if ( action == 'reduce_stock' ) {
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
$('table.woocommerce_order_items').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
var quantities = {};
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
$(selected_rows).each( function() {
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
var $item = $(this).closest('tr.item, tr.fee');
var $qty = $item.find('input.quantity');
2012-11-27 16:22:47 +00:00
quantities[ $item.attr( 'data-order_item_id' ) ] = $qty.val();
2012-11-12 12:22:35 +00:00
} );
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
var data = {
order_id: woocommerce_writepanel_params.post_id,
order_item_ids: item_ids,
order_item_qty: quantities,
action: 'woocommerce_reduce_order_item_stock',
security: woocommerce_writepanel_params.order_item_nonce
};
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
$.ajax( {
url: woocommerce_writepanel_params.ajax_url,
data: data,
type: 'POST',
success: function( response ) {
alert( response );
$('table.woocommerce_order_items').unblock();
}
2012-11-27 16:22:47 +00:00
} );
2012-11-12 12:22:35 +00:00
} else if ( action == 'increase_stock' ) {
$('table.woocommerce_order_items').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
var quantities = {};
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
$(selected_rows).each( function() {
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
var $item = $(this).closest('tr.item, tr.fee');
var $qty = $item.find('input.quantity');
2012-11-27 16:22:47 +00:00
quantities[ $item.attr( 'data-order_item_id' ) ] = $qty.val();
2012-11-12 12:22:35 +00:00
} );
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
var data = {
order_id: woocommerce_writepanel_params.post_id,
order_item_ids: item_ids,
order_item_qty: quantities,
action: 'woocommerce_increase_order_item_stock',
security: woocommerce_writepanel_params.order_item_nonce
};
2012-11-27 16:22:47 +00:00
2012-11-12 12:22:35 +00:00
$.ajax( {
url: woocommerce_writepanel_params.ajax_url,
data: data,
type: 'POST',
success: function( response ) {
alert( response );
$('table.woocommerce_order_items').unblock();
}
2012-11-27 16:22:47 +00:00
} );
2012-11-10 16:18:20 +00:00
}
2012-11-27 16:22:47 +00:00
2012-11-10 16:18:20 +00:00
return false;
} );
2012-11-27 16:22:47 +00:00
2012-08-01 12:43:46 +00:00
2012-12-28 13:02:12 +00:00
$('button.load_customer_billing').click(function(){
2012-08-01 12:43:46 +00:00
2011-11-26 16:15:25 +00:00
var answer = confirm(woocommerce_writepanel_params.load_billing);
if (answer){
2012-08-01 12:43:46 +00:00
2011-11-26 16:15:25 +00:00
// Get user ID to load data for
var user_id = $('#customer_user').val();
2012-08-01 12:43:46 +00:00
2011-11-26 16:15:25 +00:00
if (!user_id) {
alert(woocommerce_writepanel_params.no_customer_selected);
return false;
}
2012-08-01 12:43:46 +00:00
2011-11-26 16:15:25 +00:00
var data = {
user_id: user_id,
type_to_load: 'billing',
action: 'woocommerce_get_customer_details',
security: woocommerce_writepanel_params.get_customer_details_nonce
};
2012-08-01 12:43:46 +00:00
2011-11-26 16:15:25 +00:00
$(this).closest('.edit_address').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
2012-08-01 12:43:46 +00:00
2011-11-26 16:15:25 +00:00
$.ajax({
url: woocommerce_writepanel_params.ajax_url,
data: data,
type: 'POST',
success: function( response ) {
2013-01-22 14:35:10 +00:00
var info = response;
2012-08-01 12:43:46 +00:00
2011-11-26 16:15:25 +00:00
if (info) {
$('input#_billing_first_name').val( info.billing_first_name );
$('input#_billing_last_name').val( info.billing_last_name );
$('input#_billing_company').val( info.billing_company );
$('input#_billing_address_1').val( info.billing_address_1 );
$('input#_billing_address_2').val( info.billing_address_2 );
$('input#_billing_city').val( info.billing_city );
$('input#_billing_postcode').val( info.billing_postcode );
$('#_billing_country').val( info.billing_country );
2011-11-26 16:15:25 +00:00
$('input#_billing_state').val( info.billing_state );
$('input#_billing_email').val( info.billing_email );
$('input#_billing_phone').val( info.billing_phone );
}
2012-08-01 12:43:46 +00:00
2011-11-26 16:15:25 +00:00
$('.edit_address').unblock();
}
});
}
return false;
});
2012-08-01 12:43:46 +00:00
2012-12-28 13:02:12 +00:00
$('button.load_customer_shipping').click(function(){
2012-08-01 12:43:46 +00:00
2011-11-26 16:15:25 +00:00
var answer = confirm(woocommerce_writepanel_params.load_shipping);
if (answer){
2012-08-01 12:43:46 +00:00
2011-11-26 16:15:25 +00:00
// Get user ID to load data for
var user_id = $('#customer_user').val();
2012-08-01 12:43:46 +00:00
2011-11-26 16:15:25 +00:00
if (!user_id) {
alert(woocommerce_writepanel_params.no_customer_selected);
return false;
}
2012-08-01 12:43:46 +00:00
2011-11-26 16:15:25 +00:00
var data = {
user_id: user_id,
type_to_load: 'shipping',
action: 'woocommerce_get_customer_details',
security: woocommerce_writepanel_params.get_customer_details_nonce
};
2012-08-01 12:43:46 +00:00
2011-11-26 16:15:25 +00:00
$(this).closest('.edit_address').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
2012-08-01 12:43:46 +00:00
2011-11-26 16:15:25 +00:00
$.ajax({
url: woocommerce_writepanel_params.ajax_url,
data: data,
type: 'POST',
success: function( response ) {
2013-01-22 14:35:10 +00:00
var info = response;
2013-01-22 16:48:28 +00:00
2011-11-26 16:15:25 +00:00
if (info) {
$('input#_shipping_first_name').val( info.shipping_first_name );
$('input#_shipping_last_name').val( info.shipping_last_name );
$('input#_shipping_company').val( info.shipping_company );
$('input#_shipping_address_1').val( info.shipping_address_1 );
$('input#_shipping_address_2').val( info.shipping_address_2 );
$('input#_shipping_city').val( info.shipping_city );
$('input#_shipping_postcode').val( info.shipping_postcode );
$('#_shipping_country').val( info.shipping_country );
2011-11-26 16:15:25 +00:00
$('input#_shipping_state').val( info.shipping_state );
}
2012-08-01 12:43:46 +00:00
2011-11-26 16:15:25 +00:00
$('.edit_address').unblock();
}
});
}
return false;
});
2012-08-01 12:43:46 +00:00
2012-12-28 13:02:12 +00:00
$('button.billing-same-as-shipping').click(function(){
var answer = confirm(woocommerce_writepanel_params.copy_billing);
2011-08-09 15:16:18 +00:00
if (answer){
2011-11-26 16:15:25 +00:00
$('input#_shipping_first_name').val( $('input#_billing_first_name').val() );
$('input#_shipping_last_name').val( $('input#_billing_last_name').val() );
$('input#_shipping_company').val( $('input#_billing_company').val() );
$('input#_shipping_address_1').val( $('input#_billing_address_1').val() );
$('input#_shipping_address_2').val( $('input#_billing_address_2').val() );
$('input#_shipping_city').val( $('input#_billing_city').val() );
$('input#_shipping_postcode').val( $('input#_billing_postcode').val() );
$('#_shipping_country').val( $('#_billing_country').val() );
2012-08-01 12:43:46 +00:00
$('input#_shipping_state').val( $('input#_billing_state').val() );
2011-08-09 15:16:18 +00:00
}
return false;
});
2012-08-01 12:43:46 +00:00
2012-11-13 14:54:34 +00:00
// Add a tax row
2012-12-28 13:02:12 +00:00
$('a.add_tax_row').click(function(){
2012-08-01 12:43:46 +00:00
2012-11-13 14:54:34 +00:00
var data = {
order_id: woocommerce_writepanel_params.post_id,
action: 'woocommerce_add_line_tax',
security: woocommerce_writepanel_params.calc_totals_nonce
};
$('#tax_rows').closest('.totals_group').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
$.ajax({
url: woocommerce_writepanel_params.ajax_url,
data: data,
type: 'POST',
success: function( response ) {
$('#tax_rows').append( response ).closest('.totals_group').unblock();
}
});
2012-08-01 12:43:46 +00:00
return false;
});
2012-11-27 16:22:47 +00:00
2012-11-13 14:54:34 +00:00
// Delete a tax row
$('#tax_rows').on('click','a.delete_tax_row',function(){
2012-11-27 16:22:47 +00:00
2013-01-25 03:26:16 +00:00
var $tax_row = $(this).closest('.tax_row');
var tax_row_id = $tax_row.attr( 'data-order_item_id' );
2012-11-27 16:22:47 +00:00
2012-11-13 14:54:34 +00:00
var data = {
tax_row_id: tax_row_id,
action: 'woocommerce_remove_line_tax',
security: woocommerce_writepanel_params.calc_totals_nonce
};
$('#tax_rows').closest('.totals_group').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
$.ajax({
url: woocommerce_writepanel_params.ajax_url,
data: data,
type: 'POST',
success: function( response ) {
$tax_row.remove();
$('#tax_rows').closest('.totals_group').unblock();
}
});
2012-11-27 16:22:47 +00:00
2011-12-31 19:03:41 +00:00
return false;
});
2012-08-01 12:43:46 +00:00
2011-08-09 15:16:18 +00:00
// PRODUCT TYPE SPECIFIC OPTIONS
$('select#product-type').change(function(){
2012-08-01 12:43:46 +00:00
2011-08-09 15:16:18 +00:00
// Get value
var select_val = $(this).val();
2012-08-01 12:43:46 +00:00
2012-12-07 10:51:06 +00:00
if (select_val=='variable') {
$('input#_manage_stock').change();
2012-12-07 10:51:06 +00:00
$('input#_downloadable').prop('checked', false);
$('input#_virtual').removeAttr('checked');
}
2012-08-01 12:43:46 +00:00
else if (select_val=='grouped') {
2012-12-07 10:51:06 +00:00
$('input#_downloadable').prop('checked', false);
$('input#_virtual').removeAttr('checked');
}
2012-08-01 12:43:46 +00:00
else if (select_val=='external') {
2012-12-07 10:51:06 +00:00
$('input#_downloadable').prop('checked', false);
$('input#_virtual').removeAttr('checked');
}
2012-08-01 12:43:46 +00:00
2012-12-07 10:51:06 +00:00
show_and_hide_panels();
2012-08-15 18:28:08 +00:00
$('ul.wc-tabs li:visible').eq(0).find('a').click();
2012-08-01 12:43:46 +00:00
2011-08-10 17:11:11 +00:00
$('body').trigger('woocommerce-product-type-change', select_val, $(this) );
2011-08-09 15:16:18 +00:00
}).change();
2012-08-01 12:43:46 +00:00
2012-12-07 10:51:06 +00:00
$('input#_downloadable, input#_virtual').change(function(){
show_and_hide_panels();
});
function show_and_hide_panels() {
var product_type = $('select#product-type').val();
var is_virtual = $('input#_virtual:checked').size();
var is_downloadable = $('input#_downloadable:checked').size();
2012-08-01 12:43:46 +00:00
2012-12-07 10:51:06 +00:00
// Hide/Show all with rules
var hide_classes = '.hide_if_downloadable, .hide_if_virtual';
var show_classes = '.show_if_downloadable, .show_if_virtual, .show_if_external';
$.each( woocommerce_writepanel_params.product_types, function( index, value ) {
hide_classes = hide_classes + ', .hide_if_' + value;
show_classes = show_classes + ', .show_if_' + value;
} );
$( hide_classes ).show();
$( show_classes ).hide();
2012-08-01 12:43:46 +00:00
2012-12-07 10:51:06 +00:00
// Shows rules
if ( is_downloadable ) {
$('.show_if_downloadable').show();
}
2012-12-07 10:51:06 +00:00
if ( is_virtual ) {
$('.show_if_virtual').show();
}
2012-08-01 12:43:46 +00:00
$('.show_if_' + product_type).show();
2012-08-01 12:43:46 +00:00
2012-12-07 10:51:06 +00:00
// Hide rules
if ( is_downloadable ) {
$('.hide_if_downloadable').hide();
}
if ( is_virtual ) {
2011-12-24 15:32:18 +00:00
$('.hide_if_virtual').hide();
}
2012-08-01 12:43:46 +00:00
$('.hide_if_' + product_type).hide();
$('input#_manage_stock').change();
2012-12-07 10:51:06 +00:00
}
2012-08-01 12:43:46 +00:00
2011-12-24 15:32:18 +00:00
// Sale price schedule
2012-10-08 11:51:00 +00:00
$('.sale_price_dates_fields').each(function() {
2012-11-27 16:22:47 +00:00
2012-10-08 11:51:00 +00:00
var $these_sale_dates = $(this);
var sale_schedule_set = false;
var $wrap = $these_sale_dates.closest( 'div, table' );
2012-11-27 16:22:47 +00:00
2012-10-08 11:51:00 +00:00
$these_sale_dates.find('input').each(function(){
2012-11-27 16:22:47 +00:00
if ( $(this).val() != '' )
2012-10-08 11:51:00 +00:00
sale_schedule_set = true;
});
2012-11-27 16:22:47 +00:00
2012-10-08 11:51:00 +00:00
if ( sale_schedule_set ) {
2012-11-27 16:22:47 +00:00
2012-10-08 11:51:00 +00:00
$wrap.find('.sale_schedule').hide();
$wrap.find('.sale_price_dates_fields').show();
2012-11-27 16:22:47 +00:00
2012-10-08 11:51:00 +00:00
} else {
2012-11-27 16:22:47 +00:00
2012-10-08 11:51:00 +00:00
$wrap.find('.sale_schedule').show();
$wrap.find('.sale_price_dates_fields').hide();
2012-11-27 16:22:47 +00:00
2012-10-08 11:51:00 +00:00
}
2012-11-27 16:22:47 +00:00
2012-10-08 11:51:00 +00:00
});
2012-11-27 16:22:47 +00:00
2012-10-08 11:51:00 +00:00
$('#woocommerce-product-data').on( 'click', '.sale_schedule', function() {
var $wrap = $(this).closest( 'div, table' );
2012-11-27 16:22:47 +00:00
2011-12-24 15:32:18 +00:00
$(this).hide();
2012-10-08 11:51:00 +00:00
$wrap.find('.cancel_sale_schedule').show();
$wrap.find('.sale_price_dates_fields').show();
2012-11-27 16:22:47 +00:00
2011-12-24 15:32:18 +00:00
return false;
});
2012-10-08 11:51:00 +00:00
$('#woocommerce-product-data').on( 'click', '.cancel_sale_schedule', function() {
var $wrap = $(this).closest( 'div, table' );
2012-11-27 16:22:47 +00:00
2012-10-08 11:51:00 +00:00
$(this).hide();
$wrap.find('.sale_schedule').show();
$wrap.find('.sale_price_dates_fields').hide();
$wrap.find('.sale_price_dates_fields').find('input').val('');
2012-11-27 16:22:47 +00:00
2011-12-24 15:32:18 +00:00
return false;
});
2012-11-27 16:22:47 +00:00
2011-08-09 15:16:18 +00:00
// STOCK OPTIONS
$('input#_manage_stock').change(function(){
if ($(this).is(':checked')) $('div.stock_fields').show();
else $('div.stock_fields').hide();
2011-08-09 15:16:18 +00:00
}).change();
2012-08-01 12:43:46 +00:00
2011-08-09 15:16:18 +00:00
// DATE PICKER FIELDS
2012-10-08 11:51:00 +00:00
var dates = $( ".sale_price_dates_fields input" ).datepicker({
defaultDate: "",
dateFormat: "yy-mm-dd",
numberOfMonths: 1,
showButtonPanel: true,
showOn: "button",
buttonImage: woocommerce_writepanel_params.calendar_image,
buttonImageOnly: true,
onSelect: function( selectedDate ) {
2012-11-13 23:58:32 +00:00
var option = $(this).is('#_sale_price_dates_from, .sale_price_dates_from') ? "minDate" : "maxDate";
var instance = $( this ).data( "datepicker" ),
date = $.datepicker.parseDate(
instance.settings.dateFormat ||
$.datepicker._defaults.dateFormat,
selectedDate, instance.settings );
dates.not( this ).datepicker( "option", option, date );
2011-08-09 15:16:18 +00:00
}
});
2012-08-01 12:43:46 +00:00
$( ".date-picker" ).datepicker({
2011-09-20 15:05:07 +00:00
dateFormat: "yy-mm-dd",
numberOfMonths: 1,
showButtonPanel: true,
showOn: "button",
buttonImage: woocommerce_writepanel_params.calendar_image,
buttonImageOnly: true
});
2012-08-01 12:43:46 +00:00
2012-03-26 17:30:00 +00:00
$( ".date-picker-field" ).datepicker({
dateFormat: "yy-mm-dd",
numberOfMonths: 1,
showButtonPanel: true,
});
2012-08-01 12:43:46 +00:00
2012-02-25 11:43:25 +00:00
// META BOXES
2012-08-01 12:43:46 +00:00
2012-02-13 19:21:52 +00:00
// Open/close
jQuery('.wc-metaboxes-wrapper').on('click', '.wc-metabox h3', function(event){
// If the user clicks on some form input inside the h3, like a select list (for variations), the box should not be toggled
if ($(event.target).filter(':input, option').length) return;
jQuery(this).next('.wc-metabox-content').toggle();
})
.on('click', '.expand_all', function(event){
jQuery(this).closest('.wc-metaboxes-wrapper').find('.wc-metabox > table').show();
return false;
})
.on('click', '.close_all', function(event){
jQuery(this).closest('.wc-metaboxes-wrapper').find('.wc-metabox > table').hide();
return false;
2012-02-13 19:21:52 +00:00
});
2012-08-01 12:43:46 +00:00
2012-02-25 11:43:25 +00:00
jQuery('.wc-metabox.closed').each(function(){
jQuery(this).find('.wc-metabox-content').hide();
2012-02-13 19:21:52 +00:00
});
2012-08-01 12:43:46 +00:00
2012-02-25 11:43:25 +00:00
// ATTRIBUTE TABLES
2011-09-11 17:35:22 +00:00
// Multiselect attributes
2012-08-01 12:43:46 +00:00
$(".woocommerce_attributes select.multiselect").chosen();
2011-08-09 15:16:18 +00:00
// Initial order
2012-02-13 19:21:52 +00:00
var woocommerce_attribute_items = $('.woocommerce_attributes').find('.woocommerce_attribute').get();
2012-08-01 12:43:46 +00:00
2012-02-13 19:21:52 +00:00
woocommerce_attribute_items.sort(function(a, b) {
var compA = parseInt($(a).attr('rel'));
var compB = parseInt($(b).attr('rel'));
2011-08-09 15:16:18 +00:00
return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
})
2012-02-13 19:21:52 +00:00
$(woocommerce_attribute_items).each( function(idx, itm) { $('.woocommerce_attributes').append(itm); } );
2012-08-01 12:43:46 +00:00
2012-03-13 13:26:20 +00:00
function attribute_row_indexes() {
2012-08-01 12:43:46 +00:00
$('.woocommerce_attributes .woocommerce_attribute').each(function(index, el){
$('.attribute_position', el).val( parseInt( $(el).index('.woocommerce_attributes .woocommerce_attribute') ) );
2012-02-13 19:21:52 +00:00
});
2011-08-09 15:16:18 +00:00
};
2012-08-01 12:43:46 +00:00
2011-08-09 15:16:18 +00:00
// Add rows
2012-02-13 19:21:52 +00:00
$('button.add_attribute').on('click', function(){
2012-08-01 12:43:46 +00:00
2012-02-13 19:21:52 +00:00
var size = $('.woocommerce_attributes .woocommerce_attribute').size();
2012-08-01 12:43:46 +00:00
var attribute_type = $('select.attribute_taxonomy').val();
2012-08-01 12:43:46 +00:00
2011-08-09 15:16:18 +00:00
if (!attribute_type) {
2012-08-01 12:43:46 +00:00
var product_type = $('select#product-type').val();
if (product_type!='variable') enable_variation = 'style="display:none;"'; else enable_variation = '';
2012-08-01 12:43:46 +00:00
2011-08-09 15:16:18 +00:00
// Add custom attribute row
2012-03-04 12:42:24 +00:00
$('.woocommerce_attributes').append('<div class="woocommerce_attribute wc-metabox">\
2012-02-13 19:21:52 +00:00
<h3>\
<button type="button" class="remove_row button">' + woocommerce_writepanel_params.remove_label + '</button>\
<div class="handlediv" title="' + woocommerce_writepanel_params.click_to_toggle + '"></div>\
<strong class="attribute_name"></strong>\
</h3>\
<table cellpadding="0" cellspacing="0" class="woocommerce_attribute_data">\
<tbody>\
<tr>\
<td class="attribute_name">\
<label>' + woocommerce_writepanel_params.name_label + ':</label>\
<input type="text" class="attribute_name" name="attribute_names[' + size + ']" />\
<input type="hidden" name="attribute_is_taxonomy[' + size + ']" value="0" />\
<input type="hidden" name="attribute_position[' + size + ']" class="attribute_position" value="' + size + '" />\
</td>\
<td rowspan="3">\
<label>' + woocommerce_writepanel_params.values_label + ':</label>\
<textarea name="attribute_values[' + size + ']" cols="5" rows="5" placeholder="' + woocommerce_writepanel_params.text_attribute_tip + '"></textarea>\
2012-02-13 19:21:52 +00:00
</td>\
</tr>\
<tr>\
<td>\
<label><input type="checkbox" class="checkbox" ' + ( woocommerce_writepanel_params.default_attribute_visibility ? 'checked="checked"' : '' ) + ' name="attribute_visibility[' + size + ']" value="1" /> ' + woocommerce_writepanel_params.visible_label + '</label>\
2012-02-13 19:21:52 +00:00
</td>\
</tr>\
<tr>\
<td>\
<div class="enable_variation show_if_variable" ' + enable_variation + '>\
<label><input type="checkbox" class="checkbox" ' + ( woocommerce_writepanel_params.default_attribute_variation ? 'checked="checked"' : '' ) + ' name="attribute_variation[' + size + ']" value="1" /> ' + woocommerce_writepanel_params.used_for_variations_label + '</label>\
2012-02-13 19:21:52 +00:00
</div>\
</td>\
</tr>\
</tbody>\
</table>\
</div>');
2012-08-01 12:43:46 +00:00
2011-08-09 15:16:18 +00:00
} else {
2012-08-01 12:43:46 +00:00
2011-08-09 15:16:18 +00:00
// Reveal taxonomy row
2012-02-13 19:21:52 +00:00
var thisrow = $('.woocommerce_attributes .woocommerce_attribute.' + attribute_type);
$('.woocommerce_attributes').append( $(thisrow) );
$(thisrow).show().find('.woocommerce_attribute_data').show();
2012-03-13 13:26:20 +00:00
attribute_row_indexes();
2012-08-01 12:43:46 +00:00
2011-08-09 15:16:18 +00:00
}
2012-08-01 12:43:46 +00:00
2012-02-09 18:10:38 +00:00
$('select.attribute_taxonomy').val('');
2011-08-09 15:16:18 +00:00
});
2012-08-01 12:43:46 +00:00
2012-02-13 19:21:52 +00:00
$('.woocommerce_attributes').on('blur', 'input.attribute_name', function(){
$(this).closest('.woocommerce_attribute').find('strong.attribute_name').text( $(this).val() );
});
2012-08-01 12:43:46 +00:00
2012-02-13 19:21:52 +00:00
$('.woocommerce_attributes').on('click', 'button.select_all_attributes', function(){
$(this).closest('td').find('select option').attr("selected","selected");
$(this).closest('td').find('select').trigger("liszt:updated");
return false;
});
2012-08-01 12:43:46 +00:00
2012-02-13 19:21:52 +00:00
$('.woocommerce_attributes').on('click', 'button.select_no_attributes', function(){
$(this).closest('td').find('select option').removeAttr("selected");
$(this).closest('td').find('select').trigger("liszt:updated");
2011-08-09 15:16:18 +00:00
return false;
});
2012-08-01 12:43:46 +00:00
$('.woocommerce_attributes').on('click', 'button.remove_row', function() {
var answer = confirm(woocommerce_writepanel_params.remove_attribute);
2011-08-09 15:16:18 +00:00
if (answer){
2012-02-13 19:21:52 +00:00
var $parent = $(this).parent().parent();
2012-08-01 12:43:46 +00:00
2012-02-13 19:21:52 +00:00
if ($parent.is('.taxonomy')) {
$parent.find('select, input[type=text]').val('');
$parent.hide();
} else {
$parent.find('select, input[type=text]').val('');
$parent.hide();
2012-03-13 13:26:20 +00:00
attribute_row_indexes();
2012-02-13 19:21:52 +00:00
}
2011-08-09 15:16:18 +00:00
}
return false;
});
2012-08-01 12:43:46 +00:00
2011-09-19 14:57:17 +00:00
// Attribute ordering
2012-02-13 19:21:52 +00:00
$('.woocommerce_attributes').sortable({
items:'.woocommerce_attribute',
2011-09-19 14:57:17 +00:00
cursor:'move',
axis:'y',
2012-02-13 19:21:52 +00:00
handle: 'h3',
2011-09-19 14:57:17 +00:00
scrollSensitivity:40,
2012-02-13 19:21:52 +00:00
forcePlaceholderSize: true,
helper: 'clone',
opacity: 0.65,
2012-02-25 11:43:25 +00:00
placeholder: 'wc-metabox-sortable-placeholder',
2011-09-19 14:57:17 +00:00
start:function(event,ui){
ui.item.css('background-color','#f6f6f6');
},
stop:function(event,ui){
ui.item.removeAttr('style');
2012-03-13 13:26:20 +00:00
attribute_row_indexes();
2011-09-19 14:57:17 +00:00
}
2011-08-09 15:16:18 +00:00
});
2012-08-01 12:43:46 +00:00
// Add a new attribute (via ajax)
$('.woocommerce_attributes').on('click', 'button.add_new_attribute', function() {
2012-08-01 12:43:46 +00:00
$('.woocommerce_attributes').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
2012-08-01 12:43:46 +00:00
var attribute = $(this).attr('data-attribute');
var $wrapper = $(this).closest('.woocommerce_attribute_data');
var new_attribute_name = prompt( woocommerce_writepanel_params.new_attribute_prompt );
2012-08-01 12:43:46 +00:00
if ( new_attribute_name ) {
2012-08-01 12:43:46 +00:00
var data = {
action: 'woocommerce_add_new_attribute',
taxonomy: attribute,
term: new_attribute_name,
security: woocommerce_writepanel_params.add_attribute_nonce
};
2012-08-01 12:43:46 +00:00
$.post( woocommerce_writepanel_params.ajax_url, data, function( response ) {
2012-08-01 12:43:46 +00:00
2013-01-22 16:31:54 +00:00
if ( response.error ) {
// Error
2013-01-22 16:31:54 +00:00
alert( response.error );
} else if ( response.slug ) {
// Success
2013-01-22 16:31:54 +00:00
$wrapper.find('select.attribute_values').append('<option value="' + response.slug + '" selected="selected">' + response.name + '</option>');
$wrapper.find('select.attribute_values').trigger("liszt:updated");
}
2012-08-01 12:43:46 +00:00
$('.woocommerce_attributes').unblock();
2012-08-01 12:43:46 +00:00
});
2012-08-01 12:43:46 +00:00
2012-05-26 16:42:26 +00:00
} else {
$('.woocommerce_attributes').unblock();
}
2012-08-01 12:43:46 +00:00
return false;
2012-08-01 12:43:46 +00:00
});
2012-11-27 16:22:47 +00:00
// Save attributes and update variations
$('.save_attributes').on('click', function(){
2012-11-27 16:22:47 +00:00
$('.woocommerce_attributes').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
2012-11-27 16:22:47 +00:00
var data = {
post_id: woocommerce_writepanel_params.post_id,
data: $('.woocommerce_attributes').find('input, select, textarea').serialize(),
action: 'woocommerce_save_attributes',
security: woocommerce_writepanel_params.save_attributes_nonce
};
$.post( woocommerce_writepanel_params.ajax_url, data, function( response ) {
var this_page = window.location.toString();
2012-11-27 16:22:47 +00:00
this_page = this_page.replace( 'post-new.php?', 'post.php?post=' + woocommerce_writepanel_params.post_id + '&action=edit&' );
2012-11-27 16:22:47 +00:00
// Load variations panel
$('#variable_product_options').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
$('#variable_product_options').load( this_page + ' #variable_product_options_inner', function() {
$('#variable_product_options').unblock();
} );
$('.woocommerce_attributes').unblock();
});
2012-11-27 16:22:47 +00:00
});
2012-08-01 12:43:46 +00:00
// Uploading files
2012-12-18 12:04:33 +00:00
var downloadable_file_frame;
2013-04-15 13:55:00 +00:00
var file_path_field;
var file_paths;
2012-12-18 12:04:33 +00:00
2012-12-28 13:02:12 +00:00
jQuery(document).on( 'click', '.upload_file_button', function( event ){
2012-12-18 12:04:33 +00:00
var $el = $(this);
2013-04-15 13:55:00 +00:00
file_path_field = $el.parent().find('.file_paths');
file_paths = file_path_field.val();
2012-12-18 12:04:33 +00:00
event.preventDefault();
// If the media frame already exists, reopen it.
if ( downloadable_file_frame ) {
downloadable_file_frame.open();
return;
}
2012-12-18 21:56:01 +00:00
var downloadable_file_states = [
// Main states.
new wp.media.controller.Library({
library: wp.media.query(),
multiple: true,
title: $el.data('choose'),
priority: 20,
filterable: 'uploaded',
})
];
2012-12-18 12:04:33 +00:00
// Create the media frame.
downloadable_file_frame = wp.media.frames.downloadable_file = wp.media({
// Set the title of the modal.
title: $el.data('choose'),
library: {
type: ''
},
button: {
text: $el.data('update'),
},
multiple: true,
2012-12-18 21:56:01 +00:00
states: downloadable_file_states,
2012-12-18 12:04:33 +00:00
});
// When an image is selected, run a callback.
downloadable_file_frame.on( 'select', function() {
var selection = downloadable_file_frame.state().get('selection');
selection.map( function( attachment ) {
attachment = attachment.toJSON();
if ( attachment.url )
file_paths = file_paths ? file_paths + "\n" + attachment.url : attachment.url
} );
2013-04-15 13:55:00 +00:00
file_path_field.val( file_paths );
2012-12-18 12:04:33 +00:00
});
2012-12-18 21:56:01 +00:00
// Set post to 0 and set our custom type
downloadable_file_frame.on( 'ready', function() {
downloadable_file_frame.uploader.options.uploader.params = {
type: 'downloadable_product'
};
});
2012-12-18 12:04:33 +00:00
// Finally, open the modal.
downloadable_file_frame.open();
});
2011-08-09 15:16:18 +00:00
});