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 );

} );

Instructions for Admin order item manual restocking

  1. Log into a staging or locally hosted clone of your site.
  2. Install and activate Code Snippets plugin.
  3. WP Admin > Snippets > Add New
  4. Copy and paste the code from the section above.
  5. Check to ensure formatting came over properly.
  6. Customize the code as desired.
  7. Add a meaningful title.
  8. Select whether to run on front-end or back-end (or both).
  9. Click “Save and Activate”.
  10. Test your site to ensure it works.
  11. Disable if any problems, or recover.
  12. Repeat for live environment.

Need help modifying Admin order item manual restocking?

Contact me. I can help with fitting projects or refer to my partner.

License

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

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.

Disclaimer of warranty

Note: I may receive compensation for referrals.

WP Engine - A smarter way to WordPress
The best email marketing tool, responsive templates, automations, Worldwide support, tracking and reports, Benchmark Email, free plan available
Sell everywhere. Use Shopify to sell in-store and online.
Klaviyo partner badge
Okendo Partner, certified
WooCommerce, the most customizable eCommerce platform for building your online business. Click to get started.
Jetpack, a stronger, customizable site without sacrificing safety. Click to get started.