jQuery( function($){ // Prevent enter submitting post form $("#upsell_product_data").bind("keypress", function(e) { if (e.keyCode == 13) return false; }); // Type box $('.type_box').appendTo( '#woocommerce-product-data h3.hndle span' ); $(function(){ // Prevent inputs in meta box headings opening/closing contents $('#woocommerce-product-data h3.hndle').unbind('click.postboxes'); jQuery('#woocommerce-product-data').on('click', 'h3.hndle', function(event){ // If the user clicks on some form input inside the h3 the box should not be toggled if ( $(event.target).filter('input, option, label, select').length ) return; $('#woocommerce-product-data').toggleClass('closed'); }); }); // 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(); var value = $('input[name=_visibility]:checked').val(); var label = $('input[name=_visibility]:checked').attr('data-label'); if ( $('input[name=_featured]').is(':checked') ) { label = label + ', ' + woocommerce_writepanel_params.featured_label $('input[name=_featured]').attr('checked', 'checked'); } $('#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(); var current_visibility = $('#current_visibilty').val(); var current_featured = $('#current_featured').val(); $('input[name=_visibility]').removeAttr('checked'); $('input[name=_visibility][value=' + current_visibility + ']').attr('checked', 'checked'); 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'); } $('#catalog-visibility-display').text( label ); return false; }); // TABS $('ul.wc-tabs').show(); $('div.panel-wrap').each(function(){ $('div.panel:not(div.panel:first)', this).hide(); }); $('ul.wc-tabs a').click(function(){ var panel_wrap = $(this).closest('div.panel-wrap'); $('ul.wc-tabs li', panel_wrap).removeClass('active'); $(this).parent().addClass('active'); $('div.panel', panel_wrap).hide(); $( $(this).attr('href') ).show(); return false; }); // Chosen selects jQuery("select.chosen_select").chosen(); jQuery("select.chosen_select_nostd").chosen({ allow_single_deselect: 'true' }); // Ajax Chosen Product Selectors jQuery("select.ajax_chosen_select_products").ajaxChosen({ method: 'GET', url: woocommerce_writepanel_params.ajax_url, dataType: 'json', afterTypeDelay: 100, data: { action: 'woocommerce_json_search_products', security: woocommerce_writepanel_params.search_products_nonce } }, function (data) { var terms = {}; $.each(data, function (i, val) { terms[i] = val; }); return terms; }); jQuery("select.ajax_chosen_select_products_and_variations").ajaxChosen({ method: 'GET', url: woocommerce_writepanel_params.ajax_url, dataType: 'json', afterTypeDelay: 100, data: { action: 'woocommerce_json_search_products_and_variations', security: woocommerce_writepanel_params.search_products_nonce } }, function (data) { var terms = {}; $.each(data, function (i, val) { terms[i] = val; }); return terms; }); // ORDERS jQuery('#woocommerce-order-actions input, #woocommerce-order-actions a').click(function(){ window.onbeforeunload = ''; }); $('a.edit_address').click(function(event){ $(this).hide(); $(this).closest('.order_data').find('div.address').hide(); $(this).closest('.order_data').find('div.edit_address').show(); event.preventDefault(); }); $('#order_items_list .remove_row').live('click', function(){ var answer = confirm(woocommerce_writepanel_params.remove_item_notice); if (answer){ $(this).closest('tr.item').hide(); $('input', $(this).closest('tr.item')).val(''); } return false; }); $('#order_items_list').on( 'init_row', 'tr.item', function() { var $row = $(this); var $qty = $row.find('input.quantity'); var qty = $qty.val(); 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(); 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; } $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 ); }); // When the page is loaded, store the unit costs $('#order_items_list tr.item').each( function() { $(this).trigger('init_row'); } ); // 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(); 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'); 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, '' ); $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 ); }); // 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; $row.attr( 'data-unit_subtotal', value ); }); // 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; $row.attr( 'data-unit_total', value ); }); // 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; $row.attr( 'data-unit_subtotal_tax', value ); }); // 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; $row.attr( 'data-unit_total_tax', value ); }); // Display a total for taxes $('#woocommerce-order-totals').on( 'change', '#_order_tax, #_order_shipping_tax, #_cart_discount, #_order_discount', function() { var $this = $(this); var fields = $this.closest('.totals').find('input'); var total = 0; fields.each(function(){ total = total + parseFloat( $(this).val() ); }); 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 } ); $this.closest('.totals_group').find('span.inline_total').text( formatted_total ); } ); $('span.inline_total').closest('.totals_group').find('input').change(); // Calculate totals $('button.calc_line_taxes').live('click', function(){ // 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 } }); var answer = confirm(woocommerce_writepanel_params.calc_line_taxes); if (answer) { var $items = $('#order_items_list tr.item'); var country = $('#_shipping_country').val(); if (country) { var state = $('#_shipping_state').val(); var postcode = $('#_shipping_postcode').val(); var city = $('#_shipping_city').val(); } else { country = $('#_billing_country').val(); var state = $('#_billing_state').val(); var postcode = $('#_billing_postcode').val(); var city = $('#_billing_city').val(); } $items.each(function( idx ){ var $row = $(this); var data = { action: 'woocommerce_calc_line_taxes', item_id: $row.find('input.item_id').val(), line_subtotal: $row.find('input.line_subtotal').val(), line_total: $row.find('input.line_total').val(), tax_class: $row.find('select.tax_class').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) { result = jQuery.parseJSON( response ); $row.find('input.line_subtotal_tax').val( result.line_subtotal_tax ).change(); $row.find('input.line_tax').val( result.line_tax ).change(); if (idx == ($items.size() - 1)) { $('.woocommerce_order_items_wrapper').unblock(); } }); }); } else { $('.woocommerce_order_items_wrapper').unblock(); } return false; }).hover(function() { $('#order_items_list input.line_subtotal_tax, #order_items_list input.line_tax').css('background-color', '#d8c8d2'); }, function() { $('#order_items_list input.line_subtotal_tax, #order_items_list input.line_tax').css('background-color', ''); }); $('button.calc_totals').live('click', function(){ // 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 } }); var answer = confirm(woocommerce_writepanel_params.calc_totals); if (answer) { // Get row totals var line_subtotals = 0; var line_subtotal_taxes = 0; var line_totals = 0; var cart_discount = 0; var cart_tax = 0; var order_shipping = accounting.unformat( $('#_order_shipping').val() ); var order_shipping_tax = accounting.unformat( $('#_order_shipping_tax').val() ); var order_discount = accounting.unformat( $('#_order_discount').val() ); if ( ! order_shipping ) order_shipping = 0; if ( ! order_shipping_tax ) order_shipping_tax = 0; if ( ! order_discount ) order_discount = 0; $('#order_items_list tr.item').each(function(){ var line_subtotal = accounting.unformat( $(this).find('input.line_subtotal').val() ); var line_subtotal_tax = accounting.unformat( $(this).find('input.line_subtotal_tax').val() ); var line_total = accounting.unformat( $(this).find('input.line_total').val() ); var line_tax = accounting.unformat( $(this).find('input.line_tax').val() ); if ( ! line_subtotal ) line_subtotal = 0; if ( ! line_subtotal_tax ) line_subtotal_tax = 0; if ( ! line_total ) line_total = 0; if ( ! line_tax ) line_tax = 0; line_subtotals = line_subtotals + line_subtotal; line_subtotal_taxes = line_subtotal_taxes + line_subtotal_tax; line_totals = line_totals + line_total; if ( woocommerce_writepanel_params.round_at_subtotal=='no' ) { line_tax = accounting.toFixed( line_tax, 2 ); } cart_tax = cart_tax + line_tax; }); // 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 ); // Total var order_total = line_totals + cart_tax + order_shipping + order_shipping_tax - order_discount; order_total = accounting.toFixed( order_total, 2 ); cart_tax = accounting.toFixed( cart_tax, 2 ); // Set fields $('#_cart_discount').val( cart_discount ); $('#_order_tax').val( cart_tax ); $('#_order_total').val( order_total ); // Since we currently cannot calc shipping from the backend, ditch the rows. They must be manually calculated. $('#tax_rows').empty(); $('#woocommerce-order-totals').unblock(); } else { $('#woocommerce-order-totals').unblock(); } return false; }).hover(function() { $('#woocommerce-order-totals .calculated').css('background-color', '#d8c8d2'); }, function() { $('#woocommerce-order-totals .calculated').css('background-color', ''); }); $('button.add_shop_order_item').click(function(){ var add_item_ids = $('select#add_item_id').val(); if ( add_item_ids ) { count = add_item_ids.length; $('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 size = $('table.woocommerce_order_items tbody tr.item').size(); $.each( add_item_ids, function( index, value ) { var data = { action: 'woocommerce_add_order_item', item_to_add: value, index: size, security: woocommerce_writepanel_params.add_order_item_nonce }; $.post( woocommerce_writepanel_params.ajax_url, data, function(response) { $('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, 'fadeOut' : 50, 'delay' : 200 }); $('select#add_item_id').trigger("liszt:updated"); $('table.woocommerce_order_items').unblock(); } $('#order_items_list tr.new_row').trigger('init_row').removeClass('new_row'); }); size++; }); } else { $('select#add_item_id, #add_item_id_chzn .chzn-choices').css('border-color', 'red'); } }); $('button.add_meta').live('click', function(){ var index = $(this).closest('tr.item').attr('rel'); $(this).closest('table.meta').find('.meta_items').append('