在 WooCommerce Checkout 中选择付款方式时如何显示自定义付款字段?

问题描述 投票:0回答:1

我在 WooCommerce 中有一个自定义支付网关,我希望当用户在结帐页面上选择此付款方式时显示其他付款字段。目前,我正在使用 payment_fields() 方法,但我遇到了表单显示问题。有人可以指导我当用户在结账过程中点击我的付款方式时如何正确显示我的自定义付款字段吗?

    public function payment_fields()
    {
        $cc_form = new WC_Payment_Gateway_CC();
        $cc_form->id = $this->id;
        $cc_form->supports = $this->supports;
        $cc_form->form();
    }

如果我将构造中的 payment_fields() 字段加载为 $this-> payment_fields(),则表单将显示在页面顶部,但我不喜欢这样。单击我的付款方式时,我希望显示此表单而不是说明文本。如果我不将其添加到构造中,则该表单不会出现在任何地方。

woocommerce payment gateway
1个回答
0
投票

首先,尝试在构造函数中添加/替换:

$this->has_fields = true;

然后尝试添加/替换:

public function payment_fields() {
    if ( $this->supports( 'custom_credit_card_form' ) ) {
        $this->credit_card_form();
    }
}

最后,请尝试在支付网关类之外添加以下内容:

add_filter( 'woocommerce_payment_gateway_supports', 'filter_payment_gateway_supports', 10, 3 );
function filter_payment_gateway_supports( $supports, $feature, $payment_gateway ) {
    // Here in the array, set the allowed payment method IDs
    $allowed_payment_method_ids = array('your_payment_id');

    if ( in_array($payment_gateway->id, $allowed_payment_method_ids ) && $feature === 'custom_credit_card_form' ) {
        $supports = true;
    }
    return $supports;
}

它可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.