Skip to content

Instantly share code, notes, and snippets.

@younes-dro
Last active January 6, 2025 14:36
Show Gist options
  • Save younes-dro/7054019b2097b8e6fa3451a4d707500b to your computer and use it in GitHub Desktop.
Save younes-dro/7054019b2097b8e6fa3451a4d707500b to your computer and use it in GitHub Desktop.
WooCommerce custom cron job for cleaning draft orders
<?php
// 1. Deactivate the default WooCommerce cleanup draft orders cron
add_action( 'init', 'deactivate_woocommerce_cleanup_draft_orders' );
function deactivate_woocommerce_cleanup_draft_orders() {
if ( function_exists( 'as_next_scheduled_action' ) ) {
$timestamp = as_next_scheduled_action( 'woocommerce_cleanup_draft_orders' );
if ( $timestamp ) {
as_unschedule_action( 'woocommerce_cleanup_draft_orders' );
}
}
}
// 2. Schedule a custom cron job for cleaning draft orders every 3 days
add_action( 'init', 'schedule_custom_cleanup_draft_orders' );
function schedule_custom_cleanup_draft_orders() {
if ( function_exists( 'as_next_scheduled_action' ) ) {
$timestamp = as_next_scheduled_action( 'woocommerce_cleanup_draft_orders' );
if ( ! $timestamp ) {
as_schedule_recurring_action( strtotime( 'midnight tonight' ), 3 * DAY_IN_SECONDS, 'woocommerce_cleanup_draft_orders' );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment