Woocommerce中特定付款网关的结帐附加字段
我有一个自定义的Woocommerce付款网关,选择付款后,我需要在结帐栏上添加其他字段.
I have a custom Woocommerce payment gateway and I need to add additional field on the checkout when the payment is selected.
基本上,当用户单击自定义支付网关时,将出现选择"字段,他们必须从选择字段中进行选择.
Basically, when the users click the custom payment gateway a "select" field should appear and they have to choose something from the select field.
我附上了一张截图,以更好地表达我需要做的事情的想法.不幸的是,我在文档中找不到有关此信息.
I have attached a screenshot to represent better the idea of what I need to do. Unfortunately, I couldn't find any info about that in the Docs.
以下代码将添加到结帐页面中网关描述的自定义文本输入字段(在此示例中,是BACS付款网关):
The following code will append to the gateway description in checkout page, a custom text input field (here, in this example to BACS payment gateway):
// 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文件中.经过测试并可以正常工作.
Code goes in functions.php file of your active child theme (or active theme). tested and works.
完整方法,验证字段,将其保存为订单自定义元数据,并在订单和电子邮件通知中显示:
The complete way, validating the field, saving it as order custom meta data and display it on orders and email notifications: