Bulk stock increase and decrease.

This commit is contained in:
Mike Jolley 2012-11-12 12:22:35 +00:00
parent fb2d954da1
commit 877e6eb8c7
4 changed files with 191 additions and 90 deletions

View File

@ -860,74 +860,9 @@ function woocommerce_process_shop_order_meta( $post_id, $post ) {
// Order status
$order->update_status( $_POST['order_status'] );
// Handle button actions
if ( ! empty( $_POST['reduce_stock'] ) && sizeof( $order_items ) > 0 ) {
$order->add_order_note( __( 'Manually reducing stock.', 'woocommerce' ) );
foreach ( $order_items as $order_item ) {
$_product = $order->get_product_from_item( $order_item );
if ( $_product->exists() ) {
if ( $_product->managing_stock() ) {
$old_stock = $_product->stock;
$new_quantity = $_product->reduce_stock( $order_item['qty'] );
$order->add_order_note( sprintf( __( 'Item #%s stock reduced from %s to %s.', 'woocommerce' ), $order_item['id'], $old_stock, $new_quantity) );
$order->send_stock_notifications( $_product, $new_quantity, $order_item['qty'] );
}
} else {
$order->add_order_note( sprintf( __( 'Item %s %s not found, skipping.', 'woocommerce' ), $order_item['id'], $order_item['name'] ) );
}
}
$order->add_order_note( __( 'Manual stock reduction complete.', 'woocommerce' ) );
do_action( 'woocommerce_reduce_order_stock', $order );
} elseif ( ! empty( $_POST['restore_stock'] ) && sizeof( $order_items ) > 0 ) {
$order->add_order_note( __( 'Manually restoring stock.', 'woocommerce' ) );
foreach ( $order_items as $order_item ) {
$_product = $order->get_product_from_item( $order_item );
if ( $_product->exists() ) {
if ( $_product->managing_stock() ) {
$old_stock = $_product->stock;
$new_quantity = $_product->increase_stock( $order_item['qty'] );
$order->add_order_note( sprintf( __( 'Item #%s stock increased from %s to %s.', 'woocommerce' ), $order_item['id'], $old_stock, $new_quantity) );
}
} else {
$order->add_order_note( sprintf( __( 'Item %s %s not found, skipping.', 'woocommerce' ), $order_item['id'], $order_item['name'] ) );
}
}
$order->add_order_note( __( 'Manual stock restore complete.', 'woocommerce' ) );
do_action( 'woocommerce_restore_order_stock', $order );
} elseif ( ! empty( $_POST['order_email'] ) ) {
if ( ! empty( $_POST['order_email'] ) ) {
do_action( 'woocommerce_before_resend_order_emails', $order );

View File

@ -527,6 +527,15 @@ jQuery( function($){
var action = $(this).closest('.bulk_actions').find('select').val();
var selected_rows = $('#woocommerce-order-items').find('.check-column input:checked');
var item_ids = [];
$(selected_rows).each( function() {
var $item = $(this).closest('tr.item, tr.fee');
item_ids.push( $item.attr( 'data-order_item_id' ) );
} );
if ( action == 'delete' ) {
@ -536,36 +545,91 @@ jQuery( function($){
$('table.woocommerce_order_items').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
$(selected_rows).each( function() {
var $item = $(this).closest('tr.item, tr.fee');
var data = {
order_item_id: $item.attr( 'data-order_item_id' ),
action: 'woocommerce_remove_order_item',
security: woocommerce_writepanel_params.order_item_nonce
};
$.ajax( {
url: woocommerce_writepanel_params.ajax_url,
data: data,
type: 'POST',
success: function( response ) {
$item.hide();
$('table.woocommerce_order_items').unblock();
}
} );
var data = {
order_item_ids: item_ids,
action: 'woocommerce_remove_order_item',
security: woocommerce_writepanel_params.order_item_nonce
};
$.ajax( {
url: woocommerce_writepanel_params.ajax_url,
data: data,
type: 'POST',
success: function( response ) {
$item.hide();
$('table.woocommerce_order_items').unblock();
}
} );
}
} else if ( action == 'refund' ) {
// @todo refund handling
} else if ( action == 'reduce_stock' ) {
} else if ( action == 'increase_stock' ) {
$('table.woocommerce_order_items').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
var quantities = {};
$(selected_rows).each( function() {
var $item = $(this).closest('tr.item, tr.fee');
var $qty = $item.find('input.quantity');
quantities[ $item.attr( 'data-order_item_id' ) ] = $qty.val();
} );
var data = {
order_id: woocommerce_writepanel_params.post_id,
order_item_ids: item_ids,
order_item_qty: quantities,
action: 'woocommerce_reduce_order_item_stock',
security: woocommerce_writepanel_params.order_item_nonce
};
$.ajax( {
url: woocommerce_writepanel_params.ajax_url,
data: data,
type: 'POST',
success: function( response ) {
alert( response );
$('table.woocommerce_order_items').unblock();
}
} );
} else if ( action == 'increase_stock' ) {
$('table.woocommerce_order_items').block({ message: null, overlayCSS: { background: '#fff url(' + woocommerce_writepanel_params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
var quantities = {};
$(selected_rows).each( function() {
var $item = $(this).closest('tr.item, tr.fee');
var $qty = $item.find('input.quantity');
quantities[ $item.attr( 'data-order_item_id' ) ] = $qty.val();
} );
var data = {
order_id: woocommerce_writepanel_params.post_id,
order_item_ids: item_ids,
order_item_qty: quantities,
action: 'woocommerce_increase_order_item_stock',
security: woocommerce_writepanel_params.order_item_nonce
};
$.ajax( {
url: woocommerce_writepanel_params.ajax_url,
data: data,
type: 'POST',
success: function( response ) {
alert( response );
$('table.woocommerce_order_items').unblock();
}
} );
}
return false;

File diff suppressed because one or more lines are too long

View File

@ -896,13 +896,115 @@ function woocommerce_ajax_remove_order_item() {
check_ajax_referer( 'order-item', 'security' );
woocommerce_delete_order_item( absint( $_POST['order_item_id'] ) );
$order_item_ids = $_POST['order_item_ids'];
if ( sizeof( $order_item_ids ) > 0 ) {
foreach( $order_item_ids as $id ) {
woocommerce_delete_order_item( absint( $id ) );
}
}
die();
}
add_action( 'wp_ajax_woocommerce_remove_order_item', 'woocommerce_ajax_remove_order_item' );
/**
* woocommerce_ajax_reduce_order_item_stock function.
*
* @access public
* @return void
*/
function woocommerce_ajax_reduce_order_item_stock() {
global $woocommerce, $wpdb;
check_ajax_referer( 'order-item', 'security' );
$order_id = absint( $_POST['order_id'] );
$order_item_ids = $_POST['order_item_ids'];
$order_item_qty = $_POST['order_item_qty'];
$order = new WC_Order( $order_id );
$order_items = $order->get_items();
$return = array();
if ( $order && ! empty( $order_items ) && sizeof( $order_item_ids ) > 0 ) {
foreach ( $order_items as $item_id => $order_item ) {
// Only reduce checked items
if ( ! in_array( $item_id, $order_item_ids ) )
continue;
$_product = $order->get_product_from_item( $order_item );
if ( $_product->exists() && $_product->managing_stock() && isset( $order_item_qty[ $item_id ] ) && $order_item_qty[ $item_id ] > 0 ) {
$old_stock = $_product->stock;
$new_quantity = $_product->reduce_stock( $order_item_qty[ $item_id ] );
$return[] = sprintf( __( 'Item #%s stock reduced from %s to %s.', 'woocommerce' ), $order_item['id'], $old_stock, $new_quantity );
$order->add_order_note( sprintf( __( 'Item #%s stock reduced from %s to %s.', 'woocommerce' ), $order_item['id'], $old_stock, $new_quantity) );
$order->send_stock_notifications( $_product, $new_quantity, $order_item_qty[ $item_id ] );
}
}
do_action( 'woocommerce_reduce_order_stock', $order );
echo implode( ', ', $return );
}
die();
}
add_action( 'wp_ajax_woocommerce_reduce_order_item_stock', 'woocommerce_ajax_reduce_order_item_stock' );
/**
* woocommerce_ajax_increase_order_item_stock function.
*
* @access public
* @return void
*/
function woocommerce_ajax_increase_order_item_stock() {
global $woocommerce, $wpdb;
check_ajax_referer( 'order-item', 'security' );
$order_id = absint( $_POST['order_id'] );
$order_item_ids = $_POST['order_item_ids'];
$order_item_qty = $_POST['order_item_qty'];
$order = new WC_Order( $order_id );
$order_items = $order->get_items();
$return = array();
if ( $order && ! empty( $order_items ) && sizeof( $order_item_ids ) > 0 ) {
foreach ( $order_items as $item_id => $order_item ) {
// Only reduce checked items
if ( ! in_array( $item_id, $order_item_ids ) )
continue;
$_product = $order->get_product_from_item( $order_item );
if ( $_product->exists() && $_product->managing_stock() && isset( $order_item_qty[ $item_id ] ) && $order_item_qty[ $item_id ] > 0 ) {
$old_stock = $_product->stock;
$new_quantity = $_product->increase_stock( $order_item_qty[ $item_id ] );
$return[] = sprintf( __( 'Item #%s stock increased from %s to %s.', 'woocommerce' ), $order_item['id'], $old_stock, $new_quantity );
$order->add_order_note( sprintf( __( 'Item #%s stock increased from %s to %s.', 'woocommerce' ), $order_item['id'], $old_stock, $new_quantity ) );
}
}
do_action( 'woocommerce_restore_order_stock', $order );
echo implode( ', ', $return );
}
die();
}
add_action( 'wp_ajax_woocommerce_increase_order_item_stock', 'woocommerce_ajax_increase_order_item_stock' );
/**
* woocommerce_ajax_add_order_item_meta function.