Ciu*_*dan 2 php wordpress jquery checkout woocommerce
仅当在 WooCommerce 结帐屏幕中选择某个国家/地区时,我才必须按要求(强制)显示电话。
实时查看所选国家的验证规则是什么?
我尝试过以下代码,该代码可以使电话不再需要:
add_filter( 'woocommerce_billing_fields', 'wc_npr_filter_phone', 10, 1 );
function wc_npr_filter_phone( $address_fields ) {
$address_fields['billing_phone']['required'] = false;
return $address_fields;
}
Run Code Online (Sandbox Code Playgroud)
您主要需要在客户端使用 javascript 进行实时事件或现场事件\xe2\x80\xa6 下面的代码主要使用 jQuery 和一些 PHP,以使仅当客户选择特定国家/地区时才需要计费电话:
\n\n// Making the billing phone field not required (by default)\nadd_filter( \'woocommerce_billing_fields\', \'filter_billing_phone_field\', 10, 1 );\nfunction filter_billing_phone_field( $fields ) {\n $fields[\'billing_phone\'][\'required\'] = false;\n return $fields;\n}\n\n// Real time country selection actions\nadd_action( \'woocommerce_after_order_notes\', \'custom_checkout_scripts_and_fields\', 10, 1 );\nfunction custom_checkout_scripts_and_fields( $checkout ) {\n $required = esc_attr__( \'required\', \'woocommerce\' );\n\n // HERE set the countries codes (2 capital letters) in this array:\n $countries = array( \'UK\', \'BE\', \'GE\', \'IT\', \'ES\' );\n\n // Hidden field for the phone number validation\n echo \'<input type="hidden" name="billing_phone_check" id="billing_phone_check" value="0">\';\n $countries_str = "\'".implode( "\', \'", $countries )."\'"; // Formatting countries for jQuery\n ?>\n <script type="text/javascript">\n (function($){\n var required = \'<abbr class="required" title="<?php echo $required; ?>">*</abbr>\',\n countries = [<?php echo $countries_str; ?>],\n location = $(\'#billing_country option:selected\').val(),\n phoneCheck = \'input#billing_phone_check\';\n\n function actionRequire( actionToDo=\'yes\', selector=\'\' ){\n if ( actionToDo == \'yes\' ) {\n $(selector).addClass("validate-required");\n $(selector+\' label\').append(required);\n } else {\n $(selector).removeClass("validate-required");\n $(selector+\' label > .required\').remove();\n }\n $(selector).removeClass("woocommerce-validated");\n $(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field");\n }\n\n // Default value when loading\n actionRequire( \'no\',\'#billing_phone_field\' );\n if( $.inArray( location, countries ) >= 0 && $(phoneCheck).val() == \'0\' ){\n actionRequire( \'yes\',\'#billing_phone_field\' );\n $(phoneCheck).val(\'1\');\n }\n\n // Live value\n $( \'form.checkout\' ).on( \'change\', \'#billing_country\', function(){\n var location = $(\'#billing_country option:selected\').val();\n if ( $.inArray( location, countries ) >= 0 && $(phoneCheck).val() == 0 ) {\n actionRequire( \'yes\',\'#billing_phone_field\' );\n $(phoneCheck).val(\'1\');\n } else if ( $(phoneCheck).val() == 1 ) {\n actionRequire( \'no\',\'#billing_phone_field\' );\n $(phoneCheck).val(\'0\');\n }\n });\n })(jQuery);\n </script>\n <?php\n}\n\n// Phone number validation, when it\'s visible\nadd_action(\'woocommerce_checkout_process\', \'billing_phone_field_process\');\nfunction billing_phone_field_process() {\n // Check if set, if its not set add an error.\n if ( ! $_POST[\'billing_phone\'] && $_POST[\'billing_phone_check\'] == \'1\' )\n wc_add_notice( __( \'Please enter a number phone.\' ), \'error\' );\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。
\n\n经过测试并有效。
\n\n\n\n相关(2019):根据发货国家/地区将 Woocommerce 结帐电话字段设为可选
\n