将字段添加到 Woocommerce 注册表单并在管理编辑用户中

2023-12-31

在 WooCommerce 中,我在帐户注册表单中添加了额外的字段:

// 1. ADD FIELDS
add_action( 'woocommerce_register_form_start', 'add_woo_account_registration_field' );
function add_woo_account_registration_field() {
    ?>

    <p class="form-row form-row-first">
    <label for="reg_billing_account_number"><?php _e( 'Ship to/ Account number', 'woocommerce' ); ?> <span class="required">*</span></label>
    <input type="text" class="input-text" name="billing_account_number" id="reg_billing_account_number" value="<?php if ( ! empty( $_POST['billing_account_number'] ) ) esc_attr_e( $_POST['billing_account_number'] ); ?>" />
    </p>

    <div class="clear"></div>

    <?php
}

// 2. VALIDATE FIELDS
add_filter( 'woocommerce_registration_errors', 'validate_woo_account_fields', 10 );
function validate_woo_account_fields( $errors, $username, $email ) {
    if ( isset( $_POST['billing_account_name'] ) && empty( $_POST['billing_account_number'] ) ) {
        $errors->add( 'billing_account_number_error', __( '<strong>Error</strong>: account number is required!', 'woocommerce' ) );
        $fields['billing_account_number']['maxlength'] = 6;
    }
    return $errors;
}

// 3. SAVE FIELDS
add_action( 'woocommerce_created_customer', 'save_woo_account_fields' );

function save_woo_account_fields( $customer_id ) {
    if ( isset( $_POST['billing_account_number'] ) ) {
        update_user_meta( $customer_id, 'billing_account_number', sanitize_text_field( $_POST['billing_account_number'] ) );
        update_user_meta( $customer_id, 'account_number', sanitize_text_field($_POST['billing_account_number']) );
    }
}

But 字段验证不起作用因为用户仍然可以在不填写的情况下进行注册。
我做错了什么?

此外,当客户注册时,我希望在管理员用户页面上看到此字段。

任何帮助表示赞赏。


以下将在客户注册中添加“计费帐号”字段作为必填字段(验证)并显示它:

  • 在我的帐户 > 编辑帐户部分
  • 在管理员用户编辑页面的计费字段部分

代码:

// Display a field in Registration / Edit account
add_action( 'woocommerce_register_form_start', 'display_account_registration_field' );
add_action( 'woocommerce_edit_account_form_start', 'display_account_registration_field' );
function display_account_registration_field() {
    $user  = wp_get_current_user();
    $value = isset($_POST['billing_account_number']) ? esc_attr($_POST['billing_account_number']) : $user->billing_account_number;
    ?>
    <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
    <label for="reg_billing_account_number"><?php _e( 'Ship to/ Account number', 'woocommerce' ); ?> <span class="required">*</span></label>
    <input type="text" maxlength="6" class="input-text" name="billing_account_number" id="reg_billing_account_number" value="<?php echo $value ?>" />
    </p>
    <div class="clear"></div>
    <?php
}

// registration Field validation
add_filter( 'woocommerce_registration_errors', 'account_registration_field_validation', 10, 3 );
function account_registration_field_validation( $errors, $username, $email ) {
    if ( isset( $_POST['billing_account_number'] ) && empty( $_POST['billing_account_number'] ) ) {
        $errors->add( 'billing_account_number_error', __( '<strong>Error</strong>: account number is required!', 'woocommerce' ) );
    }
    return $errors;
}

// Save registration Field value
add_action( 'woocommerce_created_customer', 'save_account_registration_field' );
function save_account_registration_field( $customer_id ) {
    if ( isset( $_POST['billing_account_number'] ) ) {
        update_user_meta( $customer_id, 'billing_account_number', sanitize_text_field( $_POST['billing_account_number'] ) );
    }
}

// Save Field value in Edit account
add_action( 'woocommerce_save_account_details', 'save_my_account_billing_account_number', 10, 1 );
function save_my_account_billing_account_number( $user_id ) {
    if( isset( $_POST['billing_account_number'] ) )
        update_user_meta( $user_id, 'billing_account_number', sanitize_text_field( $_POST['billing_account_number'] ) );
}

// Display field in admin user billing fields section
add_filter( 'woocommerce_customer_meta_fields', 'admin_user_custom_billing_field', 10, 1 );
function admin_user_custom_billing_field( $args ) {
    $args['billing']['fields']['billing_account_number'] = array(
        'label'         => __( 'Ship to/ Account number', 'woocommerce' ),
        'description'   => '',
        'custom_attributes'   => array('maxlength' => 6),
    );
    return $args;
}

