You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修复WooCommerce中payment_method调用错误的问题

Fixing "payment_method was called incorrectly" Error in WooCommerce Payment Gateway Filter

That error usually pops up when your payment gateway filter runs in an inappropriate context (like the admin dashboard), tries to access WooCommerce session before it’s fully initialized, or attempts to modify gateway entries that don’t exist. Let’s adjust your function to resolve these issues:

Modified Function Code

public function custom_available_payment_gateways( $gateways ) {
    // Skip filtering in admin area (unless it's an AJAX request for front-end actions)
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
        return $gateways;
    }

    // Ensure WooCommerce Session is initialized and active
    if ( ! isset( WC()->session ) || ! WC()->session->has_session() ) {
        return $gateways;
    }

    $chosen_shipping_rates = WC()->session->get( 'chosen_shipping_methods', array() );

    // Filter gateways based on selected shipping method
    if ( in_array( 'local_pickup:14', $chosen_shipping_rates ) ) {
        // Only unset COD if it's present in the gateways array
        if ( isset( $gateways['cod'] ) ) {
            unset( $gateways['cod'] );
        }
    } elseif ( in_array( 'flat_rate:17', $chosen_shipping_rates ) || in_array( 'flat_rate:18', $chosen_shipping_rates ) ) {
        // Combine identical logic for flat rate methods to clean up code
        foreach ( array( 'bacs', 'przelewy24' ) as $gateway_key ) {
            if ( isset( $gateways[$gateway_key] ) ) {
                unset( $gateways[$gateway_key] );
            }
        }
    }

    return $gateways;
}

Key Fixes & Improvements

  • Admin Area Check: We skip the filter logic in the admin dashboard (except for AJAX requests that power front-end actions) because payment gateway filtering only makes sense for customers on the front end. This prevents unnecessary session access in contexts where it’s not needed.
  • Robust Session Validation: Instead of just checking if WC()->session exists, we use has_session() to confirm it’s active and ready. This avoids errors when the session hasn’t been initialized yet (like on early page loads).
  • Safe Gateway Unsetting: We always check if a gateway exists in the $gateways array before trying to unset it. Trying to delete a non-existent gateway can trigger warnings that lead to the "called incorrectly" error.
  • Cleaner Logic: We combined the handling for flat_rate:17 and flat_rate:18 since they do the same thing, making the code easier to maintain.

Important Note

Make sure your function is hooked correctly to the woocommerce_available_payment_gateways filter. If you’re using this in a class, the hook should look like:

add_filter( 'woocommerce_available_payment_gateways', array( $this, 'custom_available_payment_gateways' ) );

If you’re adding this directly to your theme’s functions.php file, use a standalone function instead:

function custom_available_payment_gateways( $gateways ) {
    // Paste the modified logic here
}
add_filter( 'woocommerce_available_payment_gateways', 'custom_available_payment_gateways' );

内容的提问来源于stack exchange,提问作者Wojciech

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:59:58