用户名是通过 WooCommerce 注册中的账单全名自动生成的

2023-12-14

因此,我在 WooCommerce 网上商店的注册页面中添加了各种自定义字段,如下所示:

/* ---------------------- Registration page ----------------------- */
//Add extra fields in registration form
add_action('woocommerce_register_form_start','my_extra_register_fields');
function my_extra_register_fields(){?>
    <p class="woocommerce-FormRow form-row form-row-first">
        <label for="reg_billing_first_name"><?php _e('First Name','woocommerce'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if(! empty($_POST['billing_first_name'])) esc_attr_e($_POST['billing_first_name']); ?>"/>
    </p>
    <p class="woocommerce-FormRow form-row form-row-last">
        <label for="reg_billing_last_name"><?php _e('Last Name','woocommerce'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if(! empty($_POST['billing_last_name'])) esc_attr_e($_POST['billing_last_name']); ?>"/>
    </p>
    <div class="clearfix"></div>
    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
        <label for="reg_billing_company"><?php _e('Company Name','woocommerce'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_company" id="reg_billing_company" value="<?php if(! empty($_POST['billing_company'])) esc_attr_e($_POST['billing_company']); ?>"/>
    </p>
    <div class="clearfix"></div>
    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
        <label for="reg_billing_vat"><?php _e('VAT Number','woocommerce'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_vat" id="reg_billing_vat" value="<?php if(! empty($_POST['billing_vat'])) esc_attr_e($_POST['billing_vat']); ?>" maxlength="15" placeholder="Enter VAT Number"/>
    </p>
    <div class="clearfix"></div>
<?php
    wp_enqueue_script('wc-country-select');
    woocommerce_form_field('billing_country',array(
        'type'          => 'country',
        'class'         => array('chzn-drop'),
        'label'         => __('Country'),
        'placeholder'   => __('Choose your country.'),
        'required'      => true,
        'clear'         => true,
        'default'       => 'BE'
    ));
?>
    <p class="woocommerce-FormRow form-row form-row-first">
        <label for="reg_billing_postcode"><?php _e('Postcode / ZIP','woocommerce'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_postcode" id="reg_billing_postcode" value="<?php if(! empty($_POST['billing_postcode'])) esc_attr_e($_POST['billing_postcode']); ?>"/>
    </p>
    <p class="woocommerce-FormRow form-row form-row-last">
        <label for="reg_billing_city"><?php _e('Town / City','woocommerce'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_city" id="reg_billing_city" value="<?php if(! empty($_POST['billing_city'])) esc_attr_e($_POST['billing_city']); ?>"/>
    </p>
    <div class="clearfix"></div>
    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
        <label for="reg_billing_address_1"><?php _e('Address','woocommerce'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_address_1" id="reg_billing_address_1" value="<?php if(! empty($_POST['billing_address_1'])) esc_attr_e($_POST['billing_address_1']); ?>" placeholder="Street address"/>
    </p>
    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
        <input type="text" class="input-text" name="billing_address_2" id="reg_billing_address_2" value="<?php if(! empty($_POST['billing_address_2'])) esc_attr_e($_POST['billing_address_2']); ?>" placeholder="Apartment,suite,unit etc. (optional)"/>
    </p>
    <div class="clearfix"></div>
    <p class="woocommerce-FormRow woocommerce-FormRow--wide form-row form-row-wide">
        <label for="reg_billing_phone"><?php _e('Phone','woocommerce'); ?><span class="required">*</span></label>
        <input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php if(! empty($_POST['billing_phone'])) esc_attr_e($_POST['billing_phone']); ?>"/>
    </p>
    <div class="clearfix"></div>
<?php
}
//Registration form fields Validation
add_action('woocommerce_register_post','my_validate_extra_register_fields',10,3);
function my_validate_extra_register_fields($username,$email,$validation_errors){
    if(isset($_POST['billing_first_name']) && empty($_POST['billing_first_name'])){$validation_errors->add('billing_first_name_error',__('A first name is required!','woocommerce'));}
    if(isset($_POST['billing_last_name']) && empty($_POST['billing_last_name'])){$validation_errors->add('billing_last_name_error',__('A last name is required!','woocommerce'));}
    if(isset($_POST['billing_company']) && empty($_POST['billing_company'])){$validation_errors->add('billing_company_error',__('A Company name is required!','woocommerce'));}
    if(isset($_POST['billing_vat']) && empty($_POST['billing_vat'])){$validation_errors->add('billing_vat_error',__('VAT number is required!','woocommerce'));}
    if(isset($_POST['billing_country']) && empty($_POST['billing_country'])){$validation_errors->add('billing_country_error',__('A country is required!','woocommerce'));}
    if(isset($_POST['billing_city']) && empty($_POST['billing_city'])){$validation_errors->add('billing_city_error',__('A city is required!','woocommerce'));}
    if(isset($_POST['billing_postcode']) && empty($_POST['billing_postcode'])){$validation_errors->add('billing_postcode_error',__('A postcode is required!','woocommerce'));}
    if(isset($_POST['billing_state']) && empty($_POST['billing_state'])){$validation_errors->add('billing_state_error',__('A state is required!','woocommerce'));}
    if(isset($_POST['billing_address_1']) && empty($_POST['billing_address_1'])){$validation_errors->add('billing_address_1_error',__('An address is required!','woocommerce'));}
    if(isset($_POST['billing_phone']) && empty($_POST['billing_phone'])){$validation_errors->add('billing_phone_error',__('A phone number is required!','woocommerce'));}
    return $validation_errors;
}
//Save extra fields when new user registers
add_action('woocommerce_created_customer','my_save_extra_register_fields'); 
function my_save_extra_register_fields($customer_id){
    if(isset($_POST['billing_first_name'])){update_user_meta($customer_id,'first_name',sanitize_text_field($_POST['billing_first_name']));update_user_meta($customer_id,'billing_first_name',sanitize_text_field($_POST['billing_first_name']));}
    if(isset($_POST['billing_last_name'])){update_user_meta($customer_id,'last_name',sanitize_text_field($_POST['billing_last_name']));update_user_meta($customer_id,'billing_last_name',sanitize_text_field($_POST['billing_last_name']));}
    if(isset($_POST['billing_company'])){update_user_meta($customer_id,'billing_company',sanitize_text_field($_POST['billing_company']));}
    if(isset($_POST['billing_vat'])){update_user_meta($customer_id,'billing_vat',sanitize_text_field($_POST['billing_vat']));}
    if(isset($_POST['billing_country'])){update_user_meta($customer_id,'billing_country',sanitize_text_field($_POST['billing_country']));}
    if(isset($_POST['billing_city'])){update_user_meta($customer_id,'billing_city',sanitize_text_field($_POST['billing_city']));}
    if(isset($_POST['billing_postcode'])){update_user_meta($customer_id,'billing_postcode',sanitize_text_field($_POST['billing_postcode']));}
    if(isset($_POST['billing_state'])){update_user_meta($customer_id,'billing_state',sanitize_text_field($_POST['billing_state']));}
    if(isset($_POST['billing_address_1'])){update_user_meta($customer_id,'billing_address_1',sanitize_text_field($_POST['billing_address_1']));}
    if(isset($_POST['billing_phone'])){update_user_meta($customer_id,'billing_phone',sanitize_text_field($_POST['billing_phone']));}
    if(isset($_POST['email'])){update_user_meta($customer_id,'billing_email',sanitize_text_field($_POST['email']));}
}

这一切都很好,但目前“用户名”是由电子邮件地址生成的,这起初并不是真正的问题,但由于我们的重点客户是企业(因为它是一家 B2B 商店),因此会有很多的客户拥有[电子邮件受保护]

这导致他们的用户名是 info...这并不是那么好。所以我的问题是,如何配置 WooCommerce/WordPress 以通过自定义字段生成用户名:名字 (billing_first_name) 和姓氏 (billing_last_name)?

我希望这样用户名只是客户的姓名,例如名字-姓氏,而不是 info1...info2... 等...

预先感谢您提供更多信息!


您应该需要使用挂钩的自定义函数woocommerce_new_customer_data过滤钩。下面的代码将替换错误和重复的用户名(您将在数组中设置)按完整的帐单名称:

add_filter( 'woocommerce_new_customer_data', 'custom_new_customer_data', 10, 1 );
function custom_new_customer_data( $new_customer_data ){

    // Complete HERE in this array the wrong usernames you want to replace (coma separated strings)
    $wrong_user_names = array( 'info', 'contact' );

    // get the first and last billing names
    if(isset($_POST['billing_first_name'])) $first_name = $_POST['billing_first_name'];
    if(isset($_POST['billing_last_name'])) $last_name = $_POST['billing_last_name'];

    // the customer billing complete name
    if( ! empty($first_name) || ! empty($last_name) )
        $complete_name = $first_name . ' ' . $last_name;

    // Replacing 'user_login' in the user data array, before data is inserted
    if( ! empty($complete_name) && ! in_array( complete_name, $wrong_user_names ) )
        $new_customer_data['user_login'] = sanitize_user( str_replace( ' ', '-', $complete_name ) );

    return $new_customer_data;
}

代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。

我还没有测试过这段代码,但它应该可以工作。


这是一个更简单的版本,它将用帐单完整名称替换所有用户名。

add_filter( 'woocommerce_new_customer_data', 'custom_new_customer_data', 10, 1 );
function custom_new_customer_data( $new_customer_data ){

    // get the first and last billing names
    if(isset($_POST['billing_first_name'])) $first_name = $_POST['billing_first_name'];
    if(isset($_POST['billing_last_name'])) $last_name = $_POST['billing_last_name'];

    // the customer billing complete name
    if( ! empty($first_name) || ! empty($last_name) )
        $complete_name = $first_name . ' ' . $last_name;

    // Replacing 'user_login' in the user data array, before data is inserted
    if( ! empty($complete_name) )
        $new_customer_data['user_login'] = sanitize_user( str_replace( ' ', '-', $complete_name ) );

    return $new_customer_data;
}

代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。

我还没有测试过这段代码,但它应该可以工作。

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

用户名是通过 WooCommerce 注册中的账单全名自动生成的 的相关文章

  • 将文件附加到 PHPMailer

    我目前正在开发一个项目 该项目将文件作为 blob 存储在数据库中 我需要将文件附加到电子邮件并通过 PHPMailer 发送出去 我熟悉 mail gt addAttachment 但是 这个函数似乎只接受文件路径 而我没有 我想知道是否
  • File_get_contents($url): 无法打开流

    我有一个脚本 我使用以下方法读取文件 file get contents urlencode url 我收到此错误 failed to open stream HTTP request failed HTTP 1 0 400 Bad req
  • Ajax 调用 contentType: 'application/json' 不起作用

    我有一个 ajax 调用 它将表单数据发送到 php 函数 因为我读了很多使用contentType application json 这是最佳实践 我也想尝试一下 但不幸的是 我的脚本在使用时没有返回任何内容 如果我删除它 脚本就会执行它
  • 在 csv 中查找数值

    我需要检查特定的数字 ID 是否在给定的字符串或逗号分隔值中 myVal 20 字符串可以仅包含多个值中的一个值 以逗号分隔 str 20 str 20 33 5 str 220 33 5 4420 本来我想到用strpos 但是20可以在
  • 奇怪的 500 内部服务器错误(firebug、php、display_errors、ajax)

    在一页上我正在进行多个 AJAX 调用 所有调用均成功返回响应 但最后一个调用 与其他 ajax 调用无关 返回 500 内部服务器错误作为响应代码 如 firebug 所示 但是 尽管存在错误代码 该 AJAX 调用仍会返回正确的内容 令
  • PHP 中“或”的奇怪用法

    PHP s or是一个奇怪的关键字 这是一个让我感到困惑的代码片段 echo 0 or 1 prints 1 foo 0 or 1 echo foo prints 1 foo 0 or 1 echo foo prints 0 for som
  • 使用 utf-8 文件名发送 MIME 编码的电子邮件附件

    你好亲爱的人们 我花了三天时间在网上搜索答案 但没有找到任何答案 我发现了很多 几乎 的案例 但没有一个正是我正在寻找的 我能够获取希伯来语的主题和正文消息 但无法获取希伯来语的附加文件名 顺便说一句 我对 PHPMailer 等第三方程序
  • 尝试使用 PHP GD 以固定宽度/高度生成按比例裁剪的缩略图

    我正在尝试使用 GD 在 PHP 中创建一个缩略图生成器 它将获取图像并将其缩小到固定的宽度 高度 它从原始图像中获取的正方形 基于我的固定宽度 高度 将来自图像的中心 以给出比例正确的缩略图 我将尝试用一些漂亮的 ASCII 来演示这个令
  • 如何在使用 echo 时将字符串与函数调用连接起来?

    我想在我的 echo ed html 字符串中使用两个函数调用返回的值 li a href the permalink the title a li 以下工作正常 echo li a href echo the title echo a l
  • 差异:查看页面源代码与在 Firebug 中查看

    当我查看页面的页面源时 例如 http my sa ucsb edu public curriculum coursesearch aspx http my sa ucsb edu public curriculum coursesearc
  • 使用命名占位符时 PHP/SQL 插入错误

    我有以下 PHP PDO 语句 STH this gt db gt prepare INSERT INTO UserDetails FirstName LastName Address City County PostCode Phone
  • 有没有办法通过给出整数值 PHP 来获取月份名称

    您好 我正在使用 PHP 我想传递一个整数值 1 12 并获取相应的月份名称 PHP 中有没有办法做到这一点 或者我必须通过初始化月份名称数组来完成自己的操作 我想做 month name get month name 1 echo mon
  • Razorpay 支付集成 -> 我如何检测关闭按钮 X 附近的 razorpay 模型

    我在 CI 框架中使用 Razorpay 当用户在没有付款的情况下关闭时 创建 razor 支付模型 然后对于取消订单 我希望通过状态更改为已取消来触发查询 那么我怎样才能检测到这一点 我已经在使用 by click jQuery 点击关闭
  • 如何在 Yii 框架中从数据库中获取所有表名和列名

    我正在开发一个模块 我想在其中执行动态相关下拉表和列名称功能 前任 获取所有表名称并将其显示在下拉字段中 选择特定表后 我想在下拉字段中再次显示其所有列名称 问题是 1 如何从数据库中获取所有表名 2 如何从表中获取所有列名 我尝试了一些文
  • Php mod_rewrite 无法正常工作

    我有一个带有以下链接结构的 php 页面 http localhost wisper businesspage php profile creativeartbd 所以我尝试将此链接转换为以下样式 http localhost wisper
  • 以编程方式添加数字签名外观?

    我正在以编程方式对我的 PDF 文件进行签名 并且我想将签名外观添加到 PDF 我需要哪些对象才能实现此目的 我知道我必须Annotations BBox and XObject但我真的不知道按什么顺序以及是否需要其他东西 调试此类内容以找
  • 在 Laravel Schema 中创建价格列

    我想在 Laravel 模式中创建一个价格列 public function up Schema create cameras function Blueprint table table gt increments id table gt
  • 当我在 PHP 中将 print_r() 应用于数组时,为什么会得到“Resource id #4”? [复制]

    这个问题在这里已经有答案了 可能的重复 我如何从 PHP 中的 MySql 响应中 回显 资源 id 6 https stackoverflow com questions 4290108 how do i echo a resource
  • Codeigniter,为MySQL创建表和用户

    我想以编程方式使用 CI 创建数据库和用户 到目前为止 我有这 2 个简单的 MySQL 语句 CREATE DATABASE testdb DEFAULT CHARACTER SET utf8 COLLATE utf8 general c
  • 以零开头的字符串/数字的正确格式?

    我正在尝试使用 PHP 创建一个包含电话号码列表的文件 它工作正常 但如果电话号码以零开头 则该数字将从 Excel 文件中删除 有谁知道如何正确设置格式以使其保持不变 Either Set the value explicitly as

随机推荐

  • bash中删除长度小于2的单词

    我在 CentOS 5 5 上使用 bash 我有一个用空格分隔的字符串 并且该字符串只包含字母和数字 并且这个字符串可能有多余的空格 例如之间有超过1个空格 words and string exmple This is a lovey
  • 从 ASP.NET 5 中的 config.json 检索部分

    假设我有一个config json像这样 CustomSection A 1 B 2 我知道我可以使用IConfiguration对象获取特定设置 即configuration Get CustomSection A 但是我可以获取整个层次
  • 为什么我的帐户上的 OneNote API 延迟大约 3 天?

    简而言之 通过 OneNote API 获取笔记的页面信息有大约 3 天滞后的数据 为什么会出现这种情况 我假设只针对我 以及如何解决这个问题 我正在开发一个个人 R 程序来阅读我的 OneNote 笔记 大约一周前就启动并运行了 身份验证
  • 如何在textAngular编辑器中的范围var中删除占位符img?

    我在带有 Angular 的应用程序中使用 TextAngular 指令 当我插入 youTube 链接 通过工具栏按钮 时 它会在编辑器中显示占位符图像 我希望将所有 html 保存在 scope var 中 但不包含占位符 html 目
  • 将链表的头部移动到尾部

    我需要用 Java 编写一个方法 将链表中的第一个元素移动到最后一个位置 为了实现这一点 我相信我必须设置一个节点来引用 head 之后的第一个元素 然后将下一个节点设置为 null 我尝试用我的方法执行此操作 但是运行该方法时 输出不正确
  • java网络服务客户端

    我有 stfw 但我找不到一种简单 独立的方法来在 java 中创建 web 服务客户端 有人有这个的链接 样本吗 soapUI是一个测试 Web 服务的好工具 创建服务器存根来测试客户端或仅将客户端请求发送到任何 Web 服务非常简单
  • 发现 iOS 应用程序购买日期

    我明确不是指应用内购买 是否可以在 iOS 上找到应用程序本身的购买日期 我想奖励早期购买者 奖励早期用户 启动应用程序的用户 是not要走的路 我想奖励那些在 1 月 1 日到 1 月 31 日之间购买游戏的用户 甚至是在 2 月 28
  • WPF ComboBox:将 SelectedItem 设置为不在 ItemsSource -> 绑定奇怪中的项目

    我想要实现以下目标 我想要一个显示可用 COM 端口的组合框 启动时 并单击 刷新 按钮 我想获取可用的 COM 端口并将选择设置为最后选择的值 从应用程序设置中 如果设置中的值 最后一个 com 端口 不在值列表 可用 COM 端口 中
  • 在 Vortex86DX 上从头开始构建和编译 GCC 5.2.0 时出错

    为了升级 VortexDX86 定制 linuxgcc 3 2 3编译器 我正在尝试构建 GCC 5 2 0 编译器以支持最新的 C 11 标准 我已经从以下位置下载了它的源代码gcc gnu org并做了基于标准linux包构建器这个链接
  • D3js:自动放置标签以避免重叠? (斥力)

    如何在地图标签上应用力排斥力 以便它们自动找到正确的位置 博斯托克的 让我们制作一张地图 迈克 博斯托克的让我们制作一张地图 下面的屏幕截图 默认情况下 标签放置在点的坐标和多边形 多边形的坐标处path centroid d 简单的左对齐
  • 使用 Python 写入 CSV 会添加空行 [重复]

    这个问题在这里已经有答案了 我正在尝试写入 CSV 文件 但中间有空白行 如何删除空白行 import csv b open test csv w a csv writer b data Me You 293 219 54 13 a wri
  • 如何在python中不断更新固定长度的列表?

    抱歉这个菜鸟问题 我刚刚开始编码 我需要跟踪 1 小时的价格历史记录 我希望每秒将值拉入大小为 3600 的列表中 直到列表被填满 然后从那时起每秒将列表向左移动 以便价格保持不变 while True polo exchange retu
  • 如何将自定义事件发送到 PyQt 中的事件循环

    我正在尝试在 PyQt 中发出自定义事件 一个小部件将发出 另一个小部件将侦听事件 但这两个小部件不需要相关 在 JavaScript 中 我会通过这样做来实现这一点 Component 1 document addEventListene
  • 在javascript中计算元音

    我使用此代码来搜索和计算字符串中的元音 a run forest run a a split var syl 0 for var i 0 i lt a length 1 i for var i2 0 i2 lt a i length 1 i
  • systemd 服务未执行通知发送

    我想在 python 脚本中为某些事件生成弹出窗口 我为此目的使用 通知发送 subprocess Popen notify send Authentication True False 上面的命令在终端上执行得很好 但是当我从 syste
  • 如何修复 Cocoapods 中名称冲突的框架?

    我创建了两个私有 Pod 它们都依赖于相同的供应商框架 当我将 pod 添加到我的主项目中时 出现以下错误 target has frameworks with conflicting names FirebaseAnalytics Fir
  • Kotlin karate-junit-5 未找到任何功能或场景[重复]

    这个问题在这里已经有答案了 我正在使用 karate 与 junit 5 runner 和 kotlin 并且 karate 似乎找不到我的功能文件 我得到以下内容 未找到功能或场景 classpath sncf karate org op
  • 如何在查询字符串中传递 HTML 代码

    我需要使用 QueryString 传递 html 代码 因为我使用 ajax 方法在我的网站上发送帖子 我的意思是评论而不是方法的类型 当我写这样的帖子时 Hi everybody br Whats up 它只是将 大家好 删除了其余内容
  • 如何在构建时将其他文件包含到 C# 的输出目录中?

    我的应用程序需要一些库文件才能工作 我的应用程序包含设置和部署 我已经知道 为了在安装时将库文件添加到应用程序的输出目录中 我只需在构建之前在 NET IDE 中引用这些库 唯一的问题是这些库不能被引用 所以我需要能够将这些库复制到我的应用
  • 用户名是通过 WooCommerce 注册中的账单全名自动生成的

    因此 我在 WooCommerce 网上商店的注册页面中添加了各种自定义字段 如下所示 Registration page Add extra fields in registration form add action woocommer