代码位于活动子主题(活动主题)的 function.php 文件中。经过测试并有效。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将字段添加到 Woocommerce 注册表单并在管理编辑用户中 的相关文章

  • 如何在 HTML / Javascript 页面中插入 PHP 下拉列表

    好吧 这是我的第二篇文章 请接受我是一个完全的新手 愿意学习 花了很多时间在各个网站上寻找答案 而且我几乎已经到达了我需要到达的地方 至少在这一点上 我有一个网页 其中有许多 javascript 函数 这些函数一起使用 google 地图
  • 如何在没有引用的情况下复制对象?

    PHP5 OOP 有据可查对象通过引用传递 http php net manual en language oop5 references php默认情况下 如果这是默认的 在我看来 有一种非默认的方式可以在没有参考的情况下进行复制 如何
  • 如何在html中制作多行类型的文本框?

  • 是 ValidationFailed 默认值

    会用什么方法facesContext isValidationFailed 当验证失败时 在渲染响应阶段后返回 它返回false对于我来说 它只会返回true when FacesContext validationFailed http
  • 如何使用 Google 帐户对我们网站中的用户进行身份验证

    如何在我们的网站中使用 Google 帐户对用户进行身份验证 我希望用户重定向到谷歌登录页面 然后将他重定向到我的网站 我想要这个 PHP 实现 你要OAuth http code google com apis accounts docs
  • PHP 和 NLP:嵌套括号(解析器输出)到数组?

    想要将带有嵌套括号的文本转换为嵌套数组 以下是 NLP 解析器的输出示例 TOP S NP PRP I VP VBP love NP NP DT a JJ big NN bed PP IN of NP NNS roses 原文 我喜欢一大床
  • 在 PHP 中模拟 jQuery.ajax 请求

    我必须在 PHP 中模拟 AJAX 请求 就像在 jQuery 中一样 我当前的代码在这里 原始 AJAX 调用 不得修改 ajax type POST url someFile php data data success function
  • 文件修改时间检查的成本

    对于Linux下包含少量字节的文件 我只需要处理自上次处理以来发生更改的时间 我通过调用 PHP 检查文件是否被更改clearstatcache filemtime 定期 由于整个文件总是很小 因此删除对 filemtime 的调用并通过将
  • 如何将粘在一起的单词分开?

    我有很多命名不好的文件 videoofmegoingtoschool avi 是否有一个库或某种算法可以正确地将其分离 video of me going to school avi 我不认为那里有什么 我可以想象一个程序 它使用单词词典并
  • PHP 基本身份验证 file_get_contents() [重复]

    这个问题在这里已经有答案了 我需要从网站解析一些 XML 数据 XML 数据是原始格式 但在我需要进行身份验证之前 基于基本网络服务器的身份验证 使用用户名和密码 I tried homepage file get contents htt
  • 尝试获取 Google accessToken

    看起来 无论我做什么 谷歌都在竭尽全力阻止我完成这个研究项目 我的项目让我使用 Google 电子表格作为数据库 并使用所述电子表格中的数据执行程序化的 Google 图片搜索 并向最终用户显示一些结果 设置说明 我开始按照此处的说明进行操
  • 模拟/存根在 PHPUnit 中实现 arrayaccess 的类的对象

    这是我正在为其编写测试套件的类的构造函数 它扩展了 mysqli function construct Config c store config file this gt config c do mysqli constructor pa
  • 删除 woocommerce 店面主页标题 php

    我正在使用 woocommerce 的店面主题 我需要用 php 删除主页标题 h1 我知道 css 解决方案 但我不想使用它 因为我想将 h1 添加到该页面的其他位置 并且在一个页面中包含 2 个 h1 对 seo 不利页 我也知道删除页
  • 重新排列数组键 php [重复]

    这个问题在这里已经有答案了 我有这个数组 Array 15 gt 13 1 16 gt Mark one answer 19 gt You see a car on the hard shoulder of a motorway with
  • 如何解决 Laravel 8 UI 分页问题?

    我在尝试最近发布的 laravel 8 时遇到了问题 我试图找出变化是什么以及它是如何工作的 当我这样做时 我遇到了分页 laravel 8 UI 变得混乱的问题 不知何故它发生了 有人可以帮助我吗 或者经历过同样的事情 像这样我在 lar
  • phpinfo 说 php.ini 路径是 C:\Windows 但那里没有 php.ini

    我们正在尝试从 PHP5 切换到 PHP7 现在我们已经安装了 Apache 并且 PHP 可以运行了 然而 我们在php ini文件没有任何作用 Via phpinfo 我们意识到原因是Configuration File php ini
  • UTF-8、PHP、Win7 - 现在是否有解决方案可以使用 php 在 Win 7 上保存 UTF-8 文件名?

    更新 只是为了不让您阅读所有内容 PHP 开头 7 1 0alpha2 在 Windows 上支持 UTF 8 文件名 感谢阿纳托尔 贝尔斯基 根据 stackoverflow 上的一些链接 我找到了部分答案 https stackover
  • 更改API数据输出的布局

    我是 API 集成和 PHP 的新手 我最近将 VIN 解码器集成到我的应用程序中 在输入框中输入车辆的 VIN 选择提交 然后就会显示 API 数据库中有关该车辆的所有信息 数据存储为关联数组 其中包含类别及其相应元素 例如 对于 VIN
  • HTML 代码中的 PHP [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我用 HTML 代码编写了 PHP div div 但这出现在输出页面中 else print 我怎样才能让PHP执行 你的文件有一个 p
  • 显式删除会话cookie会产生什么影响?

    我使用 php session 来维护用户的会话 Session 在登录后创建 在注销或超时后销毁 我需要管理面板中的一个选项来强制注销任何用户 如果他在网站上处于活动状态 我怎样才能做到这一点 我正在考虑删除临时会话文件 这应该有效地破坏

随机推荐