簡體   English   中英

僅在 WooCommerce 結帳中為新用戶添加驗證電子郵件地址

[英]Add a verification email address for new users only in WooCommerce checkout

我想向 WooCommerce 結帳頁面添加電子郵件地址驗證字段,但僅適用於新用戶。 因此,如果用戶已經登錄,則不應要求他們提供驗證電子郵件地址字段。

我已將此代碼添加到我的functions.php 並且效果很好。 它要求提供電子郵件地址,但它也為已經登錄的用戶執行此操作。

add_filter( 'woocommerce_checkout_fields', 'bbloomer_add_email_verification_field_checkout' );

function bbloomer_add_email_verification_field_checkout( $fields ) {

    $fields['billing']['billing_email']['class'] = array('form-row-first');

    $fields['billing']['billing_em_ver'] = array(
        'label'     => __('Email Address Verification', 'bbloomer'),
        'required'  => true,
        'class'     => array('form-row-last'),
    'clear'     => true
    );

    return $fields;
}

// ---------------------------------
// 3) Generate error message if field values are different

add_action( 'woocommerce_checkout_process', 'bbloomer_matching_email_addresses' );
function bbloomer_matching_email_addresses() { 
    $email1 = $_POST['billing_email'];
    $email2 = $_POST['billing_em_ver'];
    if ( $email2 !== $email1 ) {
        wc_add_notice( __( 'Your email addresses do not match', 'bbloomer' ), 
 'error' );
    }
}

如何使該字段僅對非登錄用戶顯示?

我對您的代碼進行了一些更改,並添加了必要條件以使其僅適用於非登錄用戶:

// Add or change Woocommerce Checkout fields
add_filter( 'woocommerce_checkout_fields', 'add_billing_email_check_field', 20, 1 );
function add_billing_email_check_field( $fields ) {
    // Only for non logged in users
    if( is_user_logged_in() ) return $fields;

    $fields['billing']['billing_email']['class'] = array('form-row-first');

    $fields['billing']['billing_email_check'] = array(
        'label'     => __('Email Address Verification', 'woocommerce'),
        'required'  => true,
        'class'     => array('form-row-last'),
        'clear'     => true
    );

    return $fields;
}

// Billing email validation check error message
add_action( 'woocommerce_checkout_process', 'billing_email_validation_check' );
function billing_email_validation_check() {
    if ( isset( $_POST['billing_email'] ) && isset( $_POST['billing_email_check'] ) &&
        $_POST['billing_email'] !== $_POST['billing_email_check'] )
        wc_add_notice( __( 'Your <strong>billing email address</strong> does not match.', 'woocommerce' ), 'error' );
}

代碼位於活動子主題(或活動主題)的 function.php 文件中。 測試和工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM