【问题标题】:Additional field on checkout for specific payment gateway in WoocommerceWoocommerce 中特定支付网关的结帐附加字段
【发布时间】:2021-06-10 04:22:03
【问题描述】:

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

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

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

【问题讨论】:

  • 我们可以看看你至少尝试过实现的代码吗?

标签: php wordpress woocommerce payment-gateway checkout


【解决方案1】:

以下代码将附加到结帐页面中的网关描述,自定义文本输入字段(此处,在此示例中为 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 文件中。经过测试并且有效。

相关:Validate and save additional checkout field for specific payment gateway in Woocommerce


完整方式,验证字段,将其保存为订单自定义元数据并显示在订单和电子邮件通知中:

Save and display specific payment gateway additional field everywhere in Woocommerce

【讨论】:

  • 感谢您的帮助!
猜你喜欢
  • 2019-05-20
  • 1970-01-01
  • 2021-04-24
  • 2019-05-03
  • 1970-01-01
  • 2019-10-08
  • 2012-12-28
  • 2019-06-15
相关资源
最近更新 更多