これまで同様の質問がありましたが、私はすべての解決策を試しましたが、何らかの理由で問題なく動作します。WooCommerceと電話フィールド検証の問題でカスタム登録フィールドを追加
私はユーザーが簡単に登録できるように私のサイトのフッターにミニWoocommerce登録フィールドを持っています。電話欄は必須ですが、偽の数字を入力する人の数を減らすために、最小の長さを設定します。
私はfunctions.phpに次のコードを追加しました。(私はあなたが理解しやすいように、フォームへのすべての通知を含めています)プレースホルダとすべてが動作しますが、最小の長さを取得できません「パターン」カスタム属性を使用して)作業します。
誰かが私の手助けをすることができれば、非常に感謝しています。
このフォームは私のウェブサイトのフッターに含まれています
add_filter('woocommerce_checkout_fields' , 'custom_override_checkout_fields');
function custom_override_checkout_fields($fields)
{
$fields['billing']['billing_phone']['custom_attributes'] = array("pattern" => ".{10,10}");
return $fields;
}
そして、これはのfunctions.phpの残りの部分である:wondercatspopup.com
私が追加したコードがある
/**
* To add WooCommerce registration form custom fields.
*/
function text_domain_woo_reg_form_fields() {
?>
<div class="formumuz" style="display:flex;"> <p class="form-row form-row-first">
<label style="display:none!important;" for="billing_first_name"><?php _e('First name', 'woocommerce'); ?><span class="required">*</span></label>
<input style="width: 130px;
display: inline-block; margin-right:1px;" type="text" class="woocommerce-Input woocommerce-Input--text input-text placeholder" placeholder="İsim/Name *" type="text" class="input-text" name="billing_first_name" id="billing_first_name" value="<?php if (!empty($_POST['billing_first_name'])) esc_attr_e($_POST['billing_first_name']); ?>" />
<label style="display:none!important;" for="billing_last_name"><?php _e('Last name', 'woocommerce'); ?><span class="required">*</span></label>
<input style="width: 130px;
display: inline-block; margin-left:1px;" type="text" class="woocommerce-Input woocommerce-Input--text input-text placeholder" placeholder="Soyisim/Surname *" type="text" class="input-text" name="billing_last_name" id="billing_last_name" value="<?php if (!empty($_POST['billing_last_name'])) esc_attr_e($_POST['billing_last_name']); ?>" />
</p></div>
<p style="margin-bottom: 0px; margin-top: 10px;" class="form-row form-row-wide">
<label style="display:none!important;" for="reg_billing_phone"><?php _e('Phone', 'woocommerce'); ?></label>
<input style="width:254px!important;" type="text" class="woocommerce-Input woocommerce-Input--text input-text placeholder" placeholder="Cep Telefonu/Mobile *" value="+905" name="billing_phone" id="reg_billing_phone" value="<?php esc_attr_e($_POST['billing_phone']); ?>" /> *
</p><br>
<div class="clear"></div>
<?php
}
add_action('woocommerce_register_form_start', 'text_domain_woo_reg_form_fields');
/**
* To validate WooCommerce registration form custom fields.
*/
function text_domain_woo_validate_reg_form_fields($username, $email, $validation_errors) {
if (isset($_POST['billing_first_name']) && empty($_POST['billing_first_name'])) {
$validation_errors->add('billing_first_name_error', __('İsim alanı zorunludur!/Name field is required!', 'woocommerce'));
}
if (isset($_POST['billing_last_name']) && empty($_POST['billing_last_name'])) {
$validation_errors->add('billing_last_name_error', __('Soyisim alanı zorunludur!/Surname field is required!', 'woocommerce'));
}
if (isset($_POST['billing_phone']) && empty($_POST['billing_phone'])) {
$validation_errors->add('billing_phone_error', __('Telefon alanı zorunludur!/Phone field is required!', 'woocommerce'));
}
return $validation_errors;
}
add_action('woocommerce_register_post', 'text_domain_woo_validate_reg_form_fields', 10, 3);
/**
* To save WooCommerce registration form custom fields.
*/
function text_domain_woo_save_reg_form_fields($customer_id) {
//First name field
if (isset($_POST['billing_first_name'])) {
update_user_meta($customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name']));
update_user_meta($customer_id, 'billing_first_name', sanitize_text_field($_POST['billing_first_name']));
}
//Last name field
if (isset($_POST['billing_last_name'])) {
update_user_meta($customer_id, 'last_name', sanitize_text_field($_POST['billing_last_name']));
update_user_meta($customer_id, 'billing_last_name', sanitize_text_field($_POST['billing_last_name']));
}
//Phone field
if (isset($_POST['billing_phone'])) {
update_user_meta($customer_id, 'phone', sanitize_text_field($_POST['billing_phone']));
update_user_meta($customer_id, 'billing_phone', sanitize_text_field($_POST['billing_phone']));
}
}
は完全に働いたこと、本当にありがとうございました!もう1つの最後の質問:上記のコードでは、2つの名字フィールドが表示されています(上のリンクに表示されています)。これも修正する方法を教えてください。 – eylul
@eylul私は私の答えコードでこの問題を抱えていません...これは、これらのフィールドのID「reg_sr_firstname」と「reg_sr_lastname」で行った他のカスタマイズによるものです。 – LoicTheAztec