I have managed to put this together after searching the web and yet, it does not work. My aim is to automatically cancel ALL orders with the status on-hold no matter the payment gateway if the order has not been paid after three days.
The code is obviously incomplete and I'm asking for help making it complete. I was testing it with -1 minute
to see if anything happened. It did not.
function get_unpaid_orders() {
global $wpdb;
$unpaid_orders = $wpdb->get_col( $wpdb->prepare( "
SELECT posts.ID
FROM {$wpdb->posts} AS posts
WHERE posts.post_status = 'wc-on-hold'
AND posts.post_date < %s
", date( 'Y-m-d H:i:s', strtotime('-1 minute') ) ) );
return $unpaid_orders;
}
add_action( 'woocommerce_cancel_unpaid_submitted', 'cancel_unpaid_orders' );
function cancel_unpaid_orders() {
$unpaid_orders = get_unpaid_orders();
if ( $unpaid_orders ) {
foreach ( $unpaid_orders as $unpaid_order ) {
$order = wc_get_order( $unpaid_order );
$cancel_order = true;
foreach ( $order->get_items() as $item_key => $item_values) {
$manage_stock = get_post_meta( $item_values, '_manage_stock', true );
if ( $manage_stock == "yes" ) {
$payment_method = $order->get_payment_method();
if ( $payment_method == "bacs" ) {
$cancel_order = false;
}
}
}
if ( $cancel_order == true ) {
$order -> update_status( 'cancelled', __( 'The order was cancelled due to no payment from customer.', 'woocommerce') );
}
}
}
}
Update 4
Note: in WooCommerce, there is already a function hooked in
woocommerce_cancel_unpaid_orders
action hook that cancel unpaid orders after 7 days.I didn't find
woocommerce_cancel_unpaid_submitted
action hook, so I don't know if it exist and if it is triggered.Now there is some mistakes in your code and you can better use wc_get_orders() which give you directly the correct array of
WC_Order
Objects instead…Here are some different ways to make it (last ones are untested):
1) This last solution is tested and works When shop manager or administrator user roles browse the Admin orders list (only executed once a day):
Code goes in function.php file of your active child theme (or active theme).
2) This third solution is tested and works: The function is triggered when any order change to "processing" or "completed" status (only executed once a day):
Code goes in function.php file of your active child theme (or active theme).
3) So you can try with
woocommerce_cancel_unpaid_submitted
action hook:Code goes in function.php file of your active child theme (or active theme).
The function code should better works. For the hook I really don't know.
4) You can try also with
woocommerce_cancel_unpaid_orders
action hook instead.