wc_scheduled_sales()
Function which handles the start and end of scheduled sales via cron.
Previously, this daily cron was the only mechanism for starting/ending scheduled sales, which caused timing issues - sales could be "a day off" depending on when WP-Cron ran. Now, per-product Action Scheduler events fire at exact sale times.
This function now acts as a safety net to:
- Catch any products missed by the per-product Action Scheduler events
- Handle products created before the AS events were introduced
This function is kept for backwards compatibility. Extenders may hook into the woocommerce_scheduled_sales cron event or the before/after hooks fired within.
Note: The before/after hooks (wc_before_products_starting_sales, etc.) only fire when this cron finds products to process. If per-product AS events handled sales on time, these hooks may not fire.
Products are processed in batches by ScheduledSaleRun. Before hooks run before any batch is primed; after hooks run after the last batch's caches are cleared.
Hooks from the function
Returns
null. Nothing (null).
Usage
wc_scheduled_sales();
Changelog
| Since 3.0.0 | Introduced. |
wc_scheduled_sales() wc scheduled sales code WC 11.3.0
function wc_scheduled_sales() {
$data_store = WC_Data_Store::load( 'product' );
$must_refresh_transient = false;
// Sales which are due to start.
$product_ids = $data_store->get_starting_sales();
if ( $product_ids ) {
$must_refresh_transient = true;
do_action( 'wc_before_products_starting_sales', $product_ids );
( new ScheduledSaleRun( $product_ids, ScheduledSaleRun::MODE_START ) )->process();
do_action( 'wc_after_products_starting_sales', $product_ids );
delete_transient( 'wc_products_onsale' );
}
// Sales which are due to end.
$product_ids = $data_store->get_ending_sales();
if ( $product_ids ) {
$must_refresh_transient = true;
do_action( 'wc_before_products_ending_sales', $product_ids );
( new ScheduledSaleRun( $product_ids, ScheduledSaleRun::MODE_END ) )->process();
do_action( 'wc_after_products_ending_sales', $product_ids );
delete_transient( 'wc_products_onsale' );
}
if ( $must_refresh_transient ) {
// Kept for compatibility, WooCommerce core doesn't use product transient versions anymore.
WC_Cache_Helper::get_transient_version( 'product', true );
}
}