Admin order item manual restocking

Custom re-stocking feature on the admin order editor line items area. Useful for selecting which products to re-stock if disabling default whole status based restocking on Completed status.

// Admin Order Items Table Header
add_action( 'woocommerce_admin_order_item_headers', function( $order ) {
	?>
	<th class="manage-column column-cb check-column">
		<input id="cb-select-all-1" type="checkbox">
	</th>
	<?php
} );

// Admin Order Item Checkbox
add_action( 'woocommerce_admin_order_item_values', function( $product, $item, $item_id ) {

	// For Products Only
	if( ! method_exists( $product, 'get_type' ) ) { return; }

	// Get Any Status - Good, Damage
	$status = wc_get_order_item_meta( $item_id, '_rstatus', true );
	if( ! $status ) { $status = 'None'; }

	// Output Checkbox And Status
	?>
	<td class="check-column">
		<input class="order_item_status" type="checkbox"
			name="order_item_status[]" value="<?php echo $item_id; ?>" />
	</td>
	<?php
}, 10, 3 );

// Add Order Items Table Buttons
add_action( 'woocommerce_order_item_add_action_buttons', function( $order ) {
	?>
	<button class="button" type="button" id="order-items-restock">Restock</button>
	<?php
} );

// JavaScript
add_action( 'admin_print_footer_scripts', function() {
	?>
	<script type="text/javascript">

	// Triggers
	jQuery( document ).ready( function( $ ) {
		$( '#order-items-restock' ).click( function() {
			ccom_order_item_status( $, 'Restock' );
		} );
	} );

	// Send Requests
	function ccom_order_item_status( $, status ) {
		var inputs = '';
		$( 'input.order_item_status:checked' ).each( function() {
			var order_item_id = $( this ).val();
			inputs += '<input type="hidden" name="order_item_status['
				+ order_item_id + ']" value="' + status + '" />';
		} );
		var form = $( '<form action="" method="POST">' + inputs + '</form>' );
		$( 'body' ).append( form );
		form.submit();
	}

	</script>
	<?php
} );

// Save Submissions
add_action( 'admin_notices', function() {

	// Submissions Only
	if( empty( $_POST['order_item_status'] ) ) { return; }

	// Loop Order items
	$notes = [];
	foreach( $_POST['order_item_status'] as $order_item_id => $return_status ) {

		// Get Order Item Product
		$product_id = wc_get_order_item_meta( $order_item_id, '_product_id' );
		$product = wc_get_product( $product_id );

		// Process Returns
		switch( $return_status ) {

			// Restocking
			case 'Restock':

				// Get Order Item Quantity
				$order_item = new WC_Order_Item_Product( $order_item_id );
				$order_item_quantity = (int) $order_item->get_quantity();

				// Get Product Stock
				$product_stock = (int) $product->get_stock_quantity();

				// Update Stock
				$new_quantity = $product_stock + $order_item_quantity;
				wc_update_product_stock( $product, $new_quantity );
				$notes[] = sprintf(
					'%s (%s) %d->%d',
					$product->get_title(), $product->get_sku(), $product_stock, $new_quantity
				);
				break;
		}
	}

	// Add Order Note
	$order = wc_get_order( $_GET['post'] );
	$message = 'Order items updated: ' . implode( ', ', $notes );
	$order->add_order_note( $message, false, true );

} );
  1. Log into a staging, development, or locally hosted clone of your site.
  2. Install and activate the Code Snippets plugin.
  3. Navigate to WP Admin > Snippets > Add New.
  4. Copy and paste the code from the Description tab above.
  5. Check to ensure formatting came over properly and no syntax errors show up in the editor.
  6. Customize the code as desired.
  7. Add a meaningful title and description.
  8. Select whether to run on front-end or back-end (or both). Some snippets require both.
  9. Click the "Save and Activate" button.
  10. Test your site to ensure it works as expected.
  11. Disable if any problems, or recover as necessary.
  12. Repeat steps two onward on your live environment.
  1. Describe the issue and what you've observed.
  2. Describe your expected outcome(s).
  3. List steps to reproduce the issue.
  4. Optionally provide screen-shot or video URLs.

We code all of our code snippets directly. Our clients provide most of the ideas and demand for the functionality provided by our code snippets.

There are several sources one can find on Google for code that has inspired or contributed to this open-source library. Here's some main ones:

All code snippets are licensed GPLv2 (or later) matching WordPress licensing.

Disclaimer of warranty:

Except when otherwise stated in writing the copyright holders and/or other parties provide the program as-is without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose.