WC_Form_Handler::cancel_order
Cancel a pending order.
Метод класса: WC_Form_Handler{}
Хуки из метода
Возвращает
null. Ничего (null).
Использование
$result = WC_Form_Handler::cancel_order();
Код WC_Form_Handler::cancel_order() WC Form Handler::cancel order WC 11.2.2
public static function cancel_order() {
if (
isset( $_GET['cancel_order'] ) &&
isset( $_GET['order'] ) &&
isset( $_GET['order_id'] ) &&
( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( wp_unslash( $_GET['_wpnonce'] ), 'woocommerce-cancel_order' ) ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
) {
wc_nocache_headers();
$order_key = is_string( $_GET['order'] ) ? wp_unslash( $_GET['order'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$order_id = absint( $_GET['order_id'] );
if ( doing_action( 'wp_loaded' ) ) {
// Only a wp_loaded dispatch arms the deferred clean redirect. Without an explicit redirect, calls outside wp_loaded return control to the caller.
self::$cancel_order_redirect_pending = true;
}
if ( isset( self::$handled_cancel_order_ids[ $order_id ] ) ) {
// Already handled earlier in this request, for example by a direct call from another callback.
return;
}
$order = wc_get_order( $order_id );
/**
* Filter valid order statuses for cancel.
*
* @since 3.5.0
*
* @param array $valid_statuses Array of valid order statuses for cancel.
* @param WC_Order $order Order object.
*/
$valid_statuses = apply_filters( 'woocommerce_valid_order_statuses_for_cancel', array( OrderStatus::PENDING, OrderStatus::FAILED ), $order );
$user_can_cancel = current_user_can( 'cancel_order', $order_id );
$order_can_cancel = $order->has_status( $valid_statuses );
$redirect = isset( $_GET['redirect'] ) ? wp_unslash( $_GET['redirect'] ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( WC()->session instanceof WC_Session_Handler && ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
if ( $user_can_cancel && $order_can_cancel && $order->get_id() === $order_id && hash_equals( $order->get_order_key(), $order_key ) ) {
// Cancel the order + restore stock.
WC()->session->set( 'order_awaiting_payment', false );
$order->update_status( OrderStatus::CANCELLED, __( 'Order cancelled by customer.', 'woocommerce' ) );
wc_add_notice( apply_filters( 'woocommerce_order_cancelled_notice', __( 'Your order was cancelled.', 'woocommerce' ) ), apply_filters( 'woocommerce_order_cancelled_notice_type', 'notice' ) );
do_action( 'woocommerce_cancelled_order', $order->get_id() );
} elseif ( $user_can_cancel && ! $order_can_cancel ) {
wc_add_notice( __( 'Your order can no longer be cancelled. Please contact us if you need assistance.', 'woocommerce' ), 'error' );
} else {
wc_add_notice( __( 'Invalid order.', 'woocommerce' ), 'error' );
}
if ( $redirect ) {
wp_safe_redirect( $redirect );
exit;
}
self::$handled_cancel_order_ids[ $order_id ] = true;
}
}