wc_maybe_schedule_product_sale_events()WC 10.5.0

Schedule sale events when a product is saved with sale dates.

No Hooks.

Returns

null. Nothing (null).

Usage

wc_maybe_schedule_product_sale_events( $product_id, $product ): void;
$product_id(int) (required)
Product ID.
$product(WC_Product|null)
Product object (optional).
Default: null

Changelog

Since 10.5.0 Introduced.

wc_maybe_schedule_product_sale_events() code WC 10.7.0

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

	$product_id = $product->get_id();

	// Always clear existing events first.
	as_unschedule_all_actions( 'wc_product_start_scheduled_sale', array( 'product_id' => $product_id ), 'woocommerce-sales' );
	as_unschedule_all_actions( 'wc_product_end_scheduled_sale', array( 'product_id' => $product_id ), 'woocommerce-sales' );

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

	if ( $date_from || $date_to ) {
		wc_schedule_product_sale_events( $product );
	}
}