Last active
January 6, 2025 14:36
-
-
Save younes-dro/7054019b2097b8e6fa3451a4d707500b to your computer and use it in GitHub Desktop.
WooCommerce custom cron job for cleaning draft orders
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 | |
// 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