Woocommerce:将特定国家/地区的计费字段设置为可选是行不通的



在我的Wordpress Woocommerce网站中,当用户选择特定的计费国家/地区时,我需要跳过计费城市字段所需的验证。

我已经在以下类似问题中提到了答案,但这两个问题似乎都不适用于我的结帐页面。我所做的只是将这些函数复制粘贴到活动主题的函数.php中,并相应地更改字段和函数名称。但是更改或代码不会出现在结帐页面的源代码中,并且所需的验证仍然适用于不需要输入城市的国家/地区。

参考(以下答案都不适用于我的结帐页面(

将结帐电话字段设置为WooCommerce中特定国家/地区的可选

根据运输国家/地区使Woocommerce结帐电话字段可选

我的代码

// Making the billing city field not required (by default)
add_filter( 'woocommerce_billing_fields', 'filter_billing_city_field');
function filter_billing_city_field( $fields ) {
$fields['billing_city']['required'] = false;
return $fields;
}
// Real time country selection actions
add_action( 'woocommerce_after_order_notes', 'custom_checkout_scripts_and_fields', 8, 1 );
function custom_checkout_scripts_and_fields( $checkout ) {
$required = esc_attr__( 'required', 'woocommerce' );
// HERE set the countries codes (2 capital letters) in this array:
$countries = array( 'LK');
// Hidden field for the phone number validation
echo '<input type="hidden"  name="billing_city_check" id="billing_city_check" value="0">';
$countries_str = "'".implode( "', '", $countries )."'"; // Formatting countries for jQuery
?>
<script type="text/javascript">
(function($){
var required = '<abbr class="required" title="<?php echo $required; ?>">*</abbr>',
countries = [<?php echo $countries_str; ?>],
location = $('#billing_country option:selected').val(),
cityCheck = 'input#billing_city_check';
function actionRequire( actionToDo='yes', selector='' ){
if ( actionToDo == 'yes' ) {
$(selector).addClass("validate-required");
$(selector+' label').append(required);
} else {
$(selector).removeClass("validate-required");
$(selector+' label > .required').remove();
}
$(selector).removeClass("woocommerce-validated");
$(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
}
// Default value when loading
actionRequire( 'no','#billing_city_check' );
if( $.inArray( location, countries ) >= 0  && $(cityCheck).val() == '0' ){
actionRequire( 'yes','#billing_city_check' );
$(cityCheck).val('1');
}
// Live value
$( 'form.checkout' ).on( 'change', '#billing_country', function(){
var location = $('#billing_country option:selected').val();
if ( $.inArray( location, countries ) >= 0 && $(cityCheck).val() == 0 ) {
actionRequire( 'yes','#billing_city_check' );
$(cityCheck).val('1');
} else if ( $(cityCheck).val() == 1 ) {
actionRequire( 'no','#billing_city_check' );
$(cityCheck).val('0');
}
});
})(jQuery);
</script>
<?php
}
// City validation, when it's visible
add_action('woocommerce_checkout_process', 'billing_city_field_process');
function billing_city_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['billing_city'] && $_POST['billing_city_check'] == '1' )
wc_add_notice( __( 'Please enter city.' ), 'error' );
}

将此代码片段添加到您激活的主题function.php末尾。

add_filter('woocommerce_billing_fields', 'vg_customize_checkout_form');
function vg_customize_checkout_form($fields)
{
$fields['billing_city']['required'] = false;
return $fields;
}
#Source: https://github.com/woocommerce/woocommerce/blob/master/includes/class-wc-checkout.php#L845
add_action('woocommerce_after_checkout_validation', 'vg_custom_validation_billing_city', 10, 2);
function vg_custom_validation_billing_city($fields, $error)
{
if('IN' != $fields['billing_country'] && empty($fields['billing_city'])) {
$error->add('validation', 'City is required field.');
}
}

让我们看看每个钩子的功能:

滤钩woocommerce_billing_fields

官方文件

https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-8

https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-2

目的:我们可以通过覆盖数组元素来更改字段的预定义属性,因为我们已经覆盖了billing_city字段的required属性


动作钩子:woocommerce_after_checkout_validation

官方文件

https://docs.woocommerce.com/wc-apidocs/source-class-WC_Checkout.html#830

目的:在购物车成功结帐后添加自定义代码。在这里,我们可以添加自定义验证,并根据验证成功或失败,我们可以通过错误。

在这里,我们验证了帐单国家/地区是否不是印度,那么应该填写该城市。如果用户没有填写印度以外的城市,那么我们的自定义错误City is a required field.将被抛出。


参考资料:

  1. https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
  2. https://docs.woocommerce.com/wc-apidocs/hook-docs.html

最新更新