// 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 );
} );
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.
How to use
PHP Code Snippets
- Log into a staging, development, or locally hosted clone of your site
- Install and activate Code Snippets
- WP Admin > Snippets > Add New
- Copy and paste the code from the Description tab above
- Check to ensure formatting came over properly and no syntax errors show up in the editor
- Customize the code as desired
- Add a meaningful title
- Select whether to run on front-end or back-end (or both)
- Click “Save and Activate”
- Test your site to ensure it works
- Disable if any problems, or recover
- Repeat for live environment
License
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.
Support
- Describe the issue and what you’ve observed.
- Describe your expected outcome(s).
- List steps to reproduce the issue.
- Optionally provide screen-shot or video URLs.
