WC_Order::payment_complete
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.
Method of the class: WC_Order{}
Hooks from the method
Returns
true|false. success
Usage
$WC_Order = new WC_Order(); $WC_Order->payment_complete( $transaction_id );
- $transaction_id(string)
- Optional transaction id to store in post meta.
Default:''
WC_Order::payment_complete() WC Order::payment complete code WC 10.5.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 );
}
/**
* Filters the valid order statuses for payment complete.
*
* @param array $valid_completed_statuses Array of valid order statuses for payment complete.
* @param WC_Order $this Order object.
* @since 2.7.0
*/
$valid_completed_statuses = apply_filters( 'woocommerce_valid_order_statuses_for_payment_complete', OrderStatus::PAYMENT_COMPLETE_STATUSES, $this );
if ( $this->has_status( $valid_completed_statuses ) ) {
if ( ! empty( $transaction_id ) ) {
$this->set_transaction_id( $transaction_id );
}
if ( ! $this->get_date_paid( 'edit' ) ) {
$this->set_date_paid( time() );
}
/**
* Filters the order status to set after payment complete.
*
* @param string $status Order status.
* @param int $order_id Order ID.
* @param WC_Order $this Order object.
* @since 2.7.0
*/
$next_status = apply_filters( 'woocommerce_payment_complete_order_status', $this->needs_processing() ? OrderStatus::PROCESSING : OrderStatus::COMPLETED, $this->get_id(), $this );
$transaction_id = $this->get_transaction_id();
$payment_method = $this->get_payment_method_title();
// translators: %1$s is the payment method, %2$s is the transaction id.
$payment_complete_note = $payment_method && $transaction_id ? sprintf( __( 'Payment via %1$s (%2$s).', 'woocommerce' ), $payment_method, $transaction_id ) : __( 'Payment complete.', 'woocommerce' );
$this->set_status( $next_status, false );
$this->add_order_note( $payment_complete_note, false, false, array( 'note_group' => OrderNoteGroup::PAYMENT ) );
$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(), false, false, array( 'note_group' => OrderNoteGroup::ERROR ) );
return false;
}
return true;
}