WC_Order::payment_complete()publicWC 1.0

When a payment is complete this function is called.

Most of the time this should mark an order as 'processing' so that admin can process/post the items. If the cart contains only downloadable items then the order is 'completed' since the admin needs to take no action. Stock levels are reduced at this point. Sales are also recorded for products. Finally, record the date of payment.

Метод класса: WC_Order{}

Возвращает

true|false. success

Использование

$WC_Order = new WC_Order();
$WC_Order->payment_complete( $transaction_id );
$transaction_id(строка)
Optional transaction id to store in post meta.
По умолчанию: ''

Код WC_Order::payment_complete() WC 8.7.0

public function payment_complete( $transaction_id = '' ) {
	if ( ! $this->get_id() ) { // Order must exist.
		return false;
	}

	try {
		do_action( 'woocommerce_pre_payment_complete', $this->get_id(), $transaction_id );

		if ( WC()->session ) {
			WC()->session->set( 'order_awaiting_payment', false );
		}

		if ( $this->has_status( apply_filters( 'woocommerce_valid_order_statuses_for_payment_complete', array( 'on-hold', 'pending', 'failed', 'cancelled' ), $this ) ) ) {
			if ( ! empty( $transaction_id ) ) {
				$this->set_transaction_id( $transaction_id );
			}
			if ( ! $this->get_date_paid( 'edit' ) ) {
				$this->set_date_paid( time() );
			}
			$this->set_status( apply_filters( 'woocommerce_payment_complete_order_status', $this->needs_processing() ? 'processing' : 'completed', $this->get_id(), $this ) );
			$this->save();

			do_action( 'woocommerce_payment_complete', $this->get_id(), $transaction_id );
		} else {
			do_action( 'woocommerce_payment_complete_order_status_' . $this->get_status(), $this->get_id(), $transaction_id );
		}
	} catch ( Exception $e ) {
		/**
		 * If there was an error completing the payment, log to a file and add an order note so the admin can take action.
		 */
		$logger = wc_get_logger();
		$logger->error(
			sprintf(
				'Error completing payment for order #%d',
				$this->get_id()
			),
			array(
				'order' => $this,
				'error' => $e,
			)
		);
		$this->add_order_note( __( 'Payment complete event failed.', 'woocommerce' ) . ' ' . $e->getMessage() );
		return false;
	}
	return true;
}