Woocommerceのチェックアウトでは、特定の配送国で電話フィールドが不要になるようにしています。基づいて"https://stackoverflow.com/questions/47431299/make-checkout-phone-field-optional-for-specific-countries-in-woocommerce/47435682#47435682「正常に機能する回答コード。請求国ではなく配送国でこのコードが機能するように、いくつかの変更を加えようとしました。
何度も試したが、どうやって動かすのかわからなかった。
どんな助けでも素晴らしく、大いに感謝されます。
次のコードは、特定の「配送」国にのみ請求電話フィールドを必須にします。
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で動作します。
関連:
元の回答を提供してくれた@LoicTheAztecに大いに感謝しますが、このソリューションでは結果が不安定になり、電話フィールドが必須状態とオプション状態(オン/オフ)の間で交互に切り替わります。
元の回答では、請求先住所のみを使用していて、別の配送先住所または配送先住所を入力していない顧客も考慮されていません。
2019年の以下の更新バージョンをご覧ください
// SETTINGS: The countries codes (2 capital letters) in the array
function defined_countries_for_phone_field(){
return array( 'GB', 'JE', 'GG', 'IM' );
}
// 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>';
var countries = [<?php echo $countries_str; ?>]; if($('.shipping_address').is(':visible')) {
// ship to different country selected
var selectedcountry = $('#shipping_country option:selected').val(); } else { var selectedcountry = $('#billing_country option:selected').val();
}
//var selectedcountry = $('#shipping_country option:selected').val(); var phoneCheck = 'input#billing_phone_check'; var phoneField = '#billing_phone_field'; function actionRequire( actionToDo='yes', selector='' ){ if ( actionToDo == 'yes' ) { $(selector).addClass("validate-required");
$(selector+' label > .required').remove(); $(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(){
if($('.shipping_address').is(':visible')) { // ship to different country selected var selectedcountry = $('#shipping_country option:selected').val();
} else {
var selectedcountry = $('#billing_country option:selected').val(); } actionRequire( 'no', phoneField ); if( $.inArray( selectedcountry, countries ) == -1){
actionRequire( 'yes',phoneField );
$(phoneCheck).val('1'); } }, 300 ); // Live value $( 'form.checkout' ).on( 'change', '#billing_country, #shipping_country, #ship-to-different-address-checkbox', function(){
setTimeout( function(){
if($('.shipping_address').is(':visible')) { // ship to different country selected var selectedcountry = $('#shipping_country option:selected').val();
} else {
var selectedcountry = $('#billing_country option:selected').val(); } if ( $.inArray( selectedcountry, countries ) == -1) {
actionRequire( 'yes' ,phoneField );
$(phoneCheck).val('1'); } else { actionRequire( 'no' ,phoneField ); $(phoneCheck).val('0');
}
}, 300 );
});
})(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' );
}
特徴的なスターのコリン・エッグレスフィールドは、RomaDrama Liveでのスリル満点のファンとの出会いについて料理しました!加えて、大会での彼のINSPIREプログラム。
ノーザンエクスポージャーが90年代の最も人気のある番組の1つになった理由を確認するには、Blu-rayまたはDVDプレーヤーをほこりで払う必要があります。
ドミニカのボイリング湖は、世界で2番目に大きいボイリング湖です。そこにたどり着くまでのトレッキングは大変で長いですが、努力する価値は十分にあります。
画像クレジット:Richard Mackney / Flickrトラベリングライトは必需品だけを運ぶことを意味するかもしれませんが、デバイスを補充する方法がない外出先では、接続を維持するのが難しくなる可能性があります。それはあなたがすべての生き物の快適さやクールなガジェットを捨てる必要があるという意味ではありません、ただあなたがいくつかのより小さなものを手に入れる必要があるということです、そしておそらくあなた自身をジュースに保つためにいくつかの、例えば非正統的な充電装置を使うでしょう。
ニューヨーク州スプリンググレンにある放棄されたホモワックロッジのボーリング場。キャッツキル南部のこの地域は、ニューヨーク市からのユダヤ人の行楽客に人気があることから、かつてはボルシチベルトとして知られていました。
ブルックリンスレートブルックリンスレートのマグカップとコースターの賞賛をすでに歌っており、それらの食器製品も同様に堅実です。ブルックリンスレートは、さまざまなサイズとテクスチャのスレートの完全な採石場を販売しています。一部のオプションは赤でも利用できます。上で見ることができるように、彼らは同様にカスタマイズをします。
画像:Juan Gaertner / Shutterstock私たちの体の内部は、私たちの細胞とは何の関係もない何十億もの微生物が住んでいる本物の生態系です。これがまだ少し気になることではなかったかのように、これらの微生物の99%が研究されたことがないことがわかりました。
Zendaya shared a sweet photo in honor of boyfriend Tom Holland's 26th birthday Wednesday
シーレン「Ms.JuicyBaby」ピアソンは、先月脳卒中で入院した後、「もう一度たくさんのことをする方法を学ばなければならない」ため、言語療法を受けていることを明らかにしました。
オスカー受賞者の世紀半ばの家には、3つのベッドルーム、2つのバス、オーシャンフロントの景色があります。
Bioscoutは、農家を運転席に置くという使命を負っています。Artesian(GrainInnovate)やUniseedと並んで、最新のシードラウンドでチームを支援できることをうれしく思います。問題真菌症による重大な作物の損失は、農民にとって試練であることが証明されています。
遠隔医療は、パンデミック後の時代では新しいものではなく、時代遅れの分野でもありません。しかし、業界を詳しく見ると、需要と供給の強力な持続可能性と、米国で絶え間ない革命となる強力な潜在的成長曲線を示しています。
2021年は、世界的なベンチャーキャピタル(VC)の資金調達にとって記録的な年でした。DealStreetAsiaによると、東南アジアも例外ではなく、この地域では年間で記録的な25の新しいユニコーンが採掘されました。
計算に対する私たちの欲求とムーアの法則が提供できるものとの間には、指数関数的に増大するギャップがあります。私たちの文明は計算に基づいています—建築と想像力の現在の限界を超える技術を見つけなければなりません。