Aucune description

meta-boxes-order.js 47KB

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