gpt4 book ai didi

php - 根据发货国家/地区使 Woocommerce 结帐电话字段可选

转载 作者:可可西里 更新时间:2023-11-01 01:11:29 25 4
gpt4 key购买 nike

在 Woocommerce 结帐中,我试图让特定运输国家/地区不需要电话字段。基于 "Make checkout phone field optional for specific countries in WooCommerce " 答案代码,它运行良好,我尝试进行一些更改以使此代码适用于shipping country 而不是billing 国家。

经过多次尝试,我无法弄清楚如何让它发挥作用。

任何帮助都将是非常棒的,非常感谢。

最佳答案

以下代码将使帐单电话字段仅在特定的“送货”国家/地区是必需的。

自 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 = '&nbsp;<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 中经过测试和工作。

相关:

关于php - 根据发货国家/地区使 Woocommerce 结帐电话字段可选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55206672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com