Save order items before adding new one when there are unsaved changes. Closes #17384 (#17402)

This commit is contained in:
Gerhard Potgieter 2017-10-27 13:38:10 +02:00 committed by Mike Jolley
parent f0b240d33a
commit 1e66cedaf0
2 changed files with 14 additions and 0 deletions

View File

@ -1115,6 +1115,11 @@ jQuery( function ( $ ) {
data : $( '#wc-backbone-modal-dialog form' ).serialize()
};
// Check if items have changed, if so pass them through so we can save them before adding a new item.
if ( 'true' === $( 'button.cancel-action' ).attr( 'data-reload' ) ) {
data.items = $( 'table.woocommerce_order_items :input[name], .wc-order-totals-items :input[name]' ).serialize();
}
$.post( woocommerce_admin_meta_boxes.ajax_url, data, function( response ) {
if ( response.success ) {
$( '#woocommerce-order-items' ).find( '.inside' ).empty();

View File

@ -773,11 +773,20 @@ class WC_AJAX {
$order_id = absint( $_POST['order_id'] );
$order = wc_get_order( $order_id );
$items_to_add = wp_parse_id_list( is_array( $_POST['item_to_add'] ) ? $_POST['item_to_add'] : array( $_POST['item_to_add'] ) );
$items = ( ! empty( $_POST['items'] ) ) ? $_POST['items'] : '';
if ( ! $order ) {
throw new Exception( __( 'Invalid order', 'woocommerce' ) );
}
// If we passed through items it means we need to save first before adding a new one.
if ( ! empty( $items ) ) {
$save_items = array();
parse_str( $items, $save_items );
// Save order items.
wc_save_order_items( $order->get_id(), $save_items );
}
foreach ( $items_to_add as $item_to_add ) {
if ( ! in_array( get_post_type( $item_to_add ), array( 'product', 'product_variation' ) ) ) {
continue;