Hi Guys,
I use inventory in wordpress and I would like know if I can make it auto restock after the order has been cancelled.
For example Customer buy things using BACS method. My Woocommerce website hold stock for 3 hours. After 3hours passed he did not transfer money to me.
So the order has been cancelled by the system then I want to make it restock by itself.
I found the plugin called "WooCommerce Auto Restore Stock" (https://wordpress.org/plugins/woocommerce-auto-restore-stock/).
I tried it but so bad. It is not working on my website. Maybe it out of date...
Anyone know how to do this kine of things please help :)
Thank you
Arnav Joy answers:
write this in functions.php
<?php
add_action( 'woocommerce_order_status_processing_to_cancelled', 'aj_restore_order_stock', 10, 1 );
add_action( 'woocommerce_order_status_completed_to_cancelled', 'aj_restore_order_stock', 10, 1 );
add_action( 'woocommerce_order_status_on-hold_to_cancelled', 'aj_restore_order_stock', 10, 1 );
function aj_restore_order_stock( $order_id ) {
$order = new WC_Order( $order_id );
foreach ( $order->get_items() as $item ) {
if ( $item['product_id'] > 0 ) {
$_product = $order->get_product_from_item( $item );
if ( $_product && $_product->exists() && $_product->managing_stock() ) {
$old_stock = $_product->stock;
$qty = apply_filters( 'woocommerce_order_item_quantity', $item['qty'], $this, $item );
$new_quantity = $_product->increase_stock( $qty );
$order->add_order_note( sprintf( __( 'Item #%s stock incremented from %s to %s.', 'woocommerce' ), $item['product_id'], $old_stock, $new_quantity) );
$order->send_stock_notifications( $_product, $new_quantity, $item['qty'] );
}
}
}
}
?>
akenarong999 comments:
Hi Thank you for your help.
But still not working.....
Maybe these three is not correct...
add_action( 'woocommerce_order_status_processing_to_cancelled', 'aj_restore_order_stock', 10, 1 );
add_action( 'woocommerce_order_status_completed_to_cancelled', 'aj_restore_order_stock', 10, 1 );
add_action( 'woocommerce_order_status_on-hold_to_cancelled', 'aj_restore_order_stock', 10, 1 );
My system need to change from waiting for payment to cancelled. So It may not work cause of this....
Arnav Joy comments:
can you show that part of code which cancel order ?
akenarong999 comments:
It is Woocommerce code and I dont know where it is locate.... :(
akenarong999 comments:
Some Hold-stock function which is include in woocommerce after reach the time that I set. Order will be cancelled
Arnav Joy comments:
try this
<?php
add_action( 'woocommerce_cancel_unpaid_order', 'aj_restore_order_stock', 10, 2 );
function aj_restore_order_stock( $status , $order ) {
//$order = new WC_Order( $order_id );
foreach ( $order->get_items() as $item ) {
if ( $item['product_id'] > 0 ) {
$_product = $order->get_product_from_item( $item );
if ( $_product && $_product->exists() && $_product->managing_stock() ) {
$old_stock = $_product->stock;
$qty = apply_filters( 'woocommerce_order_item_quantity', $item['qty'], $this, $item );
$new_quantity = $_product->increase_stock( $qty );
$order->add_order_note( sprintf( __( 'Item #%s stock incremented from %s to %s.', 'woocommerce' ), $item['product_id'], $old_stock, $new_quantity) );
$order->send_stock_notifications( $_product, $new_quantity, $item['qty'] );
}
}
}
}
?>
akenarong999 comments:
Still cannot. The new function make order not cancel even past the hold-stock time.... :(