api-keys.js
5 years ago
api-keys.min.js
2 years ago
backbone-modal.js
2 years ago
backbone-modal.min.js
2 years ago
marketplace-suggestions.js
4 years ago
marketplace-suggestions.min.js
2 years ago
meta-boxes-coupon.js
5 years ago
meta-boxes-coupon.min.js
2 years ago
meta-boxes-order.js
2 years ago
meta-boxes-order.min.js
2 years ago
meta-boxes-product-variation.js
2 years ago
meta-boxes-product-variation.min.js
2 years ago
meta-boxes-product.js
2 years ago
meta-boxes-product.min.js
2 years ago
meta-boxes.js
2 years ago
meta-boxes.min.js
2 years ago
network-orders.js
8 years ago
network-orders.min.js
2 years ago
order-attribution-admin.js
2 years ago
order-attribution-admin.min.js
2 years ago
product-editor.js
3 years ago
product-editor.min.js
2 years ago
product-ordering.js
3 years ago
product-ordering.min.js
2 years ago
quick-edit.js
4 years ago
quick-edit.min.js
2 years ago
reports.js
5 years ago
reports.min.js
2 years ago
settings-views-html-settings-tax.js
3 years ago
settings-views-html-settings-tax.min.js
2 years ago
settings.js
4 years ago
settings.min.js
2 years ago
system-status.js
3 years ago
system-status.min.js
2 years ago
term-ordering.js
3 years ago
term-ordering.min.js
2 years ago
users.js
5 years ago
users.min.js
2 years ago
wc-clipboard.js
5 years ago
wc-clipboard.min.js
5 years ago
wc-enhanced-select.js
2 years ago
wc-enhanced-select.min.js
2 years ago
wc-orders.js
3 years ago
wc-orders.min.js
2 years ago
wc-product-export.js
5 years ago
wc-product-export.min.js
2 years ago
wc-product-import.js
3 years ago
wc-product-import.min.js
2 years ago
wc-setup.js
5 years ago
wc-setup.min.js
2 years ago
wc-shipping-classes.js
2 years ago
wc-shipping-classes.min.js
2 years ago
wc-shipping-zone-methods.js
2 years ago
wc-shipping-zone-methods.min.js
2 years ago
wc-shipping-zones.js
2 years ago
wc-shipping-zones.min.js
2 years ago
wc-status-widget.js
3 years ago
wc-status-widget.min.js
3 years ago
woocommerce_admin.js
2 years ago
woocommerce_admin.min.js
2 years ago
meta-boxes-order.js
1595 lines
| 1 | // eslint-disable-next-line max-len |
| 2 | /*global woocommerce_admin_meta_boxes, woocommerce_admin, accounting, woocommerce_admin_meta_boxes_order, wcSetClipboard, wcClearClipboard */ |
| 3 | jQuery( function ( $ ) { |
| 4 | |
| 5 | // Stand-in wcTracks.recordEvent in case tracks is not available (for any reason). |
| 6 | window.wcTracks = window.wcTracks || {}; |
| 7 | window.wcTracks.recordEvent = window.wcTracks.recordEvent || function() { }; |
| 8 | |
| 9 | /** |
| 10 | * Order Data Panel |
| 11 | */ |
| 12 | var wc_meta_boxes_order = { |
| 13 | states: null, |
| 14 | init: function() { |
| 15 | if ( |
| 16 | ! ( |
| 17 | typeof woocommerce_admin_meta_boxes_order === 'undefined' || |
| 18 | typeof woocommerce_admin_meta_boxes_order.countries === 'undefined' |
| 19 | ) |
| 20 | ) { |
| 21 | /* State/Country select boxes */ |
| 22 | this.states = JSON.parse( woocommerce_admin_meta_boxes_order.countries.replace( /"/g, '"' ) ); |
| 23 | } |
| 24 | |
| 25 | $( '.js_field-country' ).selectWoo().on( 'change', this.change_country ); |
| 26 | $( '.js_field-country' ).trigger( 'change', [ true ] ); |
| 27 | $( document.body ).on( 'change', 'select.js_field-state', this.change_state ); |
| 28 | $( '#woocommerce-order-actions input, #woocommerce-order-actions a' ).on( 'click', function() { |
| 29 | window.onbeforeunload = ''; |
| 30 | }); |
| 31 | $( 'a.edit_address' ).on( 'click', this.edit_address ); |
| 32 | $( 'a.billing-same-as-shipping' ).on( 'click', this.copy_billing_to_shipping ); |
| 33 | $( 'a.load_customer_billing' ).on( 'click', this.load_billing ); |
| 34 | $( 'a.load_customer_shipping' ).on( 'click', this.load_shipping ); |
| 35 | $( '#customer_user' ).on( 'change', this.change_customer_user ); |
| 36 | }, |
| 37 | |
| 38 | change_country: function( e, stickValue ) { |
| 39 | // Check for stickValue before using it |
| 40 | if ( typeof stickValue === 'undefined' ){ |
| 41 | stickValue = false; |
| 42 | } |
| 43 | |
| 44 | // Prevent if we don't have the metabox data |
| 45 | if ( wc_meta_boxes_order.states === null ){ |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | var $this = $( this ), |
| 50 | country = $this.val(), |
| 51 | $state = $this.parents( 'div.edit_address' ).find( ':input.js_field-state' ), |
| 52 | $parent = $state.parent(), |
| 53 | stateValue = $state.val(), |
| 54 | input_name = $state.attr( 'name' ), |
| 55 | input_id = $state.attr( 'id' ), |
| 56 | value = $this.data( 'woocommerce.stickState-' + country ) ? $this.data( 'woocommerce.stickState-' + country ) : stateValue, |
| 57 | placeholder = $state.attr( 'placeholder' ), |
| 58 | $newstate; |
| 59 | |
| 60 | if ( stickValue ){ |
| 61 | $this.data( 'woocommerce.stickState-' + country, value ); |
| 62 | } |
| 63 | |
| 64 | // Remove the previous DOM element |
| 65 | $parent.show().find( '.select2-container' ).remove(); |
| 66 | |
| 67 | if ( ! $.isEmptyObject( wc_meta_boxes_order.states[ country ] ) ) { |
| 68 | var state = wc_meta_boxes_order.states[ country ], |
| 69 | $defaultOption = $( '<option value=""></option>' ) |
| 70 | .text( woocommerce_admin_meta_boxes_order.i18n_select_state_text ); |
| 71 | |
| 72 | $newstate = $( '<select></select>' ) |
| 73 | .prop( 'id', input_id ) |
| 74 | .prop( 'name', input_name ) |
| 75 | .prop( 'placeholder', placeholder ) |
| 76 | .addClass( 'js_field-state select short' ) |
| 77 | .append( $defaultOption ); |
| 78 | |
| 79 | $.each( state, function( index ) { |
| 80 | var $option = $( '<option></option>' ) |
| 81 | .prop( 'value', index ) |
| 82 | .text( state[ index ] ); |
| 83 | if ( index === stateValue ) { |
| 84 | $option.prop( 'selected' ); |
| 85 | } |
| 86 | $newstate.append( $option ); |
| 87 | } ); |
| 88 | |
| 89 | $newstate.val( value ); |
| 90 | |
| 91 | $state.replaceWith( $newstate ); |
| 92 | |
| 93 | $newstate.show().selectWoo().hide().trigger( 'change' ); |
| 94 | } else { |
| 95 | $newstate = $( '<input type="text" />' ) |
| 96 | .prop( 'id', input_id ) |
| 97 | .prop( 'name', input_name ) |
| 98 | .prop( 'placeholder', placeholder ) |
| 99 | .addClass( 'js_field-state' ) |
| 100 | .val( stateValue ); |
| 101 | $state.replaceWith( $newstate ); |
| 102 | } |
| 103 | |
| 104 | // This event has a typo - deprecated in 2.5.0 |
| 105 | $( document.body ).trigger( 'contry-change.woocommerce', [country, $( this ).closest( 'div' )] ); |
| 106 | $( document.body ).trigger( 'country-change.woocommerce', [country, $( this ).closest( 'div' )] ); |
| 107 | }, |
| 108 | |
| 109 | change_state: function() { |
| 110 | // Here we will find if state value on a select has changed and stick it to the country data |
| 111 | var $this = $( this ), |
| 112 | state = $this.val(), |
| 113 | $country = $this.parents( 'div.edit_address' ).find( ':input.js_field-country' ), |
| 114 | country = $country.val(); |
| 115 | |
| 116 | $country.data( 'woocommerce.stickState-' + country, state ); |
| 117 | }, |
| 118 | |
| 119 | init_tiptip: function() { |
| 120 | $( '#tiptip_holder' ).removeAttr( 'style' ); |
| 121 | $( '#tiptip_arrow' ).removeAttr( 'style' ); |
| 122 | $( '.tips' ).tipTip({ |
| 123 | 'attribute': 'data-tip', |
| 124 | 'fadeIn': 50, |
| 125 | 'fadeOut': 50, |
| 126 | 'delay': 200, |
| 127 | 'keepAlive': true |
| 128 | }); |
| 129 | }, |
| 130 | |
| 131 | edit_address: function( e ) { |
| 132 | e.preventDefault(); |
| 133 | |
| 134 | var $this = $( this ), |
| 135 | $wrapper = $this.closest( '.order_data_column' ), |
| 136 | $edit_address = $wrapper.find( 'div.edit_address' ), |
| 137 | $address = $wrapper.find( 'div.address' ), |
| 138 | $country_input = $edit_address.find( '.js_field-country' ), |
| 139 | $state_input = $edit_address.find( '.js_field-state' ), |
| 140 | is_billing = Boolean( $edit_address.find( 'input[name^="_billing_"]' ).length ); |
| 141 | |
| 142 | $address.hide(); |
| 143 | $this.parent().find( 'a' ).toggle(); |
| 144 | |
| 145 | if ( ! $country_input.val() ) { |
| 146 | $country_input.val( woocommerce_admin_meta_boxes_order.default_country ).trigger( 'change' ); |
| 147 | $state_input.val( woocommerce_admin_meta_boxes_order.default_state ).trigger( 'change' ); |
| 148 | } |
| 149 | |
| 150 | $edit_address.show(); |
| 151 | |
| 152 | var event_name = is_billing ? 'order_edit_billing_address_click' : 'order_edit_shipping_address_click'; |
| 153 | window.wcTracks.recordEvent( event_name, { |
| 154 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 155 | status: $( '#order_status' ).val() |
| 156 | } ); |
| 157 | }, |
| 158 | |
| 159 | change_customer_user: function() { |
| 160 | if ( ! $( '#_billing_country' ).val() ) { |
| 161 | $( 'a.edit_address' ).trigger( 'click' ); |
| 162 | wc_meta_boxes_order.load_billing( true ); |
| 163 | wc_meta_boxes_order.load_shipping( true ); |
| 164 | } |
| 165 | }, |
| 166 | |
| 167 | load_billing: function( force ) { |
| 168 | if ( true === force || window.confirm( woocommerce_admin_meta_boxes.load_billing ) ) { |
| 169 | |
| 170 | // Get user ID to load data for |
| 171 | var user_id = $( '#customer_user' ).val(); |
| 172 | |
| 173 | if ( ! user_id ) { |
| 174 | window.alert( woocommerce_admin_meta_boxes.no_customer_selected ); |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | var data = { |
| 179 | user_id : user_id, |
| 180 | action : 'woocommerce_get_customer_details', |
| 181 | security: woocommerce_admin_meta_boxes.get_customer_details_nonce |
| 182 | }; |
| 183 | |
| 184 | $( this ).closest( 'div.edit_address' ).block({ |
| 185 | message: null, |
| 186 | overlayCSS: { |
| 187 | background: '#fff', |
| 188 | opacity: 0.6 |
| 189 | } |
| 190 | }); |
| 191 | |
| 192 | $.ajax({ |
| 193 | url: woocommerce_admin_meta_boxes.ajax_url, |
| 194 | data: data, |
| 195 | type: 'POST', |
| 196 | success: function( response ) { |
| 197 | if ( response && response.billing ) { |
| 198 | $.each( response.billing, function( key, data ) { |
| 199 | $( ':input#_billing_' + key ).val( data ).trigger( 'change' ); |
| 200 | }); |
| 201 | } |
| 202 | $( 'div.edit_address' ).unblock(); |
| 203 | } |
| 204 | }); |
| 205 | } |
| 206 | return false; |
| 207 | }, |
| 208 | |
| 209 | load_shipping: function( force ) { |
| 210 | if ( true === force || window.confirm( woocommerce_admin_meta_boxes.load_shipping ) ) { |
| 211 | |
| 212 | // Get user ID to load data for |
| 213 | var user_id = $( '#customer_user' ).val(); |
| 214 | |
| 215 | if ( ! user_id ) { |
| 216 | window.alert( woocommerce_admin_meta_boxes.no_customer_selected ); |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | var data = { |
| 221 | user_id: user_id, |
| 222 | action: 'woocommerce_get_customer_details', |
| 223 | security: woocommerce_admin_meta_boxes.get_customer_details_nonce |
| 224 | }; |
| 225 | |
| 226 | $( this ).closest( 'div.edit_address' ).block({ |
| 227 | message: null, |
| 228 | overlayCSS: { |
| 229 | background: '#fff', |
| 230 | opacity: 0.6 |
| 231 | } |
| 232 | }); |
| 233 | |
| 234 | $.ajax({ |
| 235 | url: woocommerce_admin_meta_boxes.ajax_url, |
| 236 | data: data, |
| 237 | type: 'POST', |
| 238 | success: function( response ) { |
| 239 | if ( response && response.billing ) { |
| 240 | $.each( response.shipping, function( key, data ) { |
| 241 | $( ':input#_shipping_' + key ).val( data ).trigger( 'change' ); |
| 242 | }); |
| 243 | } |
| 244 | $( 'div.edit_address' ).unblock(); |
| 245 | } |
| 246 | }); |
| 247 | } |
| 248 | return false; |
| 249 | }, |
| 250 | |
| 251 | copy_billing_to_shipping: function() { |
| 252 | if ( window.confirm( woocommerce_admin_meta_boxes.copy_billing ) ) { |
| 253 | $('.order_data_column :input[name^="_billing_"]').each( function() { |
| 254 | var input_name = $(this).attr('name'); |
| 255 | input_name = input_name.replace( '_billing_', '_shipping_' ); |
| 256 | $( ':input#' + input_name ).val( $(this).val() ).trigger( 'change' ); |
| 257 | }); |
| 258 | } |
| 259 | return false; |
| 260 | } |
| 261 | }; |
| 262 | |
| 263 | /** |
| 264 | * Order Items Panel |
| 265 | */ |
| 266 | var wc_meta_boxes_order_items = { |
| 267 | init: function() { |
| 268 | this.stupidtable.init(); |
| 269 | |
| 270 | $( '#woocommerce-order-items' ) |
| 271 | .on( 'click', 'button.add-line-item', this.add_line_item ) |
| 272 | .on( 'click', 'button.add-coupon', this.add_coupon ) |
| 273 | .on( 'click', 'a.remove-coupon', this.remove_coupon ) |
| 274 | .on( 'click', 'button.refund-items', this.refund_items ) |
| 275 | .on( 'click', '.cancel-action', this.cancel ) |
| 276 | .on( 'click', '.refund-actions .cancel-action', this.track_cancel ) |
| 277 | .on( 'click', 'button.add-order-item', this.add_item ) |
| 278 | .on( 'click', 'button.add-order-fee', this.add_fee ) |
| 279 | .on( 'click', 'button.add-order-shipping', this.add_shipping ) |
| 280 | .on( 'click', 'button.add-order-tax', this.add_tax ) |
| 281 | .on( 'click', 'button.save-action', this.save_line_items ) |
| 282 | .on( 'click', 'a.delete-order-tax', this.delete_tax ) |
| 283 | .on( 'click', 'button.calculate-action', this.recalculate ) |
| 284 | .on( 'click', 'a.edit-order-item', this.edit_item ) |
| 285 | .on( 'click', 'a.delete-order-item', this.delete_item ) |
| 286 | |
| 287 | // Refunds |
| 288 | .on( 'click', '.delete_refund', this.refunds.delete_refund ) |
| 289 | .on( 'click', 'button.do-api-refund, button.do-manual-refund', this.refunds.do_refund ) |
| 290 | .on( 'change', '.refund input.refund_line_total, .refund input.refund_line_tax', this.refunds.input_changed ) |
| 291 | .on( 'change keyup', '.wc-order-refund-items #refund_amount', this.refunds.amount_changed ) |
| 292 | .on( 'change', 'input.refund_order_item_qty', this.refunds.refund_quantity_changed ) |
| 293 | |
| 294 | // Qty |
| 295 | .on( 'change', 'input.quantity', this.quantity_changed ) |
| 296 | |
| 297 | // Subtotal/total |
| 298 | .on( 'keyup change', '.split-input :input', function() { |
| 299 | var $subtotal = $( this ).parent().prev().find(':input'); |
| 300 | if ( $subtotal && ( $subtotal.val() === '' || $subtotal.is( '.match-total' ) ) ) { |
| 301 | $subtotal.val( $( this ).val() ).addClass( 'match-total' ); |
| 302 | } |
| 303 | }) |
| 304 | |
| 305 | .on( 'keyup', '.split-input :input', function() { |
| 306 | $( this ).removeClass( 'match-total' ); |
| 307 | }) |
| 308 | |
| 309 | // Meta |
| 310 | .on( 'click', 'button.add_order_item_meta', this.item_meta.add ) |
| 311 | .on( 'click', 'button.remove_order_item_meta', this.item_meta.remove ) |
| 312 | |
| 313 | // Reload items |
| 314 | .on( 'wc_order_items_reload', this.reload_items ) |
| 315 | .on( 'wc_order_items_reloaded', this.reloaded_items ); |
| 316 | |
| 317 | $( document.body ) |
| 318 | .on( 'wc_backbone_modal_loaded', this.backbone.init ) |
| 319 | .on( 'wc_backbone_modal_response', this.backbone.response ); |
| 320 | }, |
| 321 | |
| 322 | block: function() { |
| 323 | $( '#woocommerce-order-items' ).block({ |
| 324 | message: null, |
| 325 | overlayCSS: { |
| 326 | background: '#fff', |
| 327 | opacity: 0.6 |
| 328 | } |
| 329 | }); |
| 330 | }, |
| 331 | |
| 332 | unblock: function() { |
| 333 | $( '#woocommerce-order-items' ).unblock(); |
| 334 | }, |
| 335 | |
| 336 | filter_data: function( handle, data ) { |
| 337 | const filteredData = $( '#woocommerce-order-items' ) |
| 338 | .triggerHandler( |
| 339 | `woocommerce_order_meta_box_${handle}_ajax_data`, |
| 340 | [ data ] |
| 341 | ); |
| 342 | |
| 343 | if ( filteredData ) { |
| 344 | return filteredData; |
| 345 | } |
| 346 | |
| 347 | return data; |
| 348 | }, |
| 349 | |
| 350 | reload_items: function() { |
| 351 | var data = { |
| 352 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 353 | action: 'woocommerce_load_order_items', |
| 354 | security: woocommerce_admin_meta_boxes.order_item_nonce |
| 355 | }; |
| 356 | |
| 357 | data = wc_meta_boxes_order_items.filter_data( 'reload_items', data ); |
| 358 | |
| 359 | wc_meta_boxes_order_items.block(); |
| 360 | |
| 361 | $.ajax({ |
| 362 | url: woocommerce_admin_meta_boxes.ajax_url, |
| 363 | data: data, |
| 364 | type: 'POST', |
| 365 | success: function( response ) { |
| 366 | $( '#woocommerce-order-items' ).find( '.inside' ).empty(); |
| 367 | $( '#woocommerce-order-items' ).find( '.inside' ).append( response ); |
| 368 | wc_meta_boxes_order_items.reloaded_items(); |
| 369 | wc_meta_boxes_order_items.unblock(); |
| 370 | } |
| 371 | }); |
| 372 | }, |
| 373 | |
| 374 | reloaded_items: function() { |
| 375 | wc_meta_boxes_order.init_tiptip(); |
| 376 | wc_meta_boxes_order_items.stupidtable.init(); |
| 377 | }, |
| 378 | |
| 379 | // When the qty is changed, increase or decrease costs |
| 380 | quantity_changed: function() { |
| 381 | var $row = $( this ).closest( 'tr.item' ); |
| 382 | var qty = $( this ).val(); |
| 383 | var o_qty = $( this ).attr( 'data-qty' ); |
| 384 | var line_total = $( 'input.line_total', $row ); |
| 385 | var line_subtotal = $( 'input.line_subtotal', $row ); |
| 386 | |
| 387 | // Totals |
| 388 | var unit_total = accounting.unformat( line_total.attr( 'data-total' ), woocommerce_admin.mon_decimal_point ) / o_qty; |
| 389 | line_total.val( |
| 390 | parseFloat( accounting.formatNumber( unit_total * qty, woocommerce_admin_meta_boxes.rounding_precision, '' ) ) |
| 391 | .toString() |
| 392 | .replace( '.', woocommerce_admin.mon_decimal_point ) |
| 393 | ); |
| 394 | |
| 395 | var unit_subtotal = accounting.unformat( line_subtotal.attr( 'data-subtotal' ), woocommerce_admin.mon_decimal_point ) / o_qty; |
| 396 | line_subtotal.val( |
| 397 | parseFloat( accounting.formatNumber( unit_subtotal * qty, woocommerce_admin_meta_boxes.rounding_precision, '' ) ) |
| 398 | .toString() |
| 399 | .replace( '.', woocommerce_admin.mon_decimal_point ) |
| 400 | ); |
| 401 | |
| 402 | // Taxes |
| 403 | $( 'input.line_tax', $row ).each( function() { |
| 404 | var $line_total_tax = $( this ); |
| 405 | var tax_id = $line_total_tax.data( 'tax_id' ); |
| 406 | var unit_total_tax = accounting.unformat( |
| 407 | $line_total_tax.attr( 'data-total_tax' ), |
| 408 | woocommerce_admin.mon_decimal_point |
| 409 | ) / o_qty; |
| 410 | var $line_subtotal_tax = $( 'input.line_subtotal_tax[data-tax_id="' + tax_id + '"]', $row ); |
| 411 | var unit_subtotal_tax = accounting.unformat( |
| 412 | $line_subtotal_tax.attr( 'data-subtotal_tax' ), |
| 413 | woocommerce_admin.mon_decimal_point |
| 414 | ) / o_qty; |
| 415 | var round_at_subtotal = 'yes' === woocommerce_admin_meta_boxes.round_at_subtotal; |
| 416 | var precision = woocommerce_admin_meta_boxes[ |
| 417 | round_at_subtotal ? 'rounding_precision' : 'currency_format_num_decimals' |
| 418 | ]; |
| 419 | |
| 420 | if ( 0 < unit_total_tax ) { |
| 421 | $line_total_tax.val( |
| 422 | parseFloat( accounting.formatNumber( unit_total_tax * qty, precision, '' ) ) |
| 423 | .toString() |
| 424 | .replace( '.', woocommerce_admin.mon_decimal_point ) |
| 425 | ); |
| 426 | } |
| 427 | |
| 428 | if ( 0 < unit_subtotal_tax ) { |
| 429 | $line_subtotal_tax.val( |
| 430 | parseFloat( accounting.formatNumber( unit_subtotal_tax * qty, precision, '' ) ) |
| 431 | .toString() |
| 432 | .replace( '.', woocommerce_admin.mon_decimal_point ) |
| 433 | ); |
| 434 | } |
| 435 | }); |
| 436 | |
| 437 | $( this ).trigger( 'quantity_changed' ); |
| 438 | }, |
| 439 | |
| 440 | add_line_item: function() { |
| 441 | $( 'div.wc-order-add-item' ).slideDown(); |
| 442 | $( 'div.wc-order-data-row-toggle' ).not( 'div.wc-order-add-item' ).slideUp(); |
| 443 | |
| 444 | window.wcTracks.recordEvent( 'order_edit_add_items_click', { |
| 445 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 446 | status: $( '#order_status' ).val() |
| 447 | } ); |
| 448 | |
| 449 | return false; |
| 450 | }, |
| 451 | |
| 452 | add_coupon: function() { |
| 453 | window.wcTracks.recordEvent( 'order_edit_add_coupon_click', { |
| 454 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 455 | status: $( '#order_status' ).val() |
| 456 | } ); |
| 457 | |
| 458 | var value = window.prompt( woocommerce_admin_meta_boxes.i18n_apply_coupon ); |
| 459 | |
| 460 | if ( null == value ) { |
| 461 | window.wcTracks.recordEvent( 'order_edit_add_coupon_cancel', { |
| 462 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 463 | status: $( '#order_status' ).val() |
| 464 | } ); |
| 465 | } else { |
| 466 | wc_meta_boxes_order_items.block(); |
| 467 | |
| 468 | var user_id = $( '#customer_user' ).val(); |
| 469 | var user_email = $( '#_billing_email' ).val(); |
| 470 | |
| 471 | var data = $.extend( {}, wc_meta_boxes_order_items.get_taxable_address(), { |
| 472 | action : 'woocommerce_add_coupon_discount', |
| 473 | dataType : 'json', |
| 474 | order_id : woocommerce_admin_meta_boxes.post_id, |
| 475 | security : woocommerce_admin_meta_boxes.order_item_nonce, |
| 476 | coupon : value, |
| 477 | user_id : user_id, |
| 478 | user_email : user_email |
| 479 | } ); |
| 480 | |
| 481 | data = wc_meta_boxes_order_items.filter_data( 'add_coupon', data ); |
| 482 | |
| 483 | $.ajax( { |
| 484 | url: woocommerce_admin_meta_boxes.ajax_url, |
| 485 | data: data, |
| 486 | type: 'POST', |
| 487 | success: function( response ) { |
| 488 | if ( response.success ) { |
| 489 | $( '#woocommerce-order-items' ).find( '.inside' ).empty(); |
| 490 | $( '#woocommerce-order-items' ).find( '.inside' ).append( response.data.html ); |
| 491 | |
| 492 | // Update notes. |
| 493 | if ( response.data.notes_html ) { |
| 494 | $( 'ul.order_notes' ).empty(); |
| 495 | $( 'ul.order_notes' ).append( $( response.data.notes_html ).find( 'li' ) ); |
| 496 | } |
| 497 | |
| 498 | wc_meta_boxes_order_items.reloaded_items(); |
| 499 | wc_meta_boxes_order_items.unblock(); |
| 500 | } else { |
| 501 | window.alert( response.data.error ); |
| 502 | } |
| 503 | wc_meta_boxes_order_items.unblock(); |
| 504 | }, |
| 505 | complete: function() { |
| 506 | window.wcTracks.recordEvent( 'order_edit_added_coupon', { |
| 507 | order_id: data.order_id, |
| 508 | status: $( '#order_status' ).val() |
| 509 | } ); |
| 510 | } |
| 511 | } ); |
| 512 | } |
| 513 | return false; |
| 514 | }, |
| 515 | |
| 516 | remove_coupon: function() { |
| 517 | var $this = $( this ); |
| 518 | wc_meta_boxes_order_items.block(); |
| 519 | |
| 520 | var data = $.extend( {}, wc_meta_boxes_order_items.get_taxable_address(), { |
| 521 | action : 'woocommerce_remove_order_coupon', |
| 522 | dataType : 'json', |
| 523 | order_id : woocommerce_admin_meta_boxes.post_id, |
| 524 | security : woocommerce_admin_meta_boxes.order_item_nonce, |
| 525 | coupon : $this.data( 'code' ) |
| 526 | } ); |
| 527 | |
| 528 | data = wc_meta_boxes_order_items.filter_data( 'remove_coupon', data ); |
| 529 | |
| 530 | $.post( woocommerce_admin_meta_boxes.ajax_url, data, function( response ) { |
| 531 | if ( response.success ) { |
| 532 | $( '#woocommerce-order-items' ).find( '.inside' ).empty(); |
| 533 | $( '#woocommerce-order-items' ).find( '.inside' ).append( response.data.html ); |
| 534 | |
| 535 | // Update notes. |
| 536 | if ( response.data.notes_html ) { |
| 537 | $( 'ul.order_notes' ).empty(); |
| 538 | $( 'ul.order_notes' ).append( $( response.data.notes_html ).find( 'li' ) ); |
| 539 | } |
| 540 | |
| 541 | wc_meta_boxes_order_items.reloaded_items(); |
| 542 | wc_meta_boxes_order_items.unblock(); |
| 543 | } else { |
| 544 | window.alert( response.data.error ); |
| 545 | } |
| 546 | wc_meta_boxes_order_items.unblock(); |
| 547 | }); |
| 548 | }, |
| 549 | |
| 550 | refund_items: function() { |
| 551 | $( 'div.wc-order-refund-items' ).slideDown(); |
| 552 | $( 'div.wc-order-data-row-toggle' ).not( 'div.wc-order-refund-items' ).slideUp(); |
| 553 | $( 'div.wc-order-totals-items' ).slideUp(); |
| 554 | $( '#woocommerce-order-items' ).find( 'div.refund' ).show(); |
| 555 | $( '.wc-order-edit-line-item .wc-order-edit-line-item-actions' ).hide(); |
| 556 | |
| 557 | window.wcTracks.recordEvent( 'order_edit_refund_button_click', { |
| 558 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 559 | status: $( '#order_status' ).val() |
| 560 | } ); |
| 561 | |
| 562 | return false; |
| 563 | }, |
| 564 | |
| 565 | cancel: function() { |
| 566 | $( 'div.wc-order-data-row-toggle' ).not( 'div.wc-order-bulk-actions' ).slideUp(); |
| 567 | $( 'div.wc-order-bulk-actions' ).slideDown(); |
| 568 | $( 'div.wc-order-totals-items' ).slideDown(); |
| 569 | $( '#woocommerce-order-items' ).find( 'div.refund' ).hide(); |
| 570 | $( '.wc-order-edit-line-item .wc-order-edit-line-item-actions' ).show(); |
| 571 | |
| 572 | // Reload the items |
| 573 | if ( 'true' === $( this ).attr( 'data-reload' ) ) { |
| 574 | wc_meta_boxes_order_items.reload_items(); |
| 575 | } |
| 576 | |
| 577 | window.wcTracks.recordEvent( 'order_edit_add_items_cancelled', { |
| 578 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 579 | status: $( '#order_status' ).val() |
| 580 | } ); |
| 581 | |
| 582 | return false; |
| 583 | }, |
| 584 | |
| 585 | track_cancel: function() { |
| 586 | window.wcTracks.recordEvent( 'order_edit_refund_cancel', { |
| 587 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 588 | status: $( '#order_status' ).val() |
| 589 | } ); |
| 590 | }, |
| 591 | |
| 592 | add_item: function() { |
| 593 | $( this ).WCBackboneModal({ |
| 594 | template: 'wc-modal-add-products' |
| 595 | }); |
| 596 | |
| 597 | return false; |
| 598 | }, |
| 599 | |
| 600 | add_fee: function() { |
| 601 | window.wcTracks.recordEvent( 'order_edit_add_fee_click', { |
| 602 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 603 | status: $( '#order_status' ).val() |
| 604 | } ); |
| 605 | |
| 606 | var value = window.prompt( woocommerce_admin_meta_boxes.i18n_add_fee ); |
| 607 | |
| 608 | if ( null == value ) { |
| 609 | window.wcTracks.recordEvent( 'order_edit_add_fee_cancel', { |
| 610 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 611 | status: $( '#order_status' ).val() |
| 612 | } ); |
| 613 | } else { |
| 614 | wc_meta_boxes_order_items.block(); |
| 615 | |
| 616 | var data = $.extend( {}, wc_meta_boxes_order_items.get_taxable_address(), { |
| 617 | action : 'woocommerce_add_order_fee', |
| 618 | dataType: 'json', |
| 619 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 620 | security: woocommerce_admin_meta_boxes.order_item_nonce, |
| 621 | amount : value |
| 622 | } ); |
| 623 | |
| 624 | data = wc_meta_boxes_order_items.filter_data( 'add_fee', data ); |
| 625 | |
| 626 | $.post( woocommerce_admin_meta_boxes.ajax_url, data, function( response ) { |
| 627 | if ( response.success ) { |
| 628 | $( '#woocommerce-order-items' ).find( '.inside' ).empty(); |
| 629 | $( '#woocommerce-order-items' ).find( '.inside' ).append( response.data.html ); |
| 630 | wc_meta_boxes_order_items.reloaded_items(); |
| 631 | wc_meta_boxes_order_items.unblock(); |
| 632 | window.wcTracks.recordEvent( 'order_edit_added_fee', { |
| 633 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 634 | status: $( '#order_status' ).val() |
| 635 | } ); |
| 636 | } else { |
| 637 | window.alert( response.data.error ); |
| 638 | } |
| 639 | wc_meta_boxes_order_items.unblock(); |
| 640 | }); |
| 641 | } |
| 642 | return false; |
| 643 | }, |
| 644 | |
| 645 | add_shipping: function() { |
| 646 | wc_meta_boxes_order_items.block(); |
| 647 | |
| 648 | var data = { |
| 649 | action : 'woocommerce_add_order_shipping', |
| 650 | order_id : woocommerce_admin_meta_boxes.post_id, |
| 651 | security : woocommerce_admin_meta_boxes.order_item_nonce, |
| 652 | dataType : 'json' |
| 653 | }; |
| 654 | |
| 655 | data = wc_meta_boxes_order_items.filter_data( 'add_shipping', data ); |
| 656 | |
| 657 | $.post( woocommerce_admin_meta_boxes.ajax_url, data, function( response ) { |
| 658 | if ( response.success ) { |
| 659 | $( 'table.woocommerce_order_items tbody#order_shipping_line_items' ).append( response.data.html ); |
| 660 | window.wcTracks.recordEvent( 'order_edit_add_shipping', { |
| 661 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 662 | status: $( '#order_status' ).val() |
| 663 | } ); |
| 664 | } else { |
| 665 | window.alert( response.data.error ); |
| 666 | } |
| 667 | wc_meta_boxes_order_items.unblock(); |
| 668 | }); |
| 669 | |
| 670 | return false; |
| 671 | }, |
| 672 | |
| 673 | add_tax: function() { |
| 674 | $( this ).WCBackboneModal({ |
| 675 | template: 'wc-modal-add-tax' |
| 676 | }); |
| 677 | return false; |
| 678 | }, |
| 679 | |
| 680 | edit_item: function() { |
| 681 | $( this ).closest( 'tr' ).find( '.view' ).hide(); |
| 682 | $( this ).closest( 'tr' ).find( '.edit' ).show(); |
| 683 | $( this ).hide(); |
| 684 | $( 'button.add-line-item' ).trigger( 'click' ); |
| 685 | $( 'button.cancel-action' ).attr( 'data-reload', true ); |
| 686 | window.wcTracks.recordEvent( 'order_edit_edit_item_click', { |
| 687 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 688 | status: $( '#order_status' ).val() |
| 689 | } ); |
| 690 | return false; |
| 691 | }, |
| 692 | |
| 693 | delete_item: function() { |
| 694 | var notice = woocommerce_admin_meta_boxes.remove_item_notice; |
| 695 | |
| 696 | if ( $( this ).parents( 'tbody#order_fee_line_items' ).length ) { |
| 697 | notice = woocommerce_admin_meta_boxes.remove_fee_notice; |
| 698 | } |
| 699 | |
| 700 | if ( $( this ).parents( 'tbody#order_shipping_line_items' ).length ) { |
| 701 | notice = woocommerce_admin_meta_boxes.remove_shipping_notice; |
| 702 | } |
| 703 | |
| 704 | var answer = window.confirm( notice ); |
| 705 | |
| 706 | if ( answer ) { |
| 707 | var $item = $( this ).closest( 'tr.item, tr.fee, tr.shipping' ); |
| 708 | var order_item_id = $item.attr( 'data-order_item_id' ); |
| 709 | |
| 710 | wc_meta_boxes_order_items.block(); |
| 711 | |
| 712 | var data = $.extend( {}, wc_meta_boxes_order_items.get_taxable_address(), { |
| 713 | order_id : woocommerce_admin_meta_boxes.post_id, |
| 714 | order_item_ids: order_item_id, |
| 715 | action : 'woocommerce_remove_order_item', |
| 716 | security : woocommerce_admin_meta_boxes.order_item_nonce |
| 717 | } ); |
| 718 | |
| 719 | // Check if items have changed, if so pass them through so we can save them before deleting. |
| 720 | if ( 'true' === $( 'button.cancel-action' ).attr( 'data-reload' ) ) { |
| 721 | data.items = $( 'table.woocommerce_order_items :input[name], .wc-order-totals-items :input[name]' ).serialize(); |
| 722 | } |
| 723 | |
| 724 | data = wc_meta_boxes_order_items.filter_data( 'delete_item', data ); |
| 725 | |
| 726 | $.ajax({ |
| 727 | url: woocommerce_admin_meta_boxes.ajax_url, |
| 728 | data: data, |
| 729 | type: 'POST', |
| 730 | success: function( response ) { |
| 731 | if ( response.success ) { |
| 732 | $( '#woocommerce-order-items' ).find( '.inside' ).empty(); |
| 733 | $( '#woocommerce-order-items' ).find( '.inside' ).append( response.data.html ); |
| 734 | |
| 735 | // Update notes. |
| 736 | if ( response.data.notes_html ) { |
| 737 | $( 'ul.order_notes' ).empty(); |
| 738 | $( 'ul.order_notes' ).append( $( response.data.notes_html ).find( 'li' ) ); |
| 739 | } |
| 740 | |
| 741 | wc_meta_boxes_order_items.reloaded_items(); |
| 742 | wc_meta_boxes_order_items.unblock(); |
| 743 | } else { |
| 744 | window.alert( response.data.error ); |
| 745 | } |
| 746 | wc_meta_boxes_order_items.unblock(); |
| 747 | }, |
| 748 | complete: function() { |
| 749 | window.wcTracks.recordEvent( 'order_edit_remove_item', { |
| 750 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 751 | status: $( '#order_status' ).val() |
| 752 | } ); |
| 753 | } |
| 754 | }); |
| 755 | } |
| 756 | return false; |
| 757 | }, |
| 758 | |
| 759 | delete_tax: function() { |
| 760 | if ( window.confirm( woocommerce_admin_meta_boxes.i18n_delete_tax ) ) { |
| 761 | wc_meta_boxes_order_items.block(); |
| 762 | |
| 763 | var data = { |
| 764 | action: 'woocommerce_remove_order_tax', |
| 765 | rate_id: $( this ).attr( 'data-rate_id' ), |
| 766 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 767 | security: woocommerce_admin_meta_boxes.order_item_nonce |
| 768 | }; |
| 769 | |
| 770 | data = wc_meta_boxes_order_items.filter_data( 'delete_tax', data ); |
| 771 | |
| 772 | $.ajax({ |
| 773 | url: woocommerce_admin_meta_boxes.ajax_url, |
| 774 | data: data, |
| 775 | type: 'POST', |
| 776 | success: function( response ) { |
| 777 | if ( response.success ) { |
| 778 | $( '#woocommerce-order-items' ).find( '.inside' ).empty(); |
| 779 | $( '#woocommerce-order-items' ).find( '.inside' ).append( response.data.html ); |
| 780 | wc_meta_boxes_order_items.reloaded_items(); |
| 781 | wc_meta_boxes_order_items.unblock(); |
| 782 | } else { |
| 783 | window.alert( response.data.error ); |
| 784 | } |
| 785 | wc_meta_boxes_order_items.unblock(); |
| 786 | }, |
| 787 | complete: function() { |
| 788 | window.wcTracks.recordEvent( 'order_edit_delete_tax', { |
| 789 | order_id: data.order_id, |
| 790 | status: $( '#order_status' ).val() |
| 791 | } ); |
| 792 | } |
| 793 | }); |
| 794 | } else { |
| 795 | window.wcTracks.recordEvent( 'order_edit_delete_tax_cancel', { |
| 796 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 797 | status: $( '#order_status' ).val() |
| 798 | } ); |
| 799 | } |
| 800 | return false; |
| 801 | }, |
| 802 | |
| 803 | get_taxable_address: function() { |
| 804 | var country = ''; |
| 805 | var state = ''; |
| 806 | var postcode = ''; |
| 807 | var city = ''; |
| 808 | |
| 809 | if ( 'shipping' === woocommerce_admin_meta_boxes.tax_based_on ) { |
| 810 | country = $( '#_shipping_country' ).val(); |
| 811 | state = $( '#_shipping_state' ).val(); |
| 812 | postcode = $( '#_shipping_postcode' ).val(); |
| 813 | city = $( '#_shipping_city' ).val(); |
| 814 | } |
| 815 | |
| 816 | if ( 'billing' === woocommerce_admin_meta_boxes.tax_based_on || ! country ) { |
| 817 | country = $( '#_billing_country' ).val(); |
| 818 | state = $( '#_billing_state' ).val(); |
| 819 | postcode = $( '#_billing_postcode' ).val(); |
| 820 | city = $( '#_billing_city' ).val(); |
| 821 | } |
| 822 | |
| 823 | return { |
| 824 | country: country, |
| 825 | state: state, |
| 826 | postcode: postcode, |
| 827 | city: city |
| 828 | }; |
| 829 | }, |
| 830 | |
| 831 | recalculate: function() { |
| 832 | if ( window.confirm( woocommerce_admin_meta_boxes.calc_totals ) ) { |
| 833 | wc_meta_boxes_order_items.block(); |
| 834 | |
| 835 | var data = $.extend( {}, wc_meta_boxes_order_items.get_taxable_address(), { |
| 836 | action: 'woocommerce_calc_line_taxes', |
| 837 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 838 | items: $( 'table.woocommerce_order_items :input[name], .wc-order-totals-items :input[name]' ).serialize(), |
| 839 | security: woocommerce_admin_meta_boxes.calc_totals_nonce |
| 840 | } ); |
| 841 | |
| 842 | data = wc_meta_boxes_order_items.filter_data( 'recalculate', data ); |
| 843 | |
| 844 | $( document.body ).trigger( 'order-totals-recalculate-before', data ); |
| 845 | |
| 846 | $.ajax({ |
| 847 | url: woocommerce_admin_meta_boxes.ajax_url, |
| 848 | data: data, |
| 849 | type: 'POST', |
| 850 | success: function( response ) { |
| 851 | $( '#woocommerce-order-items' ).find( '.inside' ).empty(); |
| 852 | $( '#woocommerce-order-items' ).find( '.inside' ).append( response ); |
| 853 | wc_meta_boxes_order_items.reloaded_items(); |
| 854 | wc_meta_boxes_order_items.unblock(); |
| 855 | |
| 856 | $( document.body ).trigger( 'order-totals-recalculate-success', response ); |
| 857 | }, |
| 858 | complete: function( response ) { |
| 859 | $( document.body ).trigger( 'order-totals-recalculate-complete', response ); |
| 860 | |
| 861 | window.wcTracks.recordEvent( 'order_edit_recalc_totals', { |
| 862 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 863 | ok_cancel: 'OK', |
| 864 | status: $( '#order_status' ).val() |
| 865 | } ); |
| 866 | } |
| 867 | }); |
| 868 | } else { |
| 869 | window.wcTracks.recordEvent( 'order_edit_recalc_totals', { |
| 870 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 871 | ok_cancel: 'cancel', |
| 872 | status: $( '#order_status' ).val() |
| 873 | } ); |
| 874 | } |
| 875 | |
| 876 | return false; |
| 877 | }, |
| 878 | |
| 879 | save_line_items: function() { |
| 880 | var data = { |
| 881 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 882 | items: $( 'table.woocommerce_order_items :input[name], .wc-order-totals-items :input[name]' ).serialize(), |
| 883 | action: 'woocommerce_save_order_items', |
| 884 | security: woocommerce_admin_meta_boxes.order_item_nonce |
| 885 | }; |
| 886 | |
| 887 | data = wc_meta_boxes_order_items.filter_data( 'save_line_items', data ); |
| 888 | |
| 889 | wc_meta_boxes_order_items.block(); |
| 890 | |
| 891 | $.ajax({ |
| 892 | url: woocommerce_admin_meta_boxes.ajax_url, |
| 893 | data: data, |
| 894 | type: 'POST', |
| 895 | success: function( response ) { |
| 896 | if ( response.success ) { |
| 897 | $( '#woocommerce-order-items' ).find( '.inside' ).empty(); |
| 898 | $( '#woocommerce-order-items' ).find( '.inside' ).append( response.data.html ); |
| 899 | |
| 900 | // Update notes. |
| 901 | if ( response.data.notes_html ) { |
| 902 | $( 'ul.order_notes' ).empty(); |
| 903 | $( 'ul.order_notes' ).append( $( response.data.notes_html ).find( 'li' ) ); |
| 904 | } |
| 905 | |
| 906 | wc_meta_boxes_order_items.reloaded_items(); |
| 907 | wc_meta_boxes_order_items.unblock(); |
| 908 | } else { |
| 909 | wc_meta_boxes_order_items.unblock(); |
| 910 | window.alert( response.data.error ); |
| 911 | } |
| 912 | }, |
| 913 | complete: function() { |
| 914 | window.wcTracks.recordEvent( 'order_edit_save_line_items', { |
| 915 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 916 | status: $( '#order_status' ).val() |
| 917 | } ); |
| 918 | } |
| 919 | }); |
| 920 | |
| 921 | $( this ).trigger( 'items_saved' ); |
| 922 | |
| 923 | return false; |
| 924 | }, |
| 925 | |
| 926 | refunds: { |
| 927 | |
| 928 | do_refund: function() { |
| 929 | wc_meta_boxes_order_items.block(); |
| 930 | |
| 931 | if ( window.confirm( woocommerce_admin_meta_boxes.i18n_do_refund ) ) { |
| 932 | var refund_amount = $( 'input#refund_amount' ).val(); |
| 933 | var refund_reason = $( 'input#refund_reason' ).val(); |
| 934 | var refunded_amount = $( 'input#refunded_amount' ).val(); |
| 935 | |
| 936 | // Get line item refunds |
| 937 | var line_item_qtys = {}; |
| 938 | var line_item_totals = {}; |
| 939 | var line_item_tax_totals = {}; |
| 940 | |
| 941 | $( '.refund input.refund_order_item_qty' ).each(function( index, item ) { |
| 942 | if ( $( item ).closest( 'tr' ).data( 'order_item_id' ) ) { |
| 943 | if ( item.value ) { |
| 944 | line_item_qtys[ $( item ).closest( 'tr' ).data( 'order_item_id' ) ] = item.value; |
| 945 | } |
| 946 | } |
| 947 | }); |
| 948 | |
| 949 | $( '.refund input.refund_line_total' ).each(function( index, item ) { |
| 950 | if ( $( item ).closest( 'tr' ).data( 'order_item_id' ) ) { |
| 951 | line_item_totals[ $( item ).closest( 'tr' ).data( 'order_item_id' ) ] = accounting.unformat( |
| 952 | item.value, |
| 953 | woocommerce_admin.mon_decimal_point |
| 954 | ); |
| 955 | } |
| 956 | }); |
| 957 | |
| 958 | $( '.refund input.refund_line_tax' ).each(function( index, item ) { |
| 959 | if ( $( item ).closest( 'tr' ).data( 'order_item_id' ) ) { |
| 960 | var tax_id = $( item ).data( 'tax_id' ); |
| 961 | |
| 962 | if ( ! line_item_tax_totals[ $( item ).closest( 'tr' ).data( 'order_item_id' ) ] ) { |
| 963 | line_item_tax_totals[ $( item ).closest( 'tr' ).data( 'order_item_id' ) ] = {}; |
| 964 | } |
| 965 | |
| 966 | line_item_tax_totals[ $( item ).closest( 'tr' ).data( 'order_item_id' ) ][ tax_id ] = accounting.unformat( |
| 967 | item.value, |
| 968 | woocommerce_admin.mon_decimal_point |
| 969 | ); |
| 970 | } |
| 971 | }); |
| 972 | |
| 973 | var data = { |
| 974 | action : 'woocommerce_refund_line_items', |
| 975 | order_id : woocommerce_admin_meta_boxes.post_id, |
| 976 | refund_amount : refund_amount, |
| 977 | refunded_amount : refunded_amount, |
| 978 | refund_reason : refund_reason, |
| 979 | line_item_qtys : JSON.stringify( line_item_qtys, null, '' ), |
| 980 | line_item_totals : JSON.stringify( line_item_totals, null, '' ), |
| 981 | line_item_tax_totals : JSON.stringify( line_item_tax_totals, null, '' ), |
| 982 | api_refund : $( this ).is( '.do-api-refund' ), |
| 983 | restock_refunded_items: $( '#restock_refunded_items:checked' ).length ? 'true': 'false', |
| 984 | security : woocommerce_admin_meta_boxes.order_item_nonce |
| 985 | }; |
| 986 | |
| 987 | data = wc_meta_boxes_order_items.filter_data( 'do_refund', data ); |
| 988 | |
| 989 | $.ajax( { |
| 990 | url: woocommerce_admin_meta_boxes.ajax_url, |
| 991 | data: data, |
| 992 | type: 'POST', |
| 993 | success: function( response ) { |
| 994 | if ( true === response.success ) { |
| 995 | // Redirect to same page for show the refunded status |
| 996 | window.location.reload(); |
| 997 | } else { |
| 998 | window.alert( response.data.error ); |
| 999 | wc_meta_boxes_order_items.reload_items(); |
| 1000 | wc_meta_boxes_order_items.unblock(); |
| 1001 | } |
| 1002 | }, |
| 1003 | complete: function() { |
| 1004 | window.wcTracks.recordEvent( 'order_edit_refunded', { |
| 1005 | order_id: data.order_id, |
| 1006 | status: $( '#order_status' ).val(), |
| 1007 | api_refund: data.api_refund, |
| 1008 | has_reason: Boolean( data.refund_reason.length ), |
| 1009 | restock: 'true' === data.restock_refunded_items |
| 1010 | } ); |
| 1011 | } |
| 1012 | } ); |
| 1013 | } else { |
| 1014 | wc_meta_boxes_order_items.unblock(); |
| 1015 | } |
| 1016 | }, |
| 1017 | |
| 1018 | delete_refund: function() { |
| 1019 | if ( window.confirm( woocommerce_admin_meta_boxes.i18n_delete_refund ) ) { |
| 1020 | var $refund = $( this ).closest( 'tr.refund' ); |
| 1021 | var refund_id = $refund.attr( 'data-order_refund_id' ); |
| 1022 | |
| 1023 | wc_meta_boxes_order_items.block(); |
| 1024 | |
| 1025 | var data = { |
| 1026 | action: 'woocommerce_delete_refund', |
| 1027 | refund_id: refund_id, |
| 1028 | security: woocommerce_admin_meta_boxes.order_item_nonce |
| 1029 | }; |
| 1030 | |
| 1031 | data = wc_meta_boxes_order_items.filter_data( 'delete_refund', data ); |
| 1032 | |
| 1033 | $.ajax({ |
| 1034 | url: woocommerce_admin_meta_boxes.ajax_url, |
| 1035 | data: data, |
| 1036 | type: 'POST', |
| 1037 | success: function() { |
| 1038 | wc_meta_boxes_order_items.reload_items(); |
| 1039 | } |
| 1040 | }); |
| 1041 | } |
| 1042 | return false; |
| 1043 | }, |
| 1044 | |
| 1045 | input_changed: function() { |
| 1046 | var refund_amount = 0; |
| 1047 | var $items = $( '.woocommerce_order_items' ).find( 'tr.item, tr.fee, tr.shipping' ); |
| 1048 | var round_at_subtotal = 'yes' === woocommerce_admin_meta_boxes.round_at_subtotal; |
| 1049 | |
| 1050 | $items.each(function() { |
| 1051 | var $row = $( this ); |
| 1052 | var refund_cost_fields = $row.find( '.refund input:not(.refund_order_item_qty)' ); |
| 1053 | |
| 1054 | refund_cost_fields.each(function( index, el ) { |
| 1055 | var field_amount = accounting.unformat( $( el ).val() || 0, woocommerce_admin.mon_decimal_point ); |
| 1056 | refund_amount += parseFloat( round_at_subtotal ? |
| 1057 | field_amount : |
| 1058 | accounting.formatNumber( field_amount, woocommerce_admin_meta_boxes.currency_format_num_decimals, '' ) ); |
| 1059 | }); |
| 1060 | }); |
| 1061 | |
| 1062 | $( '#refund_amount' ) |
| 1063 | .val( accounting.formatNumber( |
| 1064 | refund_amount, |
| 1065 | woocommerce_admin_meta_boxes.currency_format_num_decimals, |
| 1066 | '', |
| 1067 | woocommerce_admin.mon_decimal_point |
| 1068 | ) ) |
| 1069 | .trigger( 'change' ); |
| 1070 | }, |
| 1071 | |
| 1072 | amount_changed: function() { |
| 1073 | var total = accounting.unformat( $( this ).val(), woocommerce_admin.mon_decimal_point ); |
| 1074 | |
| 1075 | $( 'button .wc-order-refund-amount .amount' ).text( accounting.formatMoney( total, { |
| 1076 | symbol: woocommerce_admin_meta_boxes.currency_format_symbol, |
| 1077 | decimal: woocommerce_admin_meta_boxes.currency_format_decimal_sep, |
| 1078 | thousand: woocommerce_admin_meta_boxes.currency_format_thousand_sep, |
| 1079 | precision: woocommerce_admin_meta_boxes.currency_format_num_decimals, |
| 1080 | format: woocommerce_admin_meta_boxes.currency_format |
| 1081 | } ) ); |
| 1082 | }, |
| 1083 | |
| 1084 | // When the refund qty is changed, increase or decrease costs |
| 1085 | refund_quantity_changed: function() { |
| 1086 | var $row = $( this ).closest( 'tr.item' ); |
| 1087 | var qty = $row.find( 'input.quantity' ).val(); |
| 1088 | var refund_qty = $( this ).val(); |
| 1089 | var line_total = $( 'input.line_total', $row ); |
| 1090 | var refund_line_total = $( 'input.refund_line_total', $row ); |
| 1091 | |
| 1092 | // Totals |
| 1093 | var unit_total = accounting.unformat( line_total.attr( 'data-total' ), woocommerce_admin.mon_decimal_point ) / qty; |
| 1094 | |
| 1095 | refund_line_total.val( |
| 1096 | parseFloat( accounting.formatNumber( unit_total * refund_qty, woocommerce_admin_meta_boxes.rounding_precision, '' ) ) |
| 1097 | .toString() |
| 1098 | .replace( '.', woocommerce_admin.mon_decimal_point ) |
| 1099 | ).trigger( 'change' ); |
| 1100 | |
| 1101 | // Taxes |
| 1102 | $( '.refund_line_tax', $row ).each( function() { |
| 1103 | var $refund_line_total_tax = $( this ); |
| 1104 | var tax_id = $refund_line_total_tax.data( 'tax_id' ); |
| 1105 | var line_total_tax = $( 'input.line_tax[data-tax_id="' + tax_id + '"]', $row ); |
| 1106 | var unit_total_tax = accounting.unformat( |
| 1107 | line_total_tax.data( 'total_tax' ), |
| 1108 | woocommerce_admin.mon_decimal_point |
| 1109 | ) / qty; |
| 1110 | |
| 1111 | if ( 0 < unit_total_tax ) { |
| 1112 | var round_at_subtotal = 'yes' === woocommerce_admin_meta_boxes.round_at_subtotal; |
| 1113 | var precision = woocommerce_admin_meta_boxes[ |
| 1114 | round_at_subtotal ? 'rounding_precision' : 'currency_format_num_decimals' |
| 1115 | ]; |
| 1116 | |
| 1117 | $refund_line_total_tax.val( |
| 1118 | parseFloat( accounting.formatNumber( unit_total_tax * refund_qty, precision, '' ) ) |
| 1119 | .toString() |
| 1120 | .replace( '.', woocommerce_admin.mon_decimal_point ) |
| 1121 | ).trigger( 'change' ); |
| 1122 | } else { |
| 1123 | $refund_line_total_tax.val( 0 ).trigger( 'change' ); |
| 1124 | } |
| 1125 | }); |
| 1126 | |
| 1127 | // Restock checkbox |
| 1128 | if ( refund_qty > 0 ) { |
| 1129 | $( '#restock_refunded_items' ).closest( 'tr' ).show(); |
| 1130 | } else { |
| 1131 | $( '#restock_refunded_items' ).closest( 'tr' ).hide(); |
| 1132 | $( '.woocommerce_order_items input.refund_order_item_qty' ).each( function() { |
| 1133 | if ( $( this ).val() > 0 ) { |
| 1134 | $( '#restock_refunded_items' ).closest( 'tr' ).show(); |
| 1135 | } |
| 1136 | }); |
| 1137 | } |
| 1138 | |
| 1139 | $( this ).trigger( 'refund_quantity_changed' ); |
| 1140 | } |
| 1141 | }, |
| 1142 | |
| 1143 | item_meta: { |
| 1144 | |
| 1145 | add: function() { |
| 1146 | var $button = $( this ); |
| 1147 | var $item = $button.closest( 'tr.item, tr.shipping' ); |
| 1148 | var $items = $item.find('tbody.meta_items'); |
| 1149 | var index = $items.find('tr').length + 1; |
| 1150 | var $row = '<tr data-meta_id="0">' + |
| 1151 | '<td>' + |
| 1152 | '<input type="text" maxlength="255" placeholder="' + |
| 1153 | woocommerce_admin_meta_boxes_order.placeholder_name + |
| 1154 | '" name="meta_key[' + $item.attr( 'data-order_item_id' ) + |
| 1155 | '][new-' + index + ']" />' + |
| 1156 | '<textarea placeholder="' + |
| 1157 | woocommerce_admin_meta_boxes_order.placeholder_value + |
| 1158 | '" name="meta_value[' + |
| 1159 | $item.attr( 'data-order_item_id' ) + |
| 1160 | '][new-' + |
| 1161 | index + |
| 1162 | ']"></textarea>' + |
| 1163 | '</td>' + |
| 1164 | '<td width="1%"><button class="remove_order_item_meta button">×</button></td>' + |
| 1165 | '</tr>'; |
| 1166 | $items.append( $row ); |
| 1167 | |
| 1168 | return false; |
| 1169 | }, |
| 1170 | |
| 1171 | remove: function() { |
| 1172 | if ( window.confirm( woocommerce_admin_meta_boxes.remove_item_meta ) ) { |
| 1173 | var $row = $( this ).closest( 'tr' ); |
| 1174 | $row.find( ':input' ).val( '' ); |
| 1175 | $row.hide(); |
| 1176 | } |
| 1177 | return false; |
| 1178 | } |
| 1179 | }, |
| 1180 | |
| 1181 | backbone: { |
| 1182 | |
| 1183 | init: function( e, target ) { |
| 1184 | if ( 'wc-modal-add-products' === target ) { |
| 1185 | $( document.body ).trigger( 'wc-enhanced-select-init' ); |
| 1186 | |
| 1187 | $( this ).on( 'change', '.wc-product-search', function() { |
| 1188 | if ( ! $( this ).closest( 'tr' ).is( ':last-child' ) ) { |
| 1189 | return; |
| 1190 | } |
| 1191 | var item_table = $( this ).closest( 'table.widefat' ), |
| 1192 | item_table_body = item_table.find( 'tbody' ), |
| 1193 | index = item_table_body.find( 'tr' ).length, |
| 1194 | row = item_table_body.data( 'row' ).replace( /\[0\]/g, '[' + index + ']' ); |
| 1195 | |
| 1196 | item_table_body.append( '<tr>' + row + '</tr>' ); |
| 1197 | $( document.body ).trigger( 'wc-enhanced-select-init' ); |
| 1198 | } ); |
| 1199 | } |
| 1200 | }, |
| 1201 | |
| 1202 | response: function( e, target, data ) { |
| 1203 | if ( 'wc-modal-add-tax' === target ) { |
| 1204 | var rate_id = data.add_order_tax; |
| 1205 | var manual_rate_id = ''; |
| 1206 | |
| 1207 | if ( data.manual_tax_rate_id ) { |
| 1208 | manual_rate_id = data.manual_tax_rate_id; |
| 1209 | } |
| 1210 | |
| 1211 | wc_meta_boxes_order_items.backbone.add_tax( rate_id, manual_rate_id ); |
| 1212 | } |
| 1213 | if ( 'wc-modal-add-products' === target ) { |
| 1214 | // Build array of data. |
| 1215 | var item_table = $( this ).find( 'table.widefat' ), |
| 1216 | item_table_body = item_table.find( 'tbody' ), |
| 1217 | rows = item_table_body.find( 'tr' ), |
| 1218 | add_items = []; |
| 1219 | |
| 1220 | $( rows ).each( function() { |
| 1221 | var item_id = $( this ).find( ':input[name="item_id"]' ).val(), |
| 1222 | item_qty = $( this ).find( ':input[name="item_qty"]' ).val(); |
| 1223 | |
| 1224 | add_items.push( { |
| 1225 | 'id' : item_id, |
| 1226 | 'qty': item_qty ? item_qty: 1 |
| 1227 | } ); |
| 1228 | } ); |
| 1229 | |
| 1230 | return wc_meta_boxes_order_items.backbone.add_items( add_items ); |
| 1231 | } |
| 1232 | }, |
| 1233 | |
| 1234 | add_items: function( add_items ) { |
| 1235 | wc_meta_boxes_order_items.block(); |
| 1236 | |
| 1237 | var data = { |
| 1238 | action : 'woocommerce_add_order_item', |
| 1239 | order_id : woocommerce_admin_meta_boxes.post_id, |
| 1240 | security : woocommerce_admin_meta_boxes.order_item_nonce, |
| 1241 | data : add_items |
| 1242 | }; |
| 1243 | |
| 1244 | // Check if items have changed, if so pass them through so we can save them before adding a new item. |
| 1245 | if ( 'true' === $( 'button.cancel-action' ).attr( 'data-reload' ) ) { |
| 1246 | data.items = $( 'table.woocommerce_order_items :input[name], .wc-order-totals-items :input[name]' ).serialize(); |
| 1247 | } |
| 1248 | |
| 1249 | data = wc_meta_boxes_order_items.filter_data( 'add_items', data ); |
| 1250 | |
| 1251 | $.ajax({ |
| 1252 | type: 'POST', |
| 1253 | url: woocommerce_admin_meta_boxes.ajax_url, |
| 1254 | data: data, |
| 1255 | success: function( response ) { |
| 1256 | if ( response.success ) { |
| 1257 | $( '#woocommerce-order-items' ).find( '.inside' ).empty(); |
| 1258 | $( '#woocommerce-order-items' ).find( '.inside' ).append( response.data.html ); |
| 1259 | |
| 1260 | // Update notes. |
| 1261 | if ( response.data.notes_html ) { |
| 1262 | $( 'ul.order_notes' ).empty(); |
| 1263 | $( 'ul.order_notes' ).append( $( response.data.notes_html ).find( 'li' ) ); |
| 1264 | } |
| 1265 | |
| 1266 | wc_meta_boxes_order_items.reloaded_items(); |
| 1267 | wc_meta_boxes_order_items.unblock(); |
| 1268 | } else { |
| 1269 | wc_meta_boxes_order_items.unblock(); |
| 1270 | window.alert( response.data.error ); |
| 1271 | } |
| 1272 | }, |
| 1273 | complete: function() { |
| 1274 | window.wcTracks.recordEvent( 'order_edit_add_products', { |
| 1275 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 1276 | status: $( '#order_status' ).val() |
| 1277 | } ); |
| 1278 | }, |
| 1279 | dataType: 'json' |
| 1280 | }); |
| 1281 | }, |
| 1282 | |
| 1283 | add_tax: function( rate_id, manual_rate_id ) { |
| 1284 | if ( manual_rate_id ) { |
| 1285 | rate_id = manual_rate_id; |
| 1286 | } |
| 1287 | |
| 1288 | if ( ! rate_id ) { |
| 1289 | return false; |
| 1290 | } |
| 1291 | |
| 1292 | var rates = $( '.order-tax-id' ).map( function() { |
| 1293 | return $( this ).val(); |
| 1294 | }).get(); |
| 1295 | |
| 1296 | // Test if already exists |
| 1297 | if ( -1 === $.inArray( rate_id, rates ) ) { |
| 1298 | wc_meta_boxes_order_items.block(); |
| 1299 | |
| 1300 | var data = { |
| 1301 | action: 'woocommerce_add_order_tax', |
| 1302 | rate_id: rate_id, |
| 1303 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 1304 | security: woocommerce_admin_meta_boxes.order_item_nonce |
| 1305 | }; |
| 1306 | |
| 1307 | data = wc_meta_boxes_order_items.filter_data( 'add_tax', data ); |
| 1308 | |
| 1309 | $.ajax({ |
| 1310 | url : woocommerce_admin_meta_boxes.ajax_url, |
| 1311 | data : data, |
| 1312 | dataType : 'json', |
| 1313 | type : 'POST', |
| 1314 | success : function( response ) { |
| 1315 | if ( response.success ) { |
| 1316 | $( '#woocommerce-order-items' ).find( '.inside' ).empty(); |
| 1317 | $( '#woocommerce-order-items' ).find( '.inside' ).append( response.data.html ); |
| 1318 | wc_meta_boxes_order_items.reloaded_items(); |
| 1319 | } else { |
| 1320 | window.alert( response.data.error ); |
| 1321 | } |
| 1322 | wc_meta_boxes_order_items.unblock(); |
| 1323 | }, |
| 1324 | complete: function() { |
| 1325 | window.wcTracks.recordEvent( 'order_edit_add_tax', { |
| 1326 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 1327 | status: $( '#order_status' ).val() |
| 1328 | } ); |
| 1329 | } |
| 1330 | }); |
| 1331 | } else { |
| 1332 | window.alert( woocommerce_admin_meta_boxes.i18n_tax_rate_already_exists ); |
| 1333 | } |
| 1334 | } |
| 1335 | }, |
| 1336 | |
| 1337 | stupidtable: { |
| 1338 | init: function() { |
| 1339 | $( '.woocommerce_order_items' ).stupidtable(); |
| 1340 | $( '.woocommerce_order_items' ).on( 'aftertablesort', this.add_arrows ); |
| 1341 | }, |
| 1342 | |
| 1343 | add_arrows: function( event, data ) { |
| 1344 | var th = $( this ).find( 'th' ); |
| 1345 | var arrow = data.direction === 'asc' ? '↑' : '↓'; |
| 1346 | var index = data.column; |
| 1347 | th.find( '.wc-arrow' ).remove(); |
| 1348 | th.eq( index ).append( '<span class="wc-arrow">' + arrow + '</span>' ); |
| 1349 | } |
| 1350 | } |
| 1351 | }; |
| 1352 | |
| 1353 | /** |
| 1354 | * Order Notes Panel |
| 1355 | */ |
| 1356 | var wc_meta_boxes_order_notes = { |
| 1357 | init: function() { |
| 1358 | $( '#woocommerce-order-notes' ) |
| 1359 | .on( 'click', 'button.add_note', this.add_order_note ) |
| 1360 | .on( 'click', 'a.delete_note', this.delete_order_note ); |
| 1361 | |
| 1362 | }, |
| 1363 | |
| 1364 | add_order_note: function() { |
| 1365 | if ( ! $( 'textarea#add_order_note' ).val() ) { |
| 1366 | return; |
| 1367 | } |
| 1368 | |
| 1369 | $( '#woocommerce-order-notes' ).block({ |
| 1370 | message: null, |
| 1371 | overlayCSS: { |
| 1372 | background: '#fff', |
| 1373 | opacity: 0.6 |
| 1374 | } |
| 1375 | }); |
| 1376 | |
| 1377 | var data = { |
| 1378 | action: 'woocommerce_add_order_note', |
| 1379 | post_id: woocommerce_admin_meta_boxes.post_id, |
| 1380 | note: $( 'textarea#add_order_note' ).val(), |
| 1381 | note_type: $( 'select#order_note_type' ).val(), |
| 1382 | security: woocommerce_admin_meta_boxes.add_order_note_nonce |
| 1383 | }; |
| 1384 | |
| 1385 | $.post( woocommerce_admin_meta_boxes.ajax_url, data, function( response ) { |
| 1386 | $( 'ul.order_notes .no-items' ).remove(); |
| 1387 | $( 'ul.order_notes' ).prepend( response ); |
| 1388 | $( '#woocommerce-order-notes' ).unblock(); |
| 1389 | $( '#add_order_note' ).val( '' ); |
| 1390 | window.wcTracks.recordEvent( 'order_edit_add_order_note', { |
| 1391 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 1392 | note_type: data.note_type || 'private', |
| 1393 | status: $( '#order_status' ).val() |
| 1394 | } ); |
| 1395 | }); |
| 1396 | |
| 1397 | return false; |
| 1398 | }, |
| 1399 | |
| 1400 | delete_order_note: function() { |
| 1401 | if ( window.confirm( woocommerce_admin_meta_boxes.i18n_delete_note ) ) { |
| 1402 | var note = $( this ).closest( 'li.note' ); |
| 1403 | |
| 1404 | $( note ).block({ |
| 1405 | message: null, |
| 1406 | overlayCSS: { |
| 1407 | background: '#fff', |
| 1408 | opacity: 0.6 |
| 1409 | } |
| 1410 | }); |
| 1411 | |
| 1412 | var data = { |
| 1413 | action: 'woocommerce_delete_order_note', |
| 1414 | note_id: $( note ).attr( 'rel' ), |
| 1415 | security: woocommerce_admin_meta_boxes.delete_order_note_nonce |
| 1416 | }; |
| 1417 | |
| 1418 | $.post( woocommerce_admin_meta_boxes.ajax_url, data, function() { |
| 1419 | $( note ).remove(); |
| 1420 | }); |
| 1421 | } |
| 1422 | |
| 1423 | return false; |
| 1424 | } |
| 1425 | }; |
| 1426 | |
| 1427 | /** |
| 1428 | * Order Downloads Panel |
| 1429 | */ |
| 1430 | var wc_meta_boxes_order_downloads = { |
| 1431 | init: function() { |
| 1432 | $( '.order_download_permissions' ) |
| 1433 | .on( 'click', 'button.grant_access', this.grant_access ) |
| 1434 | .on( 'click', 'button.revoke_access', this.revoke_access ) |
| 1435 | .on( 'click', '#copy-download-link', this.copy_link ) |
| 1436 | .on( 'aftercopy', '#copy-download-link', this.copy_success ) |
| 1437 | .on( 'aftercopyfailure', '#copy-download-link', this.copy_fail ); |
| 1438 | }, |
| 1439 | |
| 1440 | grant_access: function() { |
| 1441 | var products = $( '#grant_access_id' ).val(); |
| 1442 | |
| 1443 | if ( ! products || 0 === products.length ) { |
| 1444 | return; |
| 1445 | } |
| 1446 | |
| 1447 | $( '.order_download_permissions' ).block({ |
| 1448 | message: null, |
| 1449 | overlayCSS: { |
| 1450 | background: '#fff', |
| 1451 | opacity: 0.6 |
| 1452 | } |
| 1453 | }); |
| 1454 | |
| 1455 | var data = { |
| 1456 | action: 'woocommerce_grant_access_to_download', |
| 1457 | product_ids: products, |
| 1458 | loop: $('.order_download_permissions .wc-metabox').length, |
| 1459 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 1460 | security: woocommerce_admin_meta_boxes.grant_access_nonce |
| 1461 | }; |
| 1462 | |
| 1463 | $.post( woocommerce_admin_meta_boxes.ajax_url, data, function( response ) { |
| 1464 | |
| 1465 | if ( response && -1 !== parseInt( response ) ) { |
| 1466 | $( '.order_download_permissions .wc-metaboxes' ).append( response ); |
| 1467 | } else { |
| 1468 | window.alert( woocommerce_admin_meta_boxes.i18n_download_permission_fail ); |
| 1469 | } |
| 1470 | |
| 1471 | $( document.body ).trigger( 'wc-init-datepickers' ); |
| 1472 | $( '#grant_access_id' ).val( '' ).trigger( 'change' ); |
| 1473 | $( '.order_download_permissions' ).unblock(); |
| 1474 | }); |
| 1475 | |
| 1476 | return false; |
| 1477 | }, |
| 1478 | |
| 1479 | revoke_access: function () { |
| 1480 | if ( window.confirm( woocommerce_admin_meta_boxes.i18n_permission_revoke ) ) { |
| 1481 | var el = $( this ).parent().parent(); |
| 1482 | var product = $( this ).attr( 'rel' ).split( ',' )[0]; |
| 1483 | var file = $( this ).attr( 'rel' ).split( ',' )[1]; |
| 1484 | var permission_id = $( this ).data( 'permission_id' ); |
| 1485 | |
| 1486 | if ( product > 0 ) { |
| 1487 | $( el ).block({ |
| 1488 | message: null, |
| 1489 | overlayCSS: { |
| 1490 | background: '#fff', |
| 1491 | opacity: 0.6 |
| 1492 | } |
| 1493 | }); |
| 1494 | |
| 1495 | var data = { |
| 1496 | action: 'woocommerce_revoke_access_to_download', |
| 1497 | product_id: product, |
| 1498 | download_id: file, |
| 1499 | permission_id: permission_id, |
| 1500 | order_id: woocommerce_admin_meta_boxes.post_id, |
| 1501 | security: woocommerce_admin_meta_boxes.revoke_access_nonce |
| 1502 | }; |
| 1503 | |
| 1504 | $.post( woocommerce_admin_meta_boxes.ajax_url, data, function() { |
| 1505 | // Success |
| 1506 | $( el ).fadeOut( '300', function () { |
| 1507 | $( el ).remove(); |
| 1508 | }); |
| 1509 | }); |
| 1510 | |
| 1511 | } else { |
| 1512 | $( el ).fadeOut( '300', function () { |
| 1513 | $( el ).remove(); |
| 1514 | }); |
| 1515 | } |
| 1516 | } |
| 1517 | return false; |
| 1518 | }, |
| 1519 | |
| 1520 | /** |
| 1521 | * Copy download link. |
| 1522 | * |
| 1523 | * @param {Object} evt Copy event. |
| 1524 | */ |
| 1525 | copy_link: function( evt ) { |
| 1526 | wcClearClipboard(); |
| 1527 | wcSetClipboard( $( this ).attr( 'href' ), $( this ) ); |
| 1528 | evt.preventDefault(); |
| 1529 | }, |
| 1530 | |
| 1531 | /** |
| 1532 | * Display a "Copied!" tip when success copying |
| 1533 | */ |
| 1534 | copy_success: function() { |
| 1535 | $( this ).tipTip({ |
| 1536 | 'attribute': 'data-tip', |
| 1537 | 'activation': 'focus', |
| 1538 | 'fadeIn': 50, |
| 1539 | 'fadeOut': 50, |
| 1540 | 'delay': 0 |
| 1541 | }).trigger( 'focus' ); |
| 1542 | }, |
| 1543 | |
| 1544 | /** |
| 1545 | * Displays the copy error message when failure copying. |
| 1546 | */ |
| 1547 | copy_fail: function() { |
| 1548 | $( this ).tipTip({ |
| 1549 | 'attribute': 'data-tip-failed', |
| 1550 | 'activation': 'focus', |
| 1551 | 'fadeIn': 50, |
| 1552 | 'fadeOut': 50, |
| 1553 | 'delay': 0 |
| 1554 | }).trigger( 'focus' ); |
| 1555 | } |
| 1556 | }; |
| 1557 | |
| 1558 | /** |
| 1559 | * Configures ajax request for custom metadata box in order edit screen. |
| 1560 | */ |
| 1561 | var wc_meta_boxes_order_custom_meta = { |
| 1562 | init: function() { |
| 1563 | if ( ! $('#order_custom').length ) { |
| 1564 | return; |
| 1565 | } |
| 1566 | |
| 1567 | $( '#order_custom #the-list' ).wpList( { |
| 1568 | /** |
| 1569 | * Add order id and action to the request. |
| 1570 | */ |
| 1571 | addBefore: function( settings ) { |
| 1572 | settings.data += "&order_id=" + woocommerce_admin_meta_boxes.post_id + "&action=woocommerce_order_add_meta"; |
| 1573 | return settings; |
| 1574 | }, |
| 1575 | |
| 1576 | addAfter: function() { |
| 1577 | $('table#list-table').show(); |
| 1578 | }, |
| 1579 | |
| 1580 | delBefore: function( settings ) { |
| 1581 | settings.data.order_id = woocommerce_admin_meta_boxes.post_id; |
| 1582 | settings.data.action = 'woocommerce_order_delete_meta'; |
| 1583 | return settings; |
| 1584 | } |
| 1585 | }); |
| 1586 | } |
| 1587 | }; |
| 1588 | |
| 1589 | wc_meta_boxes_order.init(); |
| 1590 | wc_meta_boxes_order_items.init(); |
| 1591 | wc_meta_boxes_order_notes.init(); |
| 1592 | wc_meta_boxes_order_downloads.init(); |
| 1593 | wc_meta_boxes_order_custom_meta.init(); |
| 1594 | }); |
| 1595 |