WC_Product_Data_Store_CPT::get_starting_sales │ public │ WC 3.0.0

Returns an array of IDs of products that have sales starting soon.

Sales that have already ended are excluded, otherwise get_ending_sales() would undo them on the same run and the product would qualify again on every run after that. The exclusion must keep reading _sale_price_dates_to exactly the way get_ending_sales() does, or a product ends up in neither queue. See the note there.

Keep the exclusion as NOT EXISTS: the LEFT JOIN ... IS NULL rewrite is faster on MariaDB but materially slower on MySQL.

Method of the class: WC_Product_Data_Store_CPT{}

No Hooks.

Returns

array.

Usage

$WC_Product_Data_Store_CPT = new WC_Product_Data_Store_CPT();
$WC_Product_Data_Store_CPT->get_starting_sales();

Changelog

Since 3.0.0 Introduced.

WC_Product_Data_Store_CPT::get_starting_sales() code WC 11.2.2

public function get_starting_sales() {
	global $wpdb;

	$now = time();

	// phpcs:ignore WordPress.VIP.DirectDatabaseQuery.DirectQuery
	return $wpdb->get_col(
		$wpdb->prepare(
			"SELECT postmeta.post_id FROM {$wpdb->postmeta} as postmeta
			LEFT JOIN {$wpdb->postmeta} as postmeta_2 ON postmeta.post_id = postmeta_2.post_id
			LEFT JOIN {$wpdb->postmeta} as postmeta_3 ON postmeta.post_id = postmeta_3.post_id
			WHERE postmeta.meta_key = '_sale_price_dates_from'
				AND postmeta_2.meta_key = '_price'
				AND postmeta_3.meta_key = '_sale_price'
				AND postmeta.meta_value > 0
				AND postmeta.meta_value < %s
				AND postmeta_2.meta_value != postmeta_3.meta_value
				AND NOT EXISTS (
					SELECT 1 FROM {$wpdb->postmeta} as ended
					WHERE ended.post_id = postmeta.post_id
						AND ended.meta_key = '_sale_price_dates_to'
						AND ended.meta_value > 0
						AND ended.meta_value < %s
				)",
			$now,
			$now
		)
	);
}