WooCommerce BACS支付新增条件显示金额字段技术咨询
WooCommerce BACS支付网关条件字段实现方案
以下是完整可直接使用的修改后代码,覆盖原有逻辑同时新增了条件显示、字段校验、数据存储与后台展示功能:
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(); echo '<div class="bacs-options" style="padding:10px 0;">'; // 下拉选择字段 woocommerce_form_field( 'bacs_option', array( 'type' => 'select', 'label' => __("请选择支付方式", "woocommerce"), 'class' => array('form-row-wide'), 'required' => true, 'options' => array( '' => __("请选择支付类型", "woocommerce"), 'exact' => sprintf( __("精确支付 %s(订单总金额)", "woocommerce"), wc_price( WC()->cart->total ) ), 'custom' => __("自定义金额支付", "woocommerce"), ), ), ''); // 自定义金额输入框,默认隐藏 woocommerce_form_field( 'bacs_custom_amount', array( 'type' => 'number', 'label' => __("请输入支付金额", "woocommerce"), 'class' => array('form-row-wide', 'hidden'), 'custom_attributes' => array( 'step' => '0.01', 'min' => '0' ) ), ''); echo '</div>'; // 联动JS逻辑 ?> <script> jQuery(function($){ // 初始化状态 toggleCustomAmountField(); // 监听下拉切换 $('body').on('change', '#bacs_option', function(){ toggleCustomAmountField(); }); // 切换输入框显示与必填属性 function toggleCustomAmountField(){ const selectVal = $('#bacs_option').val(); if(selectVal === 'custom'){ $('#bacs_custom_amount_field').removeClass('hidden').show(); $('#bacs_custom_amount').attr('required', true); }else{ $('#bacs_custom_amount_field').addClass('hidden').hide(); $('#bacs_custom_amount').removeAttr('required'); } } }); </script> <?php $description .= ob_get_clean(); } return $description; } // 结账页自定义字段校验 add_action('woocommerce_checkout_process', 'bacs_option_validation' ); function bacs_option_validation() { if ( isset($_POST['payment_method']) && $_POST['payment_method'] === 'bacs' ) { // 校验下拉选项是否选择 if( isset($_POST['bacs_option']) && empty($_POST['bacs_option']) ) { wc_add_notice( __( '请选择银行转账的支付类型' ), 'error' ); } // 选择自定义金额时校验金额是否合法 if( $_POST['bacs_option'] === 'custom' ){ $amount = floatval($_POST['bacs_custom_amount']); if( empty($_POST['bacs_custom_amount']) || $amount <= 0 ){ wc_add_notice( __( '请输入有效的自定义支付金额,金额必须大于0' ), 'error' ); } } } } // 结账页自定义字段保存至订单元数据 add_action('woocommerce_checkout_create_order', 'save_bacs_option_order_meta', 10, 2 ); function save_bacs_option_order_meta( $order, $data ) { if ( isset($_POST['bacs_option']) && ! empty($_POST['bacs_option']) ) { $option_text = $_POST['bacs_option'] === 'exact' ? '精确支付订单金额' : '自定义金额支付'; $order->update_meta_data( '_bacs_option' , esc_attr($option_text) ); // 保存自定义金额 if( $_POST['bacs_option'] === 'custom' ){ $order->update_meta_data( '_bacs_custom_amount', floatval($_POST['bacs_custom_amount']) ); } } } // 后台订单页账单地址区块下方展示自定义字段 add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_bacs_option_near_admin_order_billing_address', 10, 1 ); function display_bacs_option_near_admin_order_billing_address( $order ){ if( $bacs_option = $order->get_meta('_bacs_option') ) { echo '<div class="bacs-option"> <p><strong>'.__('银行转账支付类型').':</strong> ' . $bacs_option . '</p>'; // 存在自定义金额则展示 if( $custom_amount = $order->get_meta('_bacs_custom_amount') ){ echo '<p><strong>'.__('自定义支付金额').':</strong> ' . wc_price($custom_amount) . '</p>'; } echo '</div>'; } }
功能说明
- 代码可直接添加到当前主题
functions.php文件或自定义功能插件中使用 - 自定义金额输入框默认隐藏,仅当用户选择「自定义金额支付」时显示,同时自动开启必填校验
- 订单后台会在账单地址下方同步展示支付类型和用户填写的自定义金额(若选择自定义支付)
内容的提问来源于stack exchange,提问作者Mike F
相关产品推荐
相关产品推荐

