簡體   English   中英

僅允許在我的帳戶上編輯每個客戶一次自定義字段 > 在 WooCommerce 中編輯帳戶

[英]Only allow edit of custom field once per customer on My account > edit account in WooCommerce

我在 WooCommerce My account部分Account details中添加了一個 DOB 字段。 該字段是日期字段(出生日期)。

客戶可以編輯和保存。 問題是; 客戶可以一遍又一遍地編輯和保存。

這是一個問題,因為我想根據 DOB 日期自動應用折扣。 如果客戶可以多次編輯該字段; 他們每天都有可能獲得折扣。 由於顯而易見的原因,這不可能發生。

我需要幫助使自定義字段可編輯一次,並且每個客戶只能編輯一次。 在 wp-admin 中,管理員可以根據需要進行編輯——這沒關系。

到目前為止,這是我的完整代碼:

add_action( 'woocommerce_edit_account_form', 'dob_on_myaccount_form' );
function dob_on_myaccount_form() {

    woocommerce_form_field( 'birthday_field', array (
    'type' => 'date',
    // how do I set the format to be y-m-d as in Year, Month, Day here
    'label' => __('Date of birth', 'woocommerce' ),
    'required' => false,
    ), get_user_meta( get_current_user_id(), 'birthday_field', true ) );
}

add_action( 'woocommerce_save_account_details_errors', 'dob_on_myaccount_form_error', 10, 1 );
function dob_on_myaccount_form_error( $args ) {

    if (isset( $_POST['birthday_field'] ) && empty( $_POST['birthday_field'] ) ) {

        $args->add('error', __( 'Please provide a date of birth', 'woocommerce' ) );
    }
}

add_action( 'woocommerce_save_account_details', 'dob_on_myaccount_form_save', 10, 1 );
function dob_on_myaccount_form_save( $user_id ) {

    if ( isset( $_POST['birthday_field'] ) && !empty( $_POST['birthday_field'] ) ) {

        update_user_meta( $user_id, 'birthday_field', sanitize_text_field( $_POST['birthday_field'] ) );
    }
}

add_action( 'woocommerce_cart_calculate_fees', 'give_birthday_discount', 10, 2 );
function give_birthday_discount( $cart, $date = 'now' ) {

    if ( 'now' === $date ) {

    $date = date( 'Y-m-d' );

    $discount_percentage = 10;

    $cart->add_fee( __( 'Birthday Discount', 'woocommerce' ), -( $cart->subtotal * $discount_percentage / 100 ));
    }
}

add_action( 'show_user_profile', 'dob_on_admin_profile', 10, 1 );
add_action( 'edit_user_profile', 'dob_on_admin_profile', 10, 1 );
function dob_on_admin_profile( $user ) { ?>

    <h3><?php _e('Birthday','woocommerce'); ?></h3>

    <table class="form-table">

        <tr>
            <th><label for="birthday_field"><?php _e('Date of Birth', 'woocommerce'); ?></label></th>
            <td><input type="date" name="birthday_field" value="<?php echo esc_attr(get_the_author_meta('birthday_field', $user->ID)); ?>" class="regular-text" /></td>
        </tr>

    </table>
    <br />
<?php
}

add_action( 'personal_options_update', 'dob_on_admin_profile_save', 10, 1 );
add_action( 'edit_user_profile_update', 'dob_on_admin_profile_save', 10, 1 );
function dob_on_admin_profile_save( $user_id ) {

    if ( ! empty( $_POST['birthday_field'] ) ) {
    
        update_user_meta( $user_id, 'birthday_field', sanitize_text_field( $_POST['birthday_field'] ) );
    }
}

在保存日期字段時,您可以保存一個額外的值birthday_field_not_editable

然后您可以檢查此值是否為true ,如果是這種情況,則該字段不再可編輯(只讀 = true)。

通過添加到代碼中的注釋標簽進行解釋

// Add field - my account
function dob_on_myaccount_form() {
    // Label
    $label = __( 'Date of birth', 'woocommerce' );
    
    // Readonly (by default false)
    $readonly = false;
    
    // Get current user id
    $user_id = get_current_user_id();
    
    // Get value
    $not_editable = get_user_meta( $user_id, 'birthday_field_not_editable', true );
    
    // If TRUE
    if ( $not_editable ) {
        $label = __( 'Date of birth - adjustment is no longer possible', 'woocommerce' );
        $readonly = true;
    }
    
    // Date field
    woocommerce_form_field( 'birthday_field', array (
        'type'              => 'date',
        'label'             => $label,
        'required'          => true,
        'custom_attributes' => array( 'readonly' => $readonly ),
    ), get_user_meta( $user_id, 'birthday_field', true ) );
}
add_action( 'woocommerce_edit_account_form', 'dob_on_myaccount_form', 10, 0 );

// Validate - my account
function dob_on_myaccount_form_error( $args ) {
    if ( isset( $_POST['birthday_field'] ) && empty( $_POST['birthday_field'] ) ) {
        $args->add('error', __( 'Please provide a date of birth', 'woocommerce' ) );
    }
}
add_action( 'woocommerce_save_account_details_errors', 'dob_on_myaccount_form_error', 10, 1 );

// Save - my account
function dob_on_myaccount_form_save( $user_id ) {
    if ( isset( $_POST['birthday_field'] ) && ! empty( $_POST['birthday_field'] ) ) {
        // Update birthday field
        update_user_meta( $user_id, 'birthday_field', sanitize_text_field( $_POST['birthday_field'] ) );
        // Update not editable field
        update_user_meta( $user_id, 'birthday_field_not_editable', true );
    }
}
add_action( 'woocommerce_save_account_details', 'dob_on_myaccount_form_save', 10, 1 );

暫無
暫無

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

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