编程语言
首页 > 编程语言> > php – Woocommerce中特定支付网关结账时的附加字段

php – Woocommerce中特定支付网关结账时的附加字段

作者:互联网

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

基本上,当用户点击自定义支付网关时,应出现“选择”字段,他们必须从选择字段中选择一些内容.

我附上了一个截图,以更好地表达我需要做的事情.不幸的是,我在文档中找不到任何关于它的信息.

this is the image

解决方法:

以下代码将附加到结帐页面中的网关描述,自定义文本输入字段(此处为此示例为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文件中.测试和工作.

enter image description here

Related: 07001

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

07002

标签:checkout,php,wordpress,woocommerce,payment-gateway
来源: https://codeday.me/bug/20191002/1841602.html