I have online store in WooCommerce and there is an option that is: if x time duration reached then change the status of that order to canceled and also set the stock where it was before order submitted.
I want to change it if x time duration reached then order status change to complete and also item was reduce from the stock.
as far as I know that is the code of reduce stock and cancel the order.
/**
* woocommerce_cancel_unpaid_orders function.
*
* @access public
* @return void
*/
function woocommerce_cancel_unpaid_orders() {
global $wpdb;
$held_duration = get_option( 'woocommerce_hold_stock_minutes' );
if ( $held_duration < 1 || get_option( 'woocommerce_manage_stock' ) != 'yes' )
return;
$date = date( "Y-m-d H:i:s", strtotime( '-' . absint( $held_duration ) . ' MINUTES', current_time( 'timestamp' ) ) );
$unpaid_orders = $wpdb->get_col( $wpdb->prepare( "
SELECT posts.ID
FROM {$wpdb->posts} AS posts
LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID=rel.object_ID
LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
LEFT JOIN {$wpdb->terms} AS term USING( term_id )
WHERE posts.post_type = 'shop_order'
AND posts.post_status = 'publish'
AND tax.taxonomy = 'shop_order_status'
AND term.slug IN ('pending')
AND posts.post_modified < %s
", $date ) );
if ( $unpaid_orders ) {
foreach ( $unpaid_orders as $unpaid_order ) {
$order = new WC_Order( $unpaid_order );
if ( apply_filters( 'woocommerce_cancel_unpaid_order', true, $order ) )
$order->update_status( 'cancelled', __( 'Unpaid order cancelled - time limit reached.', 'woocommerce' ) );
}
}
wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' );
wp_schedule_single_event( time() + ( absint( $held_duration ) * 60 ), 'woocommerce_cancel_unpaid_orders' );
}
add_action( 'woocommerce_cancel_unpaid_orders', 'woocommerce_cancel_unpaid_orders' );
Personally I did this changes, do they make sense?
/**
* woocommerce_cancel_unpaid_orders function.
*
* @access public
* @return void
*/
function woocommerce_cancel_unpaid_orders() {
global $wpdb;
$held_duration = get_option( 'woocommerce_hold_stock_minutes' );
if ( $held_duration < 1 || get_option( 'woocommerce_manage_stock' ) != 'yes' )
return;
$date = date( "Y-m-d H:i:s", strtotime( '-' . absint( $held_duration ) . ' MINUTES', current_time( 'timestamp' ) ) );
$unpaid_orders = $wpdb->get_col( $wpdb->prepare( "
SELECT posts.ID
FROM {$wpdb->posts} AS posts
LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID=rel.object_ID
LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
LEFT JOIN {$wpdb->terms} AS term USING( term_id )
WHERE posts.post_type = 'shop_order'
AND posts.post_status = 'publish'
AND tax.taxonomy = 'shop_order_status'
AND term.slug IN ('pending')
AND posts.post_modified < %s
", $date ) );
if ( $unpaid_orders ) {
foreach ( $unpaid_orders as $unpaid_order ) {
$order = new WC_Order( $unpaid_order );
if ( apply_filters( 'woocommerce_payment_complete_order_status', true, $order ) )
$order->update_status( 'completed', __( 'order completed - time limit reached.', 'woocommerce' ) );
}
}
wp_clear_scheduled_hook( 'woocommerce_payment_complete_order_status' );
wp_schedule_single_event( time() + ( absint( $held_duration ) * 60 ), 'woocommerce_payment_complete_order_status' );
}
add_action( 'woocommerce_payment_complete_order_status', 'woocommerce_payment_complete_order_status' );
I was replace this filter ‘woocommerce_cancel_unpaid_order’ with this woocommerce_payment_complete_order_status
and chnaged this
$order->update_status( ‘cancelled’, __( ‘Unpaid order cancelled – time limit reached.’, ‘woocommerce’ ) );
with this one
$order->update_status( ‘completed’, __( ‘order completed – time limit reached.’, ‘woocommerce’ ) );
not tested till yet bcz i want confirmation before testing it.