Automattic\WooCommerce\Internal\OrderReviews

Scheduler::handle_status_changedpublicWC 1.0

Unschedule the pending review-request email whenever the order leaves the eligible state. woocommerce_order_status_changed for every transition, so a single listener covers cancelled / refunded / processing / on-hold / pending / failed / custom statuses in one place.

Eligibility is read from the same woocommerce_review_order_eligible_statuses the trigger uses, so a site that widens the filter (e.g. to also accept processing) keeps the email queued through transitions inside its expanded eligible set.

Method of the class: Scheduler{}

Returns

null. Nothing (null).

Usage

$Scheduler = new Scheduler();
$Scheduler->handle_status_changed( $order_id, $old_status, $new_status ): void;
$order_id(int) (required)
Order ID.
$old_status(string) (required)
Previous status (sans wc- prefix).
$new_status(string) (required)
New status (sans wc- prefix).

Scheduler::handle_status_changed() code WC 10.8.1

public function handle_status_changed( int $order_id, string $old_status, string $new_status ): void {
	$order = wc_get_order( $order_id );

	/**
	 * Filter the order statuses that are eligible to receive the review-request email.
	 *
	 * Same hook the email's `trigger()` consults at send time; documented on
	 * `WC_Email_Customer_Review_Request::is_order_eligible_for_send()`.
	 *
	 * @since 10.8.0
	 *
	 * @param string[]      $eligible_statuses Default: `[ 'completed' ]`.
	 * @param WC_Order|null $order             Order being inspected, or null if it could not be loaded.
	 */
	$eligible_statuses = (array) apply_filters(
		'woocommerce_review_order_eligible_statuses',
		array( OrderStatus::COMPLETED ),
		$order instanceof WC_Order ? $order : null
	);

	$was_eligible = in_array( $old_status, $eligible_statuses, true );
	$is_eligible  = in_array( $new_status, $eligible_statuses, true );

	if ( ! $was_eligible || $is_eligible ) {
		return;
	}

	$this->handle_cancellation( $order_id );
}