PHP Warning: Undefined array key “order-pay”
-
today i got this error in error_log
[31-Oct-2024 14:22:27 UTC] PHP Warning: Undefined array key “order-pay” in public_html/wp-content/plugins/woocommerce/includes/class-wc-form-handler.php on line 412
and then a customer buyed an product with out payingafter that i replaced the
public static function pay_action() {
global $wp;if ( isset( $_POST['woocommerce_pay'], $_GET['key'] ) ) { wc_nocache_headers(); $nonce_value = wc_get_var( $_REQUEST['woocommerce-pay-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // @codingStandardsIgnoreLine. if ( ! wp_verify_nonce( $nonce_value, 'woocommerce-pay' ) ) { return; } ob_start(); // Pay for existing order. $order_key = wp_unslash( $_GET['key'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $order_id = absint( $wp->query_vars['order-pay'] ); $order = wc_get_order( $order_id ); if ( $order_id === $order->get_id() && hash_equals( $order->get_order_key(), $order_key ) && $order->needs_payment() ) { do_action( 'woocommerce_before_pay_action', $order ); WC()->customer->set_props( array( 'billing_country' => $order->get_billing_country() ? $order->get_billing_country() : null, 'billing_state' => $order->get_billing_state() ? $order->get_billing_state() : null, 'billing_postcode' => $order->get_billing_postcode() ? $order->get_billing_postcode() : null, 'billing_city' => $order->get_billing_city() ? $order->get_billing_city() : null, ) ); WC()->customer->save(); if ( ! empty( $_POST['terms-field'] ) && empty( $_POST['terms'] ) ) { wc_add_notice( __( 'Please read and accept the terms and conditions to proceed with your order.', 'woocommerce' ), 'error' ); return; } // Update payment method. if ( $order->needs_payment() ) { try { $payment_method_id = isset( $_POST['payment_method'] ) ? wc_clean( wp_unslash( $_POST['payment_method'] ) ) : false; if ( ! $payment_method_id ) { throw new Exception( __( 'Invalid payment method.', 'woocommerce' ) ); } $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); $payment_method = isset( $available_gateways[ $payment_method_id ] ) ? $available_gateways[ $payment_method_id ] : false; if ( ! $payment_method ) { throw new Exception( __( 'Invalid payment method.', 'woocommerce' ) ); } $order->set_payment_method( $payment_method ); $order->save(); $payment_method->validate_fields(); if ( 0 === wc_notice_count( 'error' ) ) { $result = $payment_method->process_payment( $order_id ); // Redirect to success/confirmation/payment page. if ( isset( $result['result'] ) && 'success' === $result['result'] ) { $result['order_id'] = $order_id; $result = apply_filters( 'woocommerce_payment_successful_result', $result, $order_id ); wp_redirect( $result['redirect'] ); //phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect exit; } } } catch ( Exception $e ) { wc_add_notice( $e->getMessage(), 'error' ); } } else { // No payment was required for order. $order->payment_complete(); wp_safe_redirect( $order->get_checkout_order_received_url() ); exit; } do_action( 'woocommerce_after_pay_action', $order ); } } }to this :
public static function pay_action() {
global $wp;
if ( isset( $_POST[‘woocommerce_pay’], $_GET[‘key’] ) ) {
wc_nocache_headers();
$nonce_value = wc_get_var( $_REQUEST[‘woocommerce-pay-nonce’], wc_get_var( $_REQUEST[‘_wpnonce’], ” ) ); // @codingStandardsIgnoreLine.
if ( ! wp_verify_nonce( $nonce_value, ‘woocommerce-pay’ ) ) {
return;
}
ob_start();
// Pay for existing order.
$order_key = wp_unslash( $_GET[‘key’] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitizedif ( isset( $wp->query_vars['order-pay'] ) ) {
$order_id = absint( $wp->query_vars['order-pay'] );
$order = wc_get_order( $order_id );
if ( $order_id === $order->get_id() && hash_equals( $order->get_order_key(), $order_key ) && $order->needs_payment() ) {
do_action( 'woocommerce_before_pay_action', $order );
WC()->customer->set_props(array(
'billing_country' => $order->get_billing_country() ? $order->get_billing_country() : null,
'billing_state' => $order->get_billing_state() ? $order->get_billing_state() : null,
'billing_postcode' => $order->get_billing_postcode() ? $order->get_billing_postcode() : null,
'billing_city' => $order->get_billing_city() ? $order->get_billing_city() : null,
));
WC()->customer->save();
if ( ! empty( $_POST['terms-field'] ) && empty( $_POST['terms'] ) ) {
wc_add_notice( __( 'Please read and accept the terms and conditions to proceed with your order.', 'woocommerce' ), 'error' );
return;
}
// Update payment method.
if ( $order->needs_payment() ) {
try {
$payment_method_id = isset( $_POST['payment_method'] ) ? wc_clean( wp_unslash( $_POST['payment_method'] ) ) : false;
if ( ! $payment_method_id ) {
throw new Exception( __( 'Invalid payment method.', 'woocommerce' ) );
}
$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
$payment_method = isset( $available_gateways[ $payment_method_id ] ) ? $available_gateways[ $payment_method_id ] : false;
if ( ! $payment_method ) {
throw new Exception( __( 'Invalid payment method.', 'woocommerce' ) );
}
$order->set_payment_method( $payment_method );
$order->save();
$payment_method->validate_fields();
if ( 0 === wc_notice_count( 'error' ) ) {
$result = $payment_method->process_payment( $order_id );
// Redirect to success/confirmation/payment page.
if ( isset( $result['result'] ) && 'success' === $result['result'] ) {
$result['order_id'] = $order_id;
$result = apply_filters( 'woocommerce_payment_successful_result', $result, $order_id );
wp_redirect( $result['redirect'] ); //phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
exit;
}
}
} catch ( Exception $e ) {
wc_add_notice( $e->getMessage(), 'error' );
}
} else {
// No payment was required for order.
$order->payment_complete();
wp_safe_redirect( $order->get_checkout_order_received_url() );
exit;
}
do_action( 'woocommerce_after_pay_action', $order );
}
} else {
wc_add_notice( __( 'Order ID is missing. Please try again.', 'woocommerce' ), 'error' );
return;
}
}
}but im still not sure if the code is right or not , please check if it will be fixed in the next updates or not , thanks
The topic ‘PHP Warning: Undefined array key “order-pay”’ is closed to new replies.