【问题标题】:WooCommerce checkout custom field conditional validationWooCommerce 结帐自定义字段条件验证
【发布时间】:2021-07-02 05:12:33
【问题描述】:

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

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

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

/** 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;
}

【问题讨论】:

    标签: php wordpress validation woocommerce checkout


    【解决方案1】:

    根据你的其他问题回答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 文件中。它应该可以工作。

    【讨论】:

    • 如此简单,就像一种享受
    猜你喜欢
    • 2015-04-20
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 2023-04-10
    • 2018-11-22
    相关资源
    最近更新 更多