wc_schedule_product_sale_events()WC 10.5.0

Schedule start/end sale actions for a product based on its sale dates.

Uses Action Scheduler to fire events at the exact sale start/end times, rather than relying on the daily cron.

No Hooks.

Returns

null. Nothing (null).

Usage

wc_schedule_product_sale_events( $product ): void;
$product(WC_Product) (required)
Product object.

Changelog

Since 10.5.0 Introduced.

wc_schedule_product_sale_events() code WC 10.7.0

function wc_schedule_product_sale_events( WC_Product $product ): void {
	$product_id = $product->get_id();
	$date_from  = $product->get_date_on_sale_from( 'edit' );
	$date_to    = $product->get_date_on_sale_to( 'edit' );

	if ( $date_from ) {
		$start_ts = $date_from->getTimestamp();
		if ( $start_ts > time() ) {
			as_schedule_single_action(
				$start_ts,
				'wc_product_start_scheduled_sale',
				array( 'product_id' => $product_id ),
				'woocommerce-sales'
			);
		}
	}

	if ( $date_to ) {
		$end_ts = $date_to->getTimestamp();
		if ( $end_ts > time() ) {
			as_schedule_single_action(
				$end_ts,
				'wc_product_end_scheduled_sale',
				array( 'product_id' => $product_id ),
				'woocommerce-sales'
			);
		}
	}
}