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.
Хуки из функции
Возвращает
null. Ничего (null).
Использование
wc_scheduled_sales();
Список изменений
| С версии 3.0.0 | Введена. |
Код wc_scheduled_sales() wc scheduled sales WC 10.5.2
function wc_scheduled_sales() {
$data_store = WC_Data_Store::load( 'product' );
$product_util = wc_get_container()->get( ProductUtil::class );
$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 );
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( $product ) {
wc_apply_sale_state_for_product( $product, 'start' );
// Note: wc_apply_sale_state_for_product() calls save(), which triggers
// woocommerce_update_product hook, which schedules the end AS event.
}
$product_util->delete_product_specific_transients( $product ? $product : $product_id );
}
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 );
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( $product ) {
wc_apply_sale_state_for_product( $product, 'end' );
}
$product_util->delete_product_specific_transients( $product ? $product : $product_id );
}
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 );
}
}