meta-boxes-order.js 44 KB

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