wc_handle_product_start_scheduled_sale()WC 10.5.0

Handle scheduled sale start for a product.

This is the Action Scheduler callback that fires at the exact sale start time.

No Hooks.

Returns

null. Nothing (null).

Usage

wc_handle_product_start_scheduled_sale( $product_id ): void;
$product_id(int) (required)
Product ID.

Changelog

Since 10.5.0 Introduced.

wc_handle_product_start_scheduled_sale() code WC 10.5.0

function wc_handle_product_start_scheduled_sale( $product_id ): void {
	$product = wc_get_product( $product_id );
	if ( ! $product ) {
		return;
	}

	// Skip product types with derived prices.
	if ( $product->is_type( array( 'variable', 'grouped' ) ) ) {
		return;
	}

	// Verify sale should still start (dates/price might have changed since scheduling).
	if ( ! $product->get_sale_price( 'edit' ) ) {
		return;
	}

	$now       = time();
	$date_from = $product->get_date_on_sale_from( 'edit' );
	$date_to   = $product->get_date_on_sale_to( 'edit' );

	if ( $date_from && $date_from->getTimestamp() > $now ) {
		return;
	}

	if ( $date_to && $date_to->getTimestamp() < $now ) {
		return;
	}

	if ( (float) $product->get_price( 'edit' ) === (float) $product->get_sale_price( 'edit' ) ) {
		return;
	}

	wc_apply_sale_state_for_product( $product, 'start' );
}