Ajaxify the order note addition

This commit is contained in:
Caleb Burks 2017-12-09 00:32:35 -06:00
parent 2237c35912
commit 4f187a0840
2 changed files with 70 additions and 18 deletions

View File

@ -1036,8 +1036,34 @@ jQuery( function ( $ ) {
data: data,
type: 'POST',
success: function( response ) {
window.alert( response );
wc_meta_boxes_order_items.unblock();
var response = JSON.parse( response );
if ( ! response.success ) {
window.alert( response.note );
return;
}
var order_note_data = {
action: 'woocommerce_add_order_note',
post_id: woocommerce_admin_meta_boxes.post_id,
note: response.note,
note_type: '',
security: woocommerce_admin_meta_boxes.add_order_note_nonce
};
$( '#woocommerce-order-notes' ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
$.post( woocommerce_admin_meta_boxes.ajax_url, order_note_data, function( response ) {
$( 'ul.order_notes' ).prepend( response );
$( '#woocommerce-order-notes' ).unblock();
});
}
});
},
@ -1072,8 +1098,34 @@ jQuery( function ( $ ) {
data: data,
type: 'POST',
success: function( response ) {
window.alert( response );
wc_meta_boxes_order_items.unblock();
var response = JSON.parse( response );
if ( ! response.success ) {
window.alert( response.note );
return;
}
var order_note_data = {
action: 'woocommerce_add_order_note',
post_id: woocommerce_admin_meta_boxes.post_id,
note: response.note,
note_type: '',
security: woocommerce_admin_meta_boxes.add_order_note_nonce
};
$( '#woocommerce-order-notes' ).block({
message: null,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
$.post( woocommerce_admin_meta_boxes.ajax_url, order_note_data, function( response ) {
$( 'ul.order_notes' ).prepend( response );
$( '#woocommerce-order-notes' ).unblock();
});
}
});
}

View File

@ -1115,19 +1115,19 @@ class WC_AJAX {
}
$_product = $order_item->get_product();
if ( $_product && $_product->exists() && $_product->managing_stock() && isset( $order_item_qty[ $item_id ] ) && $order_item_qty[ $item_id ] > 0 ) {
$stock_change = apply_filters( 'woocommerce_reduce_order_stock_quantity', $order_item_qty[ $item_id ], $item_id );
$new_stock = wc_update_product_stock( $_product, $stock_change, 'decrease' );
$item_name = $_product->get_formatted_name();
$note = sprintf( __( '%1$s stock reduced from %2$s to %3$s.', 'woocommerce' ), $item_name, $new_stock + $stock_change, $new_stock );
$return[] = $note;
$order->add_order_note( $note, 0, true );
$stock_change = apply_filters( 'woocommerce_reduce_order_stock_quantity', $order_item_qty[ $item_id ], $item_id );
$new_stock = wc_update_product_stock( $_product, $stock_change, 'decrease' );
$item_name = $_product->get_formatted_name();
$return['note'] = sprintf( wp_kses_post( __( '%1$s stock reduced from %2$s to %3$s.', 'woocommerce' ) ), $item_name, $new_stock + $stock_change, $new_stock );
$return['success'] = true;
}
}
do_action( 'woocommerce_reduce_order_stock', $order );
if ( empty( $return ) ) {
$return[] = __( 'No products had their stock reduced - they may not have stock management enabled.', 'woocommerce' );
$return['note'] = wp_kses_post( __( 'No products had their stock reduced - they may not have stock management enabled.', 'woocommerce' ) );
$return['success'] = false;
}
echo wp_kses_post( implode( ', ', $return ) );
echo json_encode( $return );
}
wp_die();
}
@ -1155,19 +1155,19 @@ class WC_AJAX {
$_product = $order_item->get_product();
if ( $_product && $_product->exists() && $_product->managing_stock() && isset( $order_item_qty[ $item_id ] ) && $order_item_qty[ $item_id ] > 0 ) {
$old_stock = $_product->get_stock_quantity();
$stock_change = apply_filters( 'woocommerce_restore_order_stock_quantity', $order_item_qty[ $item_id ], $item_id );
$new_quantity = wc_update_product_stock( $_product, $stock_change, 'increase' );
$item_name = $_product->get_formatted_name();
$note = sprintf( __( '%1$s stock increased from %2$s to %3$s.', 'woocommerce' ), $item_name, $old_stock, $new_quantity );
$return[] = $note;
$order->add_order_note( $note, 0, true );
$stock_change = apply_filters( 'woocommerce_restore_order_stock_quantity', $order_item_qty[ $item_id ], $item_id );
$new_quantity = wc_update_product_stock( $_product, $stock_change, 'increase' );
$item_name = $_product->get_formatted_name();
$return['note'] = sprintf( wp_kses_post( __( '%1$s stock increased from %2$s to %3$s.', 'woocommerce' ) ), $item_name, $old_stock, $new_quantity );
$return['success'] = true;
}
}
do_action( 'woocommerce_restore_order_stock', $order );
if ( empty( $return ) ) {
$return[] = __( 'No products had their stock increased - they may not have stock management enabled.', 'woocommerce' );
$return['note'] = wp_kses_post( __( 'No products had their stock increased - they may not have stock management enabled.', 'woocommerce' ) );
$return['success'] = false;
}
echo wp_kses_post( implode( ', ', $return ) );
echo json_encode( $return );
}
wp_die();
}