Payouts::order_status_changed( int $order_id, string $status_from, string $status_to, WC_Order $order )

Updates payout status on order status change.


Parameters #

$order_id

(int) (Required) Order ID.

$status_from

(string) (Required) Status being changed from.

$status_to

(string) (Required) Status being changed to.

$order

(WC_Order) (Required) Order object.


Top ↑

Source #

File: src/Tribe/Payouts.php

	public function order_status_changed( $order_id, $status_from, $status_to, $order ) {

		$repository = tribe_payouts();

		try {
			$repository->by( 'order', $order_id );
			$repository->by( 'status', [ Payouts::STATUS_PENDING_ORDER, Payouts::STATUS_PENDING ] );

			$payouts = $repository->all();
		} catch ( \Tribe__Repository__Usage_Error $exception ) {
			// There was an error, skip processing.
			return;
		}

		if ( empty( $payouts ) ) {
			return;
		}

		$new_status        = null;
		$new_date_paid     = null;
		$new_error_message = null;

		$gateway = get_post_meta( $order_id, '_tribe_split_payment_method', true );

		if ( empty( $gateway ) ) {
			return;
		}

		// No custom handling for Adaptive Payments unless we are marking an order pending payment as paid.
		if ( Payouts\PayPal\Adaptive_Payments::GATEWAY === $gateway && 'pending' === $status_from && 'processing' !== $status_to ) {
			return;
		}

		switch ( $status_to ) {
			case 'pending':
			case 'processing':
				// Waiting for order to be paid/complete before queueing to be paid.
				$new_status = Payouts::STATUS_PENDING_ORDER;

				// Handle Adaptive Payments, they are marked as paid as soon as they receive payment.
				if ( Payouts\PayPal\Adaptive_Payments::GATEWAY === $gateway ) {
					$new_status    = Payouts::STATUS_PAID;
					$new_date_paid = current_time( 'mysql' );
				}

				break;
			case 'completed':
				// Order is complete so let's queue the payouts to be paid.
				$new_status = Payouts::STATUS_PENDING;

				break;
			case 'cancelled':
			case 'refunded':
			case 'failed':
				// Order is no longer valid, cancel the payouts as failed.
				$new_status        = Payouts::STATUS_FAILED;
				$new_error_message = sprintf(
					__( 'Order status changed from %1$s to %2$s', 'tribe-events-community-tickets' ),
					$status_from,
					$status_to
				);

				break;
		}

		$changes = [
			'new_status'        => $new_status,
			'new_date_paid'     => $new_date_paid,
			'new_error_message' => $new_error_message,
		];

		/**
		 * Filter the changes to make for payouts on an order.
		 *
		 * @since 4.7.0
		 *
		 * @param array     $changes     List of changes to make.
		 * @param int       $order_id    Order ID.
		 * @param string    $status_from Status being changed from.
		 * @param string    $status_to   Status being changed to.
		 * @param \WC_Order $order       Order object.
		 */
		$changes = apply_filters( 'tribe_community_tickets_payouts_record_processor_interval', $changes, $order_id, $status_from, $status_to, $order );

		if ( empty( $changes['new_status'] ) ) {
			return;
		}

		try {
			// Set the status for all payouts.
			$repository->set( 'post_status', $changes['new_status'] );

			if ( $changes['new_date_paid'] ) {
				$repository->set( '_tribe_date_paid', esc_html( $changes['new_date_paid'] ) );
			}

			if ( $changes['new_error_message'] ) {
				$repository->set( '_tribe_error_message', esc_html( $changes['new_error_message'] ) );
			}

			// Save all payouts.
			$repository->save();
		} catch ( \Tribe__Repository__Usage_Error $exception ) {
			// There was an error, skip processing.
			return;
		}
	}

Top ↑

Changelog #

Changelog
Version Description
4.7.0 Introduced.