【问题标题】:Wordpress - add woocommerce billing_phone field into user-new.php and save it into database and my accountWordpress - 将 woocommerce billing_phone 字段添加到 user-new.php 并将其保存到数据库和我的帐户中
【发布时间】:2021-06-01 17:19:01
【问题描述】:

我遇到了一个问题。我需要从 wp-admin 手动创建一些 woocommerce 客户角色帐户。我需要做的是在表单中添加一个 woocommerce billing_phone 号码,该号码将保存到数据库中并自动添加到 woocommerce 我的帐户电话字段中。我试过用下面的代码来做,但没有帮助。有什么想法我该怎么做?

我需要将它添加到 user-new.php ...我知道在创建用户帐户后可以添加 billing_phone,但我想通过搜索帐户并执行两次操作来节省时间,所以我希望通过从 wp-admin 直接将此字段添加到帐户创建过程中来节省时间。

提前谢谢你!

我使用但没有帮助的代码:

add_action( 'user_new_form', function( $type ){
    if( 'add-new-user' !== $type )
        return;
    echo '<input type="text" name="billing_phone" id="billing_phone" value=""/>';
});

【问题讨论】:

  • 您希望什么时候发生这种情况?或者您是否正在尝试创建一个可以根据您的要求运行并将所有电话号码与 bliing_phone_number 同步的功能?这从你的问题中不清楚。 woocommerce 中也没有默认电话号码字段。您是否为它创建了自定义用户字段?如果是,它的meta_key 是什么?
  • 不,我在我的网站上使用 woocommerce。我们使用 woocommerce 预订插件向客户销售我们的疗法。如果客户通过电子邮件订购或致电我们的沙龙,我们手动创建他的帐户,然后我们将他的帐户分配到我们创建的订单中。所以我们需要添加一个字段,我们将在其中添加客户电话,这个电话号码将作为电话号码出现在 woocommerce 账单详细信息中。
  • 我需要在您在 wordpress 管理员中创建帐户时发生这种情况 - > 正如我在问题中提到的,它的页面名为 user-new.php
  • Wp-admin -> 用户 -> 添加新用户(这里会发生)
  • 因此,您只需要一个字段,您可以在其中移动客户号码,当您为他/她创建订单时,您希望该电话号码自动插入到 billing_mobile_field 中,就像使用电子邮件 ID 一样?正确吗?

标签: php wordpress woocommerce


【解决方案1】:

这实际上很容易做到。因此,WooCommerce 自动填充这些字段的方式是获取用户的实际账单和送货地址。如果我们设法将用户的联系电话保存在 regster hook 上,我们就可以完成这项工作。

我们将首先创建一个字段来保存移动数据。

add_action( 'user_new_form', 'bks_add_mobile_field' );
add_action( 'edit_user_profile', 'bks_add_mobile_field' );
add_action( 'show_user_profile', 'bks_add_mobile_field' );

function bks_add_mobile_field( $user ) { ?>
    <h3><?php _e("Extra profile information", "blank"); ?></h3>

    <table class="form-table">
        <tr>
            <th><label for="mobile"><?php _e("Mobile Number"); ?></label></th>
            <td>
                <input type="text" name="mobile" id="mobile" value="<?php echo esc_attr( get_the_author_meta( 'mobile', $user->ID ) ); ?>" class="regular-text" /><br />
                <span class="description"><?php _e("Please enter mobile."); ?></span>
            </td>
        </tr>
    </table>
<?php }

现在我们将把这个手机保存在mobile和billing_mobile这两个字段中。

add_action( 'user_register', 'bks_save_mobile_field' );

function bks_save_mobile_field($user_id) {
    // You can maybe add checks here whch would determine if the users role is customer 
    // or not or maybe validate the number. 
    if ( isset( $_POST['mobile'] ) ) {
        update_user_meta($user_id, 'mobile', $_POST['mobile']);
        update_user_meta($user_id, 'billing_phone', $_POST['mobile']);
    }
}

因此,将所有内容放在一起,将以下代码添加到您的 functions.php 文件中。

add_action( 'user_new_form', 'bks_add_mobile_field' );
add_action( 'show_user_profile', 'bks_add_mobile_field' );
add_action( 'edit_user_profile', 'bks_add_mobile_field' );

function bks_add_mobile_field( $user ) { ?>
    <h3><?php _e("Extra profile information", "blank"); ?></h3>

    <table class="form-table">
        <tr>
            <th><label for="mobile"><?php _e("Mobile Number"); ?></label></th>
            <td>
                <input type="text" name="mobile" id="mobile" value="<?php echo esc_attr( get_the_author_meta( 'mobile', $user->ID ) ); ?>" class="regular-text" /><br />
                <span class="description"><?php _e("Please enter mobile."); ?></span>
            </td>
        </tr>
    </table>
<?php }


add_action( 'user_register', 'bks_save_mobile_field' );

function bks_save_mobile_field($user_id) {
    // You can maybe add checks here whch would determine if the users role is customer 
    // or not or maybe validate the number. 
    if ( isset( $_POST['mobile'] ) ) {
        update_user_meta($user_id, 'mobile', $_POST['mobile']);
        update_user_meta($user_id, 'billing_phone', $_POST['mobile']);
    }
}

此代码已经过测试并且可以正常工作。

【讨论】:

  • 有没有办法编辑此代码并将所有默认的 woocommerce 计费字段包含到 user-new.php 页面中?原因是,计费国家和计费状态是 woocommerce 中的动态字段。
  • @MubasharAftab,我认为你可以做到,但是你必须创建很多重复的字段。并使用重复字段更新其他“WooCommerce”字段。
猜你喜欢
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 2016-03-23
  • 1970-01-01
  • 2015-03-17
  • 2021-05-30
  • 2023-03-11
相关资源
最近更新 更多