在“我的帐户"上,每个客户只能编辑一次自定义字段>在WooCommerce中编辑帐户 [英] Only allow edit of custom field once per customer on My account > edit account in WooCommerce

查看:54
本文介绍了在“我的帐户"上,每个客户只能编辑一次自定义字段>在WooCommerce中编辑帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在WooCommerce 我的帐户部分帐户详细信息中添加了一个DOB字段.该字段是日期字段(出生日期).

I have added a DOB field to the WooCommerce My account section Account details. The field is a date field (date of birth).

客户可以编辑和保存.问题是;客户可以一遍又一遍地进行编辑和保存.

The customer can edit and save. Problem is; the customer can edit and save over and over again.

这是一个问题,因为我想根据DOB日期自动申请折扣.客户是否可以多次编辑此字段;他们可能每天都会获得折扣.出于明显的原因,这不可能发生.

This is a problem because of the discount that I would like to automatically apply based on the DOB date. If the customer can edit this field more than once; they can potentially get a discount each and every day. For obvious reasons, this cannot happen.

在使自定义字段可编辑一次且每个客户仅一次时,我需要帮助.在wp-admin中,管理员可以根据自己的需要进行编辑-可以.

I need help in making the custom field editable ONCE and only once per customer. In wp-admin, admins can edit as they see fit - which is ok.

到目前为止,这是我的完整代码:

Here is my full code so far:

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

While saving the date field you can save an extra value birthday_field_not_editable

然后您可以检查此值是否为 true ,如果是这种情况,则该字段不再可编辑(只读= true).

Then you can check if this value is true, if this is the case, the field is no longer editable (readonly = true).

通过添加到代码中的注释标签进行解释

Explanation via comment tags added to the code

// 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 );

这篇关于在“我的帐户"上,每个客户只能编辑一次自定义字段&gt;在WooCommerce中编辑帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