【问题标题】:Only allow edit of custom field once per customer on My account > edit account in WooCommerce仅允许在我的帐户上的每个客户编辑自定义字段一次 > 在 WooCommerce 中编辑帐户
【发布时间】:2020-12-30 11:16:01
【问题描述】:

我在 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'] ) );
    }
}

【问题讨论】:

    标签: php wordpress woocommerce readonly custom-fields


    【解决方案1】:

    在保存日期字段时,您可以保存一个额外的值birthday_field_not_editable

    然后你可以检查这个值是否为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 );
    

    【讨论】:

    • 哇-谢谢。我所做的只是对if ( $not_editable ) { 进行了一次小调整,然后将其更改为if ( $not_editable &amp;&amp; !current_user_can('administrator') ) {,以便在前端也可以进行相同的编辑。再次,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2019-06-17
    • 1970-01-01
    相关资源
    最近更新 更多