WooCommerce管理员的“新订单”页面将自定义字段(billing_birth_date)保存到自定义字段(billing_birth_date)中的“ edit-user.php”页面

时间:2020-03-25 14:05:32

标签: php wordpress woocommerce

我希望已在“ user-edit.php”页面中设置/保存已添加到“新订单” woocommerce管理页面的自定义字段。该字段显示,并且在提交输入日期后也显示。

所有这些代码都在我的functions.php文件中。

这是我在woocommerce“新订单”页面上显示字段的代码:

add_filter('woocommerce_admin_billing_fields', 'add_woocommerce_admin_billing_fields');
function add_woocommerce_admin_billing_fields($billing_fields) {
    // Remove unnecessary fields
    unset($billing_fields['country']);
    unset($billing_fields['company']);
    unset($billing_fields['address_1']);
    unset($billing_fields['address_2']);
    unset($billing_fields['city']);
    unset($billing_fields['postcode']);
    unset($billing_fields['state']);

    // Add custom field
    $billing_fields['birth_date'] = array(
        'type' => 'date',
        'label' => __('Geboortedatum'),
        'class' => 'form-field-wide',
        'priority' => 25,
        'required' => true,
        'clear' => true,
    );
    return $billing_fields;
}

使用此代码,该字段将显示在“ edit-user.php”页面上:

add_action( 'edit_user_profile', 'custom_user_profile_fields' );
add_action( 'show_user_profile', 'custom_user_profile_fields' );
function custom_user_profile_fields($user) { ?>
    <table class="form-table" role="presentation">
        <tr class="form-field form-required">
            <th scope="row">
                <label for="billing_birth_date">
                    <?php _e('Geboortedatum');
                    if (empty(esc_attr( get_the_author_meta( 'billing_birth_date', $user->ID ) ))) { ?>
                        <span class="description"><?php _e('(required)') ?></span>
                    <?php }?>
                </label>
            </th>
            <td>
                <?php if (empty(esc_attr( get_the_author_meta( 'billing_birth_date', $user->ID ) ))) { ?>
                    <input type="date" class="regular-text" id="birthday" name="billing_birth_date" value="<?php echo esc_attr( get_the_author_meta( 'billing_birth_date', $user->ID ) ); ?>"/>
                <?php }
                else { ?>
                    <input type="date" class="regular-text" id="birthday" name="billing_birth_date" value="<?php echo esc_attr( get_the_author_meta( 'billing_birth_date', $user->ID ) ); ?>" disabled="disabled" />
                    <span class="description">Geboortedatums kunnen niet worden veranderd.</span>
                <?php }?>
            </td>
        </tr>
    </table>
    <?php
}

使用此代码,将保存“ edit-user.php”页面上的自定义字段:

function userMetaBirthdaySave($userId) {
    if(empty(esc_attr( get_the_author_meta( 'billing_birth_date', $userId ) ))) {
        if (!current_user_can('edit_user', $userId)) {
            return;
        }

        update_user_meta($userId, 'billing_birth_date', $_POST['billing_birth_date']);
    }

}
add_action('personal_options_update', 'userMetaBirthdaySave');
add_action('edit_user_profile_update', 'userMetaBirthdaySave');

那么如何将“新订单”页面上的自定义字段的值设置为“编辑用户”页面上的自定义字段?

0 个答案:

没有答案