Woocommerce 中特定支付网关的结帐附加字段

Additional field on checkout for specific payment gateway in Woocommerce

我有一个自定义的 Woocommerce 支付网关,我需要在支付 selected 时在结账时添加额外的字段。

基本上,当用户单击自定义支付网关时,应该会出现一个 "select" 字段,他们必须从 select 字段中选择一些内容。

我附上了一张截图,以更好地表达我需要做什么的想法。不幸的是,我在文档中找不到任何相关信息。

以下代码将附加到结帐页面中的网关描述,自定义文本输入字段(在此示例中为 BACS 支付网关)

// BACS payement gateway description: Append custom select field
add_filter( 'woocommerce_gateway_description', 'gateway_bacs_custom_fields', 20, 2 );
function gateway_bacs_custom_fields( $description, $payment_id ){
    //
    if( 'bacs' === $payment_id ){
        ob_start(); // Start buffering

        echo '<div  class="bacs-fields" style="padding:10px 0;">';

        woocommerce_form_field( 'field_slug', array(
            'type'          => 'select',
            'label'         => __("Fill in this field", "woocommerce"),
            'class'         => array('form-row-wide'),
            'required'      => false,
            'options'       => array(
                ''          => __("Select something", "woocommerce"),
                'choice-1'  => __("Choice one", "woocommerce"),
                'choice-2'  => __("Choice two", "woocommerce"),
            ),
        ), '');

        echo '<div>';

        $description .= ob_get_clean(); // Append buffered content
    }
    return $description;
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。

Related:


The complete way, validating the field, saving it as order custom meta data and display it on orders and email notifications:

Save and display specific payment gateway additional field everywhere in Woocommerce