php - WooCommerce 结帐自定义字段条件验证

标签 php wordpress validation woocommerce checkout

我想将我的自定义字段保存到新用户元数据中。 我的自定义字段 = T.C.金利克没有

代码行开头的函数用于将字段添加到成员(member)部分。

我主要想做的是当涉及到注册用户的结帐页面时,自动拉取我在顶部控制的自定义区域

/** TC Kimlik No Ekleme **/

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields( $fields ) {
$fields['billing']['shipping_tc'] = array(
'label' => __('TC Kimlik No', 'woocommerce'),
'placeholder' => _x('Fatura İçin Gerekli', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true
);

return $fields;
}
/** TC Doğrula  **/
function isTcKimlik($tc){
if(strlen($tc) < 11){ return false; }
if($tc[0] == '0'){ return false; }
$plus = ($tc[0] + $tc[2] + $tc[4] + $tc[6] + $tc[8]) * 7;
$minus = $plus - ($tc[1] + $tc[3] + $tc[5] + $tc[7]);
$mod = $minus % 10;
if($mod != $tc[9]){ return false; }
$all = '';
for($i = 0 ; $i < 10 ; $i++){ $all += $tc[$i]; }
if($all % 10 != $tc[10]){ return false; }

return true;
}
/**  TC Kimlik Noyu Doğrula **/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
$tcno = $_POST['shipping_tc'];
if(!isTcKimlik($tcno))
wc_add_notice( __( 'Lütfen Geçerli TC Kimlik No Girin.' ), 'error' );
}
/** Admin Sipariş Detayında Fatura Bilgilerinde TC No'yu Görebilmesi İçin**/

add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('TC Kimlik No').':</strong> ' . get_post_meta( $order->get_id(), '_shipping_tc', true ) . '</p>';
}
add_filter( 'woocommerce_checkout_fields', 'misha_email_first' );
 
function misha_email_first( $checkout_fields ) {
    $checkout_fields['billing']['shipping_tc']['priority'] = 20;
    return $checkout_fields;
}





// Custom function to display the Billing Address form to registration page
add_action('woocommerce_register_form_start','zk_add_billing_form_to_registration');
function zk_add_billing_form_to_registration(){
    $checkout = WC()->checkout;
    foreach ( $checkout->get_checkout_fields( 'billing' ) as $key => $field ) :
        if($key!='billing_email')
            woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
    endforeach;
}

// Custom function to save Usermeta or Billing Address of registered user
add_action('woocommerce_created_customer','zk_save_billing_address');
function zk_save_billing_address($user_id){
    $address = $_POST;
    foreach ($address as $key => $field){
        // Only billing fields values
        if( strpos( $key, 'billing_' ) !== false ){
            // Condition to add firstname and last name to user meta table
            if($key == 'billing_first_name' || $key == 'billing_last_name'){
                $new_key = str_replace( 'billing_', '', $key );
                update_user_meta( $user_id, $new_key, $_POST[$key] );
            }
            update_user_meta( $user_id, $key, $_POST[$key] );
        }
}
}
add_action( 'woocommerce_checkout_update_user_meta', 'reigel_woocommerce_checkout_update_user_meta', 10, 2 );

// Checking & validation of the additional fields in registration form.
add_action('woocommerce_register_post','zk_validation_billing_address', 10, 3 );
function zk_validation_billing_address( $username, $email, $validation_errors ){
    foreach ($_POST as $key => $field) :
        // Validation: Required fields
        if( strpos( $key, 'billing_' ) !== false ){
            if($key == 'billing_country' && empty($field) ){
                $validation_errors->add( $key.'_error',  __( 'Lütfen ülke seçimi yapınız.', 'woocommerce' ));
            }
            if($key == 'billing_first_name' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Adınızı giriniz.', 'woocommerce' ) );
            }
            if($key == 'billing_last_name' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Soyadınızı giriniz.', 'woocommerce' ) );
            }
            if($key == 'billing_address_1' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Adresinizi giriniz.', 'woocommerce' ) );
            }
            if($key == 'billing_city' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Lütfen şehir seçimi yapınız.', 'woocommerce' ) );
            }
            if($key == 'billing_state' && empty($field) ){
                if(count( WC()->countries->get_states($_POST['billing_country']) ) > 0)
                    $validation_errors->add( $key.'_error', __( 'Mahalle girişi yapınız.', 'woocommerce' ) );
            }
            if($key == 'billing_postcode' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Posta kodu girişi yapınız.', 'woocommerce' ) );
            }
            /*
            if($key == 'billing_email' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter billing email address.', 'woocommerce' ) );
            }
            */
            if($key == 'billing_phone' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Lütfen telefon numaranızı giriniz.', 'woocommerce' ) );
            }
            if($key == 'shipping_tc' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'TC.', 'woocommerce' ) );
            }


        }
    endforeach;
}

add_filter( 'woocommerce_billing_fields', 'sv_required_billing_fields' );
function sv_required_billing_fields( $fields ) {
    $fields['billing_phone']['required'] = true;
    $fields['billing_city']['required'] = true;
    $fields['billing_country']['required'] = true;
    $fields['billing_address_1']['required'] = true;
    return $fields;
}

最佳答案

根据您的其他问题回答 Save WooCommerce checkout custom field as user meta data ,即解决将字段数据保存为用户元数据和订单元数据。

现在要进行字段验证,您将使用:

function is_billing_identifier_valid( $tc ){
    if (strlen($tc) < 11 || $tc[0] == '0') 
        return false;
    
    $plus  = ($tc[0] + $tc[2] + $tc[4] + $tc[6] + $tc[8]) * 7;
    $minus = $plus - ($tc[1] + $tc[3] + $tc[5] + $tc[7]);
    $all   = ''; 
    
    if ( $minus % 10 != $tc[9])
        return false;
    
    for ($i = 0 ; $i < 10 ; $i++) 
        $all += $tc[$i];
    
    return $all % 10 != $tc[10] ? false : true;
}

add_action('woocommerce_checkout_process', 'custom_checkout_field_validation');
function custom_checkout_field_validation() {
    if( isset($_POST['billing_identifier']) && ! is_billing_identifier_valid( esc_attr($_POST['billing_identifier']) ) ) {
        wc_add_notice( __( 'Lütfen Geçerli TC Kimlik No Girin.' ), 'error' );
    }
}

代码进入事件子主题(或事件主题)的 functions.php 文件。它应该有效。

关于php - WooCommerce 结帐自定义字段条件验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66968303/

相关文章:

php - 我应该使用 php 邮件功能还是 phpmailer?

php - 如何使用一个存储过程从单独的表中返回 mysql 列?

wordpress - 图片上传的重力形式预览

validation - 如何在 textformfield onchange 值上修剪文本

javascript - 如果有人触摸输入框并将其留空,我如何使用 React Native 验证输入文本(已修改)

带有 IsEditable ="True"的 WPF ComboBox - 如何指示未找到匹配项?

javascript - 调用 Controller 并通过 onclick 监听器传递值

php - SQL Comment表插入语句

javascript - jquery 输入字段更改 wordpress 中的所有 php 循环项值

mysql - 将 WordPress 从 WAMP 上传到 GoDaddy 时出现数据库错误(无法本地访问网站)