如何在WooCommerce结帐中创建必填字段 [英] How to make a form field required in WooCommerce checkout
问题描述
在结帐"表单中,我创建了一个选择字段.我的问题是,如何在Wordpress或Woocmmerce中按要求保留这个阵营.
In the Checkout form I created a select field. My question is how in Wordpress or Woocmmerce this camp can be left as required.
<p class="form-row form-row-wide validate-required validate-region" id="shipping_region_field" data-priority="6">
<select name="shipping_region" id="shipping_region" class="state_select select2-selection--single" autocomplete="address-level1" data-placeholder="" tabindex="-1" aria-hidden="true">
<option>Opción 01</option>
<option>Opción 02</option>
</select>
</p>
推荐答案
1)对于普通或自定义的计费和运输字段,您可以在结帐页面上使用woocommerce_billing_fields
或woocommerce_shipping_fields
操作挂钩,如下所示.
1) For normal or custom billing and shipping fields you can use woocommerce_billing_fields
or woocommerce_shipping_fields
action hooks on checkout page as follow.
这将使自定义结帐字段为必填字段,无需添加验证脚本并按顺序保存.该字段也会显示在我的帐户编辑地址"字段部分.
It will make a custom checkout field required without any need to add a validation script and to save it in the order. The field will also appear in My account edit adresses field section.
一些参数解释:
- 类
'update_totals_on_change'
允许在更改时触发更新签出". -
'required'
属性使该字段为必填字段 -
'priority'
属性使您可以更改字段的位置.
- The class
'update_totals_on_change'
allow to trigger "update checkout" on change. - The
'required'
attribute make the field required or not - The
'priority'
attribute allow you to change the location of the field.
代码:
add_filter( 'woocommerce_shipping_fields', 'display_shipping_region_checkout_field', 20, 1 );
function display_shipping_region_checkout_field( $fields ) {
$fields['shipping_region'] = array(
'type' => 'select',
'label' => __("Region", "woocommerce") ,
'class' => array('form-row-wide', 'update_totals_on_change'),
'required' => true,
'options' => array(
'' => __("Choose a region please"),
'option-1' => __("Option 01"),
'option-2' => __("Option 02"),
'option-3' => __("Option 03"),
),
'priority' => 100,
'clear' => true,
);
return $fields;
}
代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
2)在特定情况下,您需要使用woocommerce_default_address_fields
过滤器.此过滤器适用于所有结算和发货默认字段*(
2) In specific cases you need to use the woocommerce_default_address_fields
filter. This filter is applied to all billing and shipping default fields *(see the documentation). It's used only some default checkout fields.
3) You can also use woocommerce_checkout_fields
that has $fields
as function argument (see documentation).
4)对于其他自定义结帐字段,您可以将以下挂钩之一与woocommerce_form_field()函数一起使用:
4) For other custom checkout fields you can use one of the following hooks with the woocommerce_form_field() function:
-
woocommerce_before_checkout_billing_form
具有$checkout
作为函数参数 -
woocommerce_before_checkout_billing_form
具有$checkout
作为函数参数 -
woocommerce_before_checkout_shipping_form
具有$checkout
作为函数参数 -
woocommerce_before_checkout_shipping_form
具有$checkout
作为函数参数 -
woocommerce_before_order_notes
具有$checkout
作为函数参数 -
woocommerce_after_order_notes
具有$checkout
作为函数参数
woocommerce_before_checkout_billing_form
has$checkout
as function argumentwoocommerce_before_checkout_billing_form
has$checkout
as function argumentwoocommerce_before_checkout_shipping_form
has$checkout
as function argumentwoocommerce_before_checkout_shipping_form
has$checkout
as function argumentwoocommerce_before_order_notes
has$checkout
as function argumentwoocommerce_after_order_notes
has$checkout
as function argument
用于显示字段,验证字段并按以下顺序保存字段的代码:
The code that display the field, validate the field and save the field in the order:
// Display field
add_action( 'woocommerce_after_checkout_shipping_form', 'display_shipping_region_after_checkout_shipping_form', 10, 1 );
function display_shipping_region_after_checkout_shipping_form ( $checkout ) {
woocommerce_form_field( 'shipping_region', array(
'type' => 'select',
'label' => __("Region", "woocommerce") ,
'class' => array('form-row-wide','update_totals_on_change'),
'required' => true,
'options' => array(
'' => __("Choose a region please"),
'option-1' => __("Option 01"),
'option-2' => __("Option 02"),
'option-3' => __("Option 03"),
),
'priority' => 100,
'clear' => true,
), $checkout->get_value( 'shipping_region' ) );
}
// Field Validation
add_action('woocommerce_checkout_process', 'shipping_region_custom_checkout_field_validation');
function shipping_region_custom_checkout_field_validation() {
if ( isset($_POST['shipping_region']) && empty($_POST['shipping_region']) )
wc_add_notice( __( 'Please select something into Region field.' ), 'error' );
}
// Save Field value
add_action( 'woocommerce_checkout_create_order', 'action_checkout_create_order_callback', 10, 2 );
function action_checkout_create_order_callback( $order, $data ) {
if ( isset($_POST['shipping_region']) && empty($_POST['shipping_region']) ) {
$order->update_meta_data( '_shipping_region', esc_attr($_POST['shipping_region']) );
if( $order->get_user_id() > 0 )
update_user_met( $order->get_user_id(), 'shipping_region', esc_attr($_POST['shipping_region']) );
}
}
代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
WooCommerce文档:自定义结帐字段使用动作和过滤器
WooCommerce Documentation: Customizing checkout fields using actions and filters
这篇关于如何在WooCommerce结帐中创建必填字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!