Added mechanism to customize data before AJAX requests in `meta-boxes-order.js` (#33563)

Added `triggerHandler` before each ajax request.

* Added namespace to event name
* Linting.

Co-authored-by: Luigi Pulcini <luigi@barn2.com>
Co-authored-by: Barry Hughes <3594411+barryhughes@users.noreply.github.com>
This commit is contained in:
Luigi Pulcini 2022-07-05 02:09:13 +02:00 committed by GitHub
parent bddb6621ee
commit 63dd5eda6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 8 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Added a mechanism to customize the data being sent via AJAX by the order meta box

View File

@ -333,6 +333,20 @@ jQuery( function ( $ ) {
$( '#woocommerce-order-items' ).unblock();
},
filter_data: function( handle, data ) {
const filteredData = $( '#woocommerce-order-items' )
.triggerHandler(
`woocommerce_order_meta_box_${handle}_ajax_data`,
[ data ]
);
if ( filteredData ) {
return filteredData;
}
return data;
},
reload_items: function() {
var data = {
order_id: woocommerce_admin_meta_boxes.post_id,
@ -340,6 +354,8 @@ jQuery( function ( $ ) {
security: woocommerce_admin_meta_boxes.order_item_nonce
};
data = wc_meta_boxes_order_items.filter_data( 'reload_items', data );
wc_meta_boxes_order_items.block();
$.ajax({
@ -462,6 +478,8 @@ jQuery( function ( $ ) {
user_email : user_email
} );
data = wc_meta_boxes_order_items.filter_data( 'add_coupon', data );
$.ajax( {
url: woocommerce_admin_meta_boxes.ajax_url,
data: data,
@ -500,6 +518,8 @@ jQuery( function ( $ ) {
coupon : $this.data( 'code' )
} );
data = wc_meta_boxes_order_items.filter_data( 'remove_coupon', data );
$.post( woocommerce_admin_meta_boxes.ajax_url, data, function( response ) {
if ( response.success ) {
$( '#woocommerce-order-items' ).find( '.inside' ).empty();
@ -587,6 +607,8 @@ jQuery( function ( $ ) {
amount : value
} );
data = wc_meta_boxes_order_items.filter_data( 'add_fee', data );
$.post( woocommerce_admin_meta_boxes.ajax_url, data, function( response ) {
if ( response.success ) {
$( '#woocommerce-order-items' ).find( '.inside' ).empty();
@ -594,7 +616,7 @@ jQuery( function ( $ ) {
wc_meta_boxes_order_items.reloaded_items();
wc_meta_boxes_order_items.unblock();
window.wcTracks.recordEvent( 'order_edit_added_fee', {
order_id: data.post_id,
order_id: woocommerce_admin_meta_boxes.post_id,
status: $( '#order_status' ).val()
} );
} else {
@ -616,11 +638,13 @@ jQuery( function ( $ ) {
dataType : 'json'
};
data = wc_meta_boxes_order_items.filter_data( 'add_shipping', data );
$.post( woocommerce_admin_meta_boxes.ajax_url, data, function( response ) {
if ( response.success ) {
$( 'table.woocommerce_order_items tbody#order_shipping_line_items' ).append( response.data.html );
window.wcTracks.recordEvent( 'order_edit_add_shipping', {
order_id: data.post_id,
order_id: woocommerce_admin_meta_boxes.post_id,
status: $( '#order_status' ).val()
} );
} else {
@ -683,6 +707,8 @@ jQuery( function ( $ ) {
data.items = $( 'table.woocommerce_order_items :input[name], .wc-order-totals-items :input[name]' ).serialize();
}
data = wc_meta_boxes_order_items.filter_data( 'delete_item', data );
$.ajax({
url: woocommerce_admin_meta_boxes.ajax_url,
data: data,
@ -707,7 +733,7 @@ jQuery( function ( $ ) {
},
complete: function() {
window.wcTracks.recordEvent( 'order_edit_remove_item', {
order_id: data.post_id,
order_id: woocommerce_admin_meta_boxes.post_id,
status: $( '#order_status' ).val()
} );
}
@ -727,6 +753,8 @@ jQuery( function ( $ ) {
security: woocommerce_admin_meta_boxes.order_item_nonce
};
data = wc_meta_boxes_order_items.filter_data( 'delete_tax', data );
$.ajax({
url: woocommerce_admin_meta_boxes.ajax_url,
data: data,
@ -797,6 +825,8 @@ jQuery( function ( $ ) {
security: woocommerce_admin_meta_boxes.calc_totals_nonce
} );
data = wc_meta_boxes_order_items.filter_data( 'recalculate', data );
$( document.body ).trigger( 'order-totals-recalculate-before', data );
$.ajax({
@ -815,7 +845,7 @@ jQuery( function ( $ ) {
$( document.body ).trigger( 'order-totals-recalculate-complete', response );
window.wcTracks.recordEvent( 'order_edit_recalc_totals', {
order_id: data.post_id,
order_id: woocommerce_admin_meta_boxes.post_id,
ok_cancel: 'OK',
status: $( '#order_status' ).val()
} );
@ -840,6 +870,8 @@ jQuery( function ( $ ) {
security: woocommerce_admin_meta_boxes.order_item_nonce
};
data = wc_meta_boxes_order_items.filter_data( 'save_line_items', data );
wc_meta_boxes_order_items.block();
$.ajax({
@ -866,7 +898,7 @@ jQuery( function ( $ ) {
},
complete: function() {
window.wcTracks.recordEvent( 'order_edit_save_line_items', {
order_id: data.post_id,
order_id: woocommerce_admin_meta_boxes.post_id,
status: $( '#order_status' ).val()
} );
}
@ -938,6 +970,8 @@ jQuery( function ( $ ) {
security : woocommerce_admin_meta_boxes.order_item_nonce
};
data = wc_meta_boxes_order_items.filter_data( 'do_refund', data );
$.ajax( {
url: woocommerce_admin_meta_boxes.ajax_url,
data: data,
@ -980,6 +1014,8 @@ jQuery( function ( $ ) {
security: woocommerce_admin_meta_boxes.order_item_nonce
};
data = wc_meta_boxes_order_items.filter_data( 'delete_refund', data );
$.ajax({
url: woocommerce_admin_meta_boxes.ajax_url,
data: data,
@ -1192,6 +1228,8 @@ jQuery( function ( $ ) {
data.items = $( 'table.woocommerce_order_items :input[name], .wc-order-totals-items :input[name]' ).serialize();
}
data = wc_meta_boxes_order_items.filter_data( 'add_items', data );
$.ajax({
type: 'POST',
url: woocommerce_admin_meta_boxes.ajax_url,
@ -1216,7 +1254,7 @@ jQuery( function ( $ ) {
},
complete: function() {
window.wcTracks.recordEvent( 'order_edit_add_products', {
order_id: data.post_id,
order_id: woocommerce_admin_meta_boxes.post_id,
status: $( '#order_status' ).val()
} );
},
@ -1248,6 +1286,8 @@ jQuery( function ( $ ) {
security: woocommerce_admin_meta_boxes.order_item_nonce
};
data = wc_meta_boxes_order_items.filter_data( 'add_tax', data );
$.ajax({
url : woocommerce_admin_meta_boxes.ajax_url,
data : data,
@ -1265,7 +1305,7 @@ jQuery( function ( $ ) {
},
complete: function() {
window.wcTracks.recordEvent( 'order_edit_add_tax', {
order_id: data.post_id,
order_id: woocommerce_admin_meta_boxes.post_id,
status: $( '#order_status' ).val()
} );
}
@ -1330,7 +1370,7 @@ jQuery( function ( $ ) {
$( '#woocommerce-order-notes' ).unblock();
$( '#add_order_note' ).val( '' );
window.wcTracks.recordEvent( 'order_edit_add_order_note', {
order_id: data.post_id,
order_id: woocommerce_admin_meta_boxes.post_id,
note_type: data.note_type || 'private',
status: $( '#order_status' ).val()
} );