每个客户只能在“我的帐户”>“在WooCommerce中编辑帐户”上编辑一次自定义字段

哈罗德·奥尔德森

我在WooCommerceMy account部分添加了一个DOB字段Account details该字段是日期字段(出生日期)。

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

这是一个问题,因为我想根据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'] ) );
    }
}
7uc1f3r

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

然后,您可以检查该值是否为true,如果是这种情况,则该字段不再可编辑(readonly = 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 );

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Woocommerce编辑帐户页面中添加自定义字段

隐藏自定义结帐字段,使其不显示在WooCommerce我的帐户编辑地址中

在Woocommerce编辑帐户页面中添加其他自定义字段

在Woocommerce编辑个人资料“我的帐户”页面中提供自定义注册字段

在Woocommerce 3中自定义我的帐户地址字段

在Woocommerce中的我的帐户>编辑帐户上添加手机字段

Woocommerce管理员中的自定义可编辑字段将订单页面编辑到每个项目中

从Woocommerce 3.4+中“我的帐户”编辑地址上的字段中删除(可选)文本

在Woocommerce中将我的帐户编辑帐户字段设为只读

我只能在linqpad中为每个进程运行一次功能吗?

magento中的客户注册表格编辑(编辑时自定义字段不会更新)

Keycloak:从“编辑帐户”页面更新自定义用户属性

在我的帐户上添加带有验证的单选按钮 > 在 WooCommerce 中编辑帐户

使用户能够在MVC中编辑自定义帐户设置

在WooCommerce我的帐户编辑地址中将字段设为可选

从我在WooCommerce中的帐户编辑地址中删除计费电话和电子邮件字段

从WooCommerce的“客户”角色隐藏我的帐户中充值的帐户资金

使我的自定义视图更新编辑器中的文本字段

在WooCommerce中编辑我的帐户订单视图页面

根据Woocommerce中的用户角色自定义客户新帐户邮件

自定义功能在Midnight Commander中为“编辑”

自定义订单状态未显示在客户我的帐户订单历史记录中

在WooCommerce我的帐户自定义菜单项内容中定义用户变量

绕过 WooCommerce 我的帐户登录以进行自定义登录

如何自定义woocommerce“我的帐户”页面?

滑动自定义分页只能滑动一次

需要确保用户帐户一次只能在一台计算机上登录的建议

在 Woocommerce 3 中显示自定义帐户菜单项的自定义内容

如何在 Woocommerce 我的帐户侧边栏中添加新的自定义标题?