Your cart is currently empty!
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
- Log into a staging or locally hosted clone of your site.
- Install and activate Code Snippets plugin.
- WP Admin > Snippets > Add New
- Copy and paste the code from the section above.
- Check to ensure formatting came over properly.
- 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.
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