Last active
May 14, 2025 05:50
-
-
Save mehrshaddarzi/3c66daaae90dddc057d981fc0977d603 to your computer and use it in GitHub Desktop.
Delete Cancelled WooCommerce Order Before X day Cron Job
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Remove Another Order Pending When create New Order in Checkout | |
// @see https://wp-kama.com/plugin/woocommerce/function/WC_Checkout::create_order | |
// @see https://wp-kama.com/plugin/woocommerce/function/WC_Checkout::process_checkout | |
// https://wp-kama.com/filecode/woocommerce/includes/class-wc-checkout.php#L484 | |
add_action('woocommerce_checkout_create_order', 'checkout_order_created_delete_before_pending_customer_order', 20, 1); | |
function checkout_order_created_delete_before_pending_customer_order($order) | |
{ | |
// Only Admin | |
if (is_admin() and ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) { | |
return null; | |
} | |
if (!is_user_logged_in()) { | |
return null; | |
} | |
$user_id = get_current_user_id(); | |
$order_id = $order->get_id(); | |
// Prepare Param | |
$query = new \WP_Query([ | |
'post_type' => 'shop_order', | |
'post_status' => ['wc-pending'], | |
'post__not_in' => [$order_id], | |
'posts_per_page' => '-1', | |
'order' => 'DESC', | |
'fields' => 'ids', | |
'cache_results' => false, | |
'no_found_rows' => true, | |
'update_post_meta_cache' => false, | |
'update_post_term_cache' => false, | |
'suppress_filters' => true, | |
'meta_query' => array( | |
array( | |
'key' => '_customer_user', | |
'value' => $user_id, | |
'compare' => '=', | |
) | |
) | |
]); | |
foreach ($query->posts as $post_id) { | |
if ($post_id == $order_id) { | |
continue; | |
} | |
// https://rudrastyh.com/woocommerce/delete-order-programmatically.html | |
$obj = wc_get_order($post_id); | |
// First Cancelled For Run Hook | |
$obj->update_status('cancelled'); | |
// Delete | |
//$obj->delete(true); | |
} | |
} | |
// Setup CronJob | |
//add_action('plugins_loaded', 'add_custom_wc_cron_job', 70); | |
function add_custom_wc_cron_job() | |
{ | |
if (!wp_next_scheduled('remove_cancelled_wc_orders')) { | |
wp_schedule_event(time(), 'hourly', 'remove_cancelled_wc_orders'); | |
} | |
} | |
// Delete Cancelled Orders CronJob | |
//add_action('remove_cancelled_wc_orders', 'cronjob_remove_cancelled_wc_orders'); | |
function cronjob_remove_cancelled_wc_orders() | |
{ | |
// auto_remove_cancelled_wc_orders(10); | |
} | |
function auto_remove_cancelled_wc_orders($day) | |
{ | |
$order_ids = wc_get_orders(array( | |
'limit' => 50, | |
'order' => 'ASC', | |
'orderby' => 'ID', | |
'status' => array('wc-cancelled', 'wc-failed'), | |
'return' => 'ids', | |
'date_created' => '<' . (time() - (DAY_IN_SECONDS * $day)), | |
)); | |
foreach ($order_ids as $order_id) { | |
// https://rudrastyh.com/woocommerce/delete-order-programmatically.html | |
$order = wc_get_order( $order_id ); | |
//$order->delete( true ); | |
} | |
return $order_ids; | |
} | |
/* | |
add_action('init', function(){ | |
if(isset($_GET['___deleted_pending_order'])) { | |
$order_ids = wc_get_orders(array( | |
'limit' => 100, | |
'order' => 'ASC', | |
'orderby' => 'ID', | |
'status' => array('wc-failed'), | |
'return' => 'ids', | |
'date_created' => '<' . (time() - (DAY_IN_SECONDS * $day)), | |
)); | |
foreach ($order_ids as $order_id) { | |
// https://rudrastyh.com/woocommerce/delete-order-programmatically.html | |
$order = wc_get_order( $order_id ); | |
$order->delete( true ); | |
echo 'Deleted '.$order_id; | |
echo '<hr>'; | |
} | |
exit; | |
} | |
}); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment