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

在WooCommerce结账页根据账单邮箱/手机号动态控制支付方式

WooCommerce结账字段与支付方式控制问题解答

问题描述

开发WooCommerce插件时,需根据用户在结账表单输入的邮箱或手机号隐藏特定支付方式。已实现以下逻辑:

  1. JS监听邮箱/手机号字段变更,触发结账更新:
$(document).ready(function () {
    if ($('form.woocommerce-checkout').length) {
        $('#billing_phone_field, #billing_email_field').on('change', function () {
            $('body').trigger('update_checkout');
        });
    }
});
  1. 使用woocommerce_available_payment_gateways钩子尝试从WC Session获取字段值,但发现Session中的邮箱和手机号为空:
public function update_payment_methods_based_on_user_input( $available_gateways ) {

    if ( is_admin() ) return $available_gateways;
    if ( ! is_checkout() ) return $available_gateways;

    $wp_session = isset( WC()->session ) && WC()->session->has_session() ? WC()->session : null;

    if ( is_a( $wp_session, 'WC_Session_Handler' ) ) {
        $cust_session = $wp_session->get( 'customer' );

        file_put_contents( ABSPATH . '/wp-content/cust_session.txt', print_r( $cust_session, true ) );
    }

    return $available_gateways;

}

疑问:为什么Session中拿不到值?不通过Session的话,该如何获取这两个字段的值?

问题原因

用户在结账表单输入的邮箱、手机号不会自动同步到WC Session的customer数据集合中:

  • WC Session的customer数据仅存储已登录用户的账户信息,或用户主动创建账户后持久化的信息;
  • 触发update_checkout时,表单字段值仅通过AJAX请求的POST参数传递到后台,并未自动写入Session。

解决方案

直接从AJAX请求的POST参数中读取字段值,这是触发update_checkout时获取实时输入值的正确方式:

public function update_payment_methods_based_on_user_input( $available_gateways ) {
    // 非结账页面或后台直接返回原支付方式
    if ( is_admin() || ! is_checkout() ) {
        return $available_gateways;
    }

    // 从POST参数中获取并清洗邮箱、手机号
    $billing_email = isset( $_POST['billing_email'] ) ? sanitize_email( $_POST['billing_email'] ) : '';
    $billing_phone = isset( $_POST['billing_phone'] ) ? sanitize_text_field( $_POST['billing_phone'] ) : '';

    // 示例逻辑:根据邮箱域名隐藏货到付款(cod)方式
    if ( ! empty( $billing_email ) && strpos( $billing_email, '@example.com' ) !== false ) {
        unset( $available_gateways['cod'] );
    }

    // 示例逻辑:根据手机号前缀隐藏某支付方式
    if ( ! empty( $billing_phone ) && str_starts_with( $billing_phone, '138' ) ) {
        unset( $available_gateways['paypal'] );
    }

    return $available_gateways;
}

可选:持久化字段值到Session

如果后续流程需要复用这些字段值,可以在获取后手动存入WC Session:

// 确保Session已初始化
if ( WC()->session->has_session() ) {
    if ( ! empty( $billing_email ) ) {
        WC()->session->set( 'custom_billing_email', $billing_email );
    }
    if ( ! empty( $billing_phone ) ) {
        WC()->session->set( 'custom_billing_phone', $billing_phone );
    }
}

// 后续读取Session值
$saved_email = WC()->session->get( 'custom_billing_email' );

内容的提问来源于stack exchange,提问作者James B.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 11:05:25