• Resolved bafna

    (@bafna)


    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 paying

    after 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.InputNotSanitized

        if ( 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

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter bafna

    (@bafna)

    or maybe its this part ? idk

    [31-Oct-2024 14:22:27 UTC] PHP Fatal error: Uncaught Error: Call to a member function get_id() on bool in public_html/wp-content/plugins/woocommerce/includes/class-wc-form-handler.php:415 Stack trace: #0 public_html/wp-includes/class-wp-hook.php(324): WC_Form_Handler::pay_action() #1 public_html/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters() #2 public_html/wp-includes/plugin.php(565): WP_Hook->do_action() #3 public_html/wp-includes/class-wp.php(830): do_action_ref_array() #4 public_html/wp-includes/functions.php(1336): WP->main() #5 public_html/wp-blog-header.php(16): wp() #6 public_html/index.php(17): require(‘…’) #7 {main} thrown in public_html/wp-content/plugins/woocommerce/includes/class-wc-form-handler.php on line 415

    Woocommerce 9.3.3 started throwing out random fatal errors on my website, had to downgrade to 9.2.3:

    https://developer.woocommerce.com/releases/

    try that and see if it helps.

    Something’s fishy with WC 9.3.3

    Thread Starter bafna

    (@bafna)

    thanks , i still wish for a fix update soon :)))

    • This reply was modified 1 year, 7 months ago by bafna.
    Plugin Support shahzeen(woo-hc)

    (@shahzeenfarooq)

    Hi there!

    Just to confirm, was your issue resolved after downgrading woocommerce to older version? I’m currently using WooCommerce 9.3.3 on my test site and haven’t encountered any fatal errors.

    For testing, could you activate the default Storefront theme and only WooCommerce to check if the error still appears? To avoid affecting your live site, I recommend creating a staging environment, deactivating all plugins, and activating the Storefront theme there.

    For more details on running a conflict test, please refer to this guide: How to Test for Conflicts.

    In the meantime, I’d like to understand your site properly. Please share with us the necessary information below for us to investigate the issue further:

    • System Status Report which you can find via WooCommerce > Status > Get system report > Copy for support.
    • Fatal error logs (if any) under WooCommerce > Status > Logs.

    Once we have more information, we’ll be able to assist you further.


Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘PHP Warning: Undefined array key “order-pay”’ is closed to new replies.