php - 根据发货国家/地区将 Woocommerce 结帐电话字段设为可选
问题描述
在 Woocommerce 结帐中,我正在尝试使特定运输国家/地区不需要电话字段。基于“在 WooCommerce 中为特定国家/地区设置结帐电话字段可选”答案代码,它工作正常,我尝试进行一些更改以使此代码适用于发货国家/地区而不是计费国家/地区。
经过多次尝试,我无法弄清楚如何使它工作。
任何帮助都会很棒,非常感谢。
解决方案
以下代码将使计费电话字段仅适用于特定的“送货”国家/地区。
自 Woocommerce 版本 3.4+ 以来,Woocommerce 表单字段发生了一些变化,因此需要额外的功能和代码。
我还扩展了代码以处理“我的帐户”>“编辑地址”中的电话字段行为,客户可以在其中更改他的帐户数据。
这是完整的代码(在第一个函数中定义您的国家代码):
// SETTINGS: The countries codes (2 capital letters) in the array
function defined_countries_for_phone_field(){
return array( 'UK', 'BE', 'GE', 'IT', 'ES' );
}
// Remove "(optional)" from non required "Billing phone" field
add_filter( 'woocommerce_form_field' , 'remove_checkout_optional_fields_label', 10, 4 );
function remove_checkout_optional_fields_label( $field, $key, $args, $value ) {
// Get the defined countries codes
$countries = defined_countries_for_phone_field();
// Get Customer shipping country
$shipping_country = WC()->customer->get_shipping_country();
// Only on checkout page and My account > Edit address for billing phone field
if( 'billing_phone' === $key && ( ( is_wc_endpoint_url( 'edit-address' )
&& ! in_array($shipping_country, $countries) ) || is_checkout() ) ) {
$optional = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
$field = str_replace( $optional, '', $field );
}
return $field;
}
// Make the billing phone field optional (by default)
add_filter( 'woocommerce_billing_fields', 'filter_billing_phone_field', 10, 1 );
function filter_billing_phone_field( $fields ) {
// Get the defined countries codes
$countries = defined_countries_for_phone_field();
// Get Customer shipping country
$shipping_country = WC()->customer->get_shipping_country();
// Only on checkout page and My account > Edit address
if ( ( is_wc_endpoint_url( 'edit-address' )
&& ! in_array($shipping_country, $countries) ) || is_checkout() )
$fields['billing_phone']['required'] = false;
return $fields;
}
// Real time shipping country selection actions
add_action( 'woocommerce_after_order_notes', 'custom_checkout_scripts_and_fields', 10, 1 );
function custom_checkout_scripts_and_fields( $checkout ) {
$required = esc_attr__( 'required', 'woocommerce' );
// Get the defined countries codes
$countries = defined_countries_for_phone_field();
// Hidden field for the phone number validation
echo '<input type="hidden" name="billing_phone_check" id="billing_phone_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 = $('#shipping_country option:selected').val(),
phoneCheck = 'input#billing_phone_check',
phoneField = '#billing_phone_field';
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 Once DOM is loaded (with a 300 ms delay)
setTimeout( function(){
actionRequire( 'no', phoneField );
if( $.inArray( location, countries ) >= 0 && $(phoneCheck).val() == '0' ){
actionRequire( 'yes',phoneField );
$(phoneCheck).val('1');
}
}, 300 );
// Live value
$( 'form.checkout' ).on( 'change', '#shipping_country', function(){
var location = $('#shipping_country option:selected').val();
if ( $.inArray( location, countries ) >= 0 && $(phoneCheck).val() == 0 ) {
actionRequire( 'yes' ,phoneField );
$(phoneCheck).val('1');
} else if ( $(phoneCheck).val() == 1 ) {
actionRequire( 'no' ,phoneField );
$(phoneCheck).val('0');
}
});
})(jQuery);
</script>
<?php
}
// Phone number validation, when the field is required
add_action('woocommerce_checkout_process', 'billing_phone_field_process');
function billing_phone_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['billing_phone'] && $_POST['billing_phone_check'] == '1' )
wc_add_notice( __( 'Please enter a number phone.' ), 'error' );
}
代码位于您的活动子主题(或活动主题)的 function.php 文件中。从 3.4 及更高版本开始在 WooCommerce 中测试和工作。
有关的:
推荐阅读
- node.js - 在 Sinon 中重置存根历史记录失败
- java - Camel - 使用先前设置的属性作为方法的参数
- intellij-idea - 新文件未出现在 Intellij 项目资源管理器中
- javascript - Javascript 代码将内存泄漏到 DOM 节点 - Javascript 参考在哪里
- php - 如何使用数组显示表单?
- postgresql - 如何确保 docker-compose 不会使用 postgres 数据删除我的卷
- marklogic - 无法使用 DHF 5.2.1 部署到应用服务器上的 SSL
- regex - Convert Regex to be safari compliant
- python - ImportError When Importing Tensorflow Pywrap_tfe
- apache-kafka - Partition replication in case of messages directly posted to a partition in Kafka