将其他计费注册字段与 WooCommerce 中的默认 Wordpress 字段同步

2024-01-19

我已将以下代码添加到 Woocommerce 用户注册表中,以获取注册页面上的账单详细信息。

现在当新用户注册时会发生什么,名字和姓氏将在账单详细信息数据库以及默认 WordPress 用户帐户中注册。如果用户更新其帐户(wordpress 用户帐户)上的名字和姓氏,账单详细信息也应更新。

Woocommerce 设置:

访客结帐已禁用。 结帐页面用户注册已启用。 登录页面注册已启用。 只有注册用户才能进行购买。

  1. 这是用户注册表单,我从结账过程中提取这些额外的账单详细信息。
  1. 在“帐户详细信息”上,我更新了“名字”,它在这里有效,但我在“帐单详细信息”上没有得到相同的更新。如果用户在“帐户详细信息”上更新这两个字段及其电子邮件地址,我希望“帐单详细信息”上有相同的“名字”和“姓氏”更新。
  1. 现在,在更新“帐户详细信息”上的“名字”和“姓氏”后,我回到“帐单详细信息”,但它仍然显示注册过程中使用的旧“名字”和“姓氏”。另外,我想从账单详细信息中隐藏这 3 个字段:“名字”、“姓氏”和“电子邮件地址”,以免混淆注册用户。我需要数据库中“账单详细信息”的这些更新,只是因为这些信息将打印在发票和电子邮件上

仅当管理员或商店经理进入用户配置文件(从后端)并手动按下“更新”按钮时,数据才会同步/更新,然后才会生效。我希望当注册用户从其帐户(前端)进行任何更改时,数据自动同步/更新。

任何帮助将不胜感激。

请检查以下代码:

// Custom function to display the Billing Address form to registration page
add_action('woocommerce_register_form_start','zk_add_billing_form_to_registration');
function zk_add_billing_form_to_registration(){
    $checkout = WC()->checkout;
    foreach ( $checkout->get_checkout_fields( 'billing' ) as $key => $field ) :
        if($key!='billing_email')
            woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
    endforeach;
}

// Custom function to save Usermeta or Billing Address of registered user
add_action('woocommerce_created_customer','zk_save_billing_address');
function zk_save_billing_address($user_id){
    $address = $_POST;
    foreach ($address as $key => $field){
        // Only billing fields values
        if( strpos( $key, 'billing_' ) !== false ){
            // Condition to add firstname and last name to user meta table
            if($key == 'billing_first_name' || $key == 'billing_last_name'){
                $new_key = str_replace( 'billing_', '', $key );
                update_user_meta( $user_id, $new_key, $_POST[$key] );
            }
            update_user_meta( $user_id, $key, $_POST[$key] );
        }
    }
}

// Checking & validation of the additional fields in registration form.
add_action('woocommerce_register_post','zk_validation_billing_address', 10, 3 );
function zk_validation_billing_address( $username, $email, $validation_errors ){
    foreach ($_POST as $key => $field) :
        // Validation: Required fields
        if( strpos( $key, 'billing_' ) !== false ){
            if($key == 'billing_country' && empty($field) ){
                $validation_errors->add( $key.'_error',  __( 'Please select a country.', 'woocommerce' ));
            }
            if($key == 'billing_first_name' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter first name.', 'woocommerce' ) );
            }
            if($key == 'billing_last_name' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter last name.', 'woocommerce' ) );
            }
            if($key == 'billing_address_1' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter address.', 'woocommerce' ) );
            }
            if($key == 'billing_city' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter city.', 'woocommerce' ) );
            }
            if($key == 'billing_state' && empty($field) ){
                if(count( WC()->countries->get_states($_POST['billing_country']) ) > 0)
                    $validation_errors->add( $key.'_error', __( 'Please enter state.', 'woocommerce' ) );
            }
            if($key == 'billing_postcode' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter a postcode.', 'woocommerce' ) );
            }
            /*
            if($key == 'billing_email' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter billing email address.', 'woocommerce' ) );
            }
            */
            if($key == 'billing_phone' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter phone number.', 'woocommerce' ) );
            }

        }
    endforeach;
}

add_filter( 'woocommerce_billing_fields', 'sv_required_billing_fields' );
function sv_required_billing_fields( $fields ) {
    $fields['billing_phone']['required'] = true;
    $fields['billing_city']['required'] = true;
    $fields['billing_country']['required'] = true;
    $fields['billing_address_1']['required'] = true;
    return $fields;
}

// Hidding some billing fields (Wordpress edit user pages)
add_action( 'edit_user_profile', 'user_profile_hide_some_fields_css', 1, 1 );
function user_profile_hide_some_fields_css( $user ){
    ?>
    <style>
    .user-edit-php table#fieldset-billing tr:first-child,
    .user-edit-php table#fieldset-billing tr:nth-child(2),
    .user-edit-php table#fieldset-billing tr:last-child {
        display:none;
    }
    </style>
    <?php
}

// Sync hidden billing fields (Wordpress edit user pages)
add_action( 'personal_options_update', 'sync_user_data_wp_and_billing_wc', 100, 1 );
add_action( 'edit_user_profile_update', 'sync_user_data_wp_and_billing_wc', 100, 1 );
function sync_user_data_wp_and_billing_wc( $user_id )
{
    if( ! empty($_POST['first_name']) ) {
        update_user_meta( $user_id, 'billing_first_name', sanitize_text_field( $_POST['first_name'] ) );
    }

    if( ! empty($_POST['last_name']) ) {
        update_user_meta( $user_id, 'billing_last_name', sanitize_text_field( $_POST['last_name'] ) );
    }

    if( ! empty($_POST['email']) ) {
        update_user_meta( $user_id, 'billing_email', sanitize_text_field( $_POST['email'] ), sanitize_text_field( $_POST['billing_email'] ) );
    }
}

我重新审视了你的代码,例如最后 4 个函数可以合并为一个,还有其他的事情......

数据更新与同步

现在,当客户在我的帐户页面上更新他的数据时,所有数据都会由 woocommerce 同步到各地,除了他现有的过去的命令

如果客户更改结账字段并处理结账,数据也会随处更新......

So 您无需担心客户同步数据。

Note:该函数挂接在woocommerce_billing_fields将在您的附加注册字段和结帐字段中启用,因为您正在使用结帐对象生成附加注册字段...您可以使用条件! is_checkout()仅针对我的帐户注册字段。

这是您重新访问的代码:

// Custom function to display the Billing Address form to registration page
add_action('woocommerce_register_form_start','zk_add_billing_form_to_registration');
function zk_add_billing_form_to_registration(){
    $checkout = WC()->checkout;
    foreach ( $checkout->get_checkout_fields( 'billing' ) as $key => $field ) :
        if($key!='billing_email')
            woocommerce_form_field( $key, $field, $checkout->get_value( $key ) );
    endforeach;
}

// Custom function to save Usermeta or Billing Address of registered user
add_action('woocommerce_created_customer','zk_save_billing_address');
function zk_save_billing_address($user_id){
    $address = $_POST;
    foreach ($address as $key => $field){
        // Only billing fields values
        if( strpos( $key, 'billing_' ) !== false ){
            // Condition to add firstname and last name to user meta table
            if($key == 'billing_first_name' || $key == 'billing_last_name'){
                $new_key = str_replace( 'billing_', '', $key );
                update_user_meta( $user_id, $new_key, $_POST[$key] );
            }
            update_user_meta( $user_id, $key, $_POST[$key] );
        }
    }
}

// Checking & validation of the additional fields in registration form.
add_action('woocommerce_register_post','zk_validation_billing_address', 10, 3 );
function zk_validation_billing_address( $username, $email, $validation_errors ){
    foreach ($_POST as $key => $field) :
        // Validation: Required fields
        if( strpos( $key, 'billing_' ) !== false ){
            if($key == 'billing_country' && empty($field) ){
                $validation_errors->add( $key.'_error',  __( 'Please select a country.', 'woocommerce' ));
            }
            if($key == 'billing_first_name' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter first name.', 'woocommerce' ) );
            }
            if($key == 'billing_last_name' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter last name.', 'woocommerce' ) );
            }
            if($key == 'billing_address_1' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter address.', 'woocommerce' ) );
            }
            if($key == 'billing_city' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter city.', 'woocommerce' ) );
            }
            if($key == 'billing_state' && empty($field) ){
                if(count( WC()->countries->get_states($_POST['billing_country']) ) > 0)
                    $validation_errors->add( $key.'_error', __( 'Please enter state.', 'woocommerce' ) );
            }
            if($key == 'billing_postcode' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter a postcode.', 'woocommerce' ) );
            }
            /*
            if($key == 'billing_email' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter billing email address.', 'woocommerce' ) );
            }
            */
            if($key == 'billing_phone' && empty($field) ){
                $validation_errors->add( $key.'_error', __( 'Please enter phone number.', 'woocommerce' ) );
            }

        }
    endforeach;
}

add_filter( 'woocommerce_billing_fields', 'sv_required_billing_fields' );
function sv_required_billing_fields( $fields ) {
    $fields['billing_phone']['required'] = true;
    $fields['billing_city']['required'] = true;
    $fields['billing_country']['required'] = true;
    $fields['billing_address_1']['required'] = true;
    return $fields;
}

客户无法(永远)访问 WordPress 后端用户编辑页面。只有店长和管理员才能做到...
要在后端同步 WordPress 用户数据,您需要选择哪些字段具有优先级:

  • WordPress 默认字段(或)
  • 账单字段(来自 WooCommerce)。

最好优先考虑 WordPress 默认字段并隐藏必要的计费字段......

此代码将隐藏 3 个账单字段(名字、姓氏和电子邮件),并将它们与默认字段更新值同步:

// Hidding some billing fields (Wordpress edit user pages)
add_action( 'edit_user_profile', 'user_profile_hide_some_fields_css', 1, 1 );
function user_profile_hide_some_fields_css( $user ){
    ?>
    <style>
    .user-edit-php table#fieldset-billing tr:first-child,
    .user-edit-php table#fieldset-billing tr:nth-child(2),
    .user-edit-php table#fieldset-billing tr:last-child {
        display:none;
    }
    </style>
    <?php
}

// Sync hidden billing fields (Wordpress edit user pages)
add_action( 'personal_options_update', 'sync_user_data_wp_and_billing_wc', 100, 1 );
add_action( 'edit_user_profile_update', 'sync_user_data_wp_and_billing_wc', 100, 1 );
function sync_user_data_wp_and_billing_wc( $user_id )
{
    if( ! empty($_POST['first_name']) ) {
        update_user_meta( $user_id, 'billing_first_name', sanitize_text_field( $_POST['first_name'] ) );
    }

    if( ! empty($_POST['last_name']) ) {
        update_user_meta( $user_id, 'billing_last_name', sanitize_text_field( $_POST['last_name'] ) );
    }

    if( ! empty($_POST['email']) ) {
        update_user_meta( $user_id, 'billing_email', sanitize_text_field( $_POST['email'] ), sanitize_text_field( $_POST['billing_email'] ) );
    }
}

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

经过测试并有效...

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

将其他计费注册字段与 WooCommerce 中的默认 Wordpress 字段同步 的相关文章

  • 使用 GD lib 过滤器标准化 CSS 过滤器

    我想让用户拖动范围滑块并通过实时预览 CSS 滤镜 调整图像的亮度和对比度 然后使用 GD 库保存调整 但是 我似乎无法从 CSS 过滤器和 GD lib 亮度和对比度过滤器获得相同的结果 我的 CSS 过滤器范围为 50 150 其中 1
  • 尝试通过比较不同的表从 SQL 查询输出正确的值

    我对 SQL 非常陌生 需要有关如何使用正确的查询完成此任务的帮助 我有 2 张桌子需要使用 表 TB1 有 id Name 1 bob 2 blow 3 joe 表 TB2 有 compid property 1 bob 2 blow 我
  • 为什么我的 Facebook 访问令牌突然停止工作? “OAuthException:验证访问令牌时出错。”

    我有一个 iframe Facebook 应用程序 它使用 Facebook PHP SDK 进行身份验证并进行 api 调用 在身份验证过程中 系统会提示用户输入基本信息和离线访问 如果他们允许我的应用程序访问 Facebook 会将它们
  • PHP将数据写入文件中间而不重写文件的最佳方法是什么

    我正在 php 1GB 中处理大型文本文件 我正在使用 file get contents file txt NULL NULL 100000000 100 要从文件中间获取数据 但如果我想将文件中的数据更改为与原始数据不同的更改 我将不得
  • Ioncube 编码的文件是否可以解码?

    我是一名 php 开发人员 我的客户计划分发一个使用 Php 开发的软件 计划使用 ioncube 或类似软件对文件进行编码 在谷歌搜索时 我发现很少有人解码这些文件 这些文件使用 ioncube 甚至其他软件进行编码 如果您询问是否可以破
  • Paypal 付款标准默认输入卡详细信息

    我确信这个主题已经在这里了 但我刚刚与 Paypal 通电话 试图查明他们的帐户上是否有一个设置 可以让客户看到 输入卡详细信息区域 而不是自动 默认设置 引导您登录 注册您的 PayPal 帐户 Paypal 表示没有一个设置可以在他们这
  • PHP:如何检查 Guzzle 4 中的超时异常?

    如果请求期间发生错误 Guzzle 会引发异常 不幸的是 似乎没有特定于超时的错误 这对我来说很重要 因为我知道这些错误偶尔会发生 我想重试相应的请求 并且需要能够判断错误是否是由于超时而发生的 来自docs http docs guzzl
  • PHP ASCII 表库 [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 是否有一个事实上的标准库用于在 PHP 中创建 ASCII 表 我想要一些 PHP 代码 当传递数组或其
  • 如何在 Laravel 5 中通过键获取所有缓存项的列表?

    Laravel 中的 Cache 类具有 get itemKey 等方法来从缓存中检索项目 以及 Remember itemKey myData1 myData2 来将项目保存在缓存中 还有一个方法可以检查缓存中是否存在某个项目 Cache
  • Symfony 3新建项目报错

    我开始编写有关 Symfony 3 的教程 在使用以下命令创建新项目时遇到问题 php symfony phar new Symfony 我有这个错误 GuzzleHttp Exception RequestException Error
  • 如何修复 Nginx 自动 301 重定向到带有尾部斜杠的相同 URL?

    当我尝试将 Web 应用程序的子目录中的索引文件访问到相同的 URL 但附加了斜杠 时 Nginx 出现了不良行为 它正在重新路由请求 我有一个简单的 Web 应用程序 其中设置了一个根目录和其中的许多子目录 每个子目录中都有一个 inde
  • Woocommerce 中的欧洲 GDPR 附加结帐验证复选框

    您好 我一直在尝试向我的 Woocommerce 结帐页面添加一个额外的条件复选框 该复选框与条款和条件相同 但包含有关新 GDPR 数据保护 的信息以及指向我的隐私政策的链接 他们必须在方框中打勾才能结帐 我一直在使用从此处找到的各种代码
  • 下拉 24 小时选项值和 12 小时显示

    我需要创建一个时间数组 以便在 HTML 下拉列表中使用 数组键应采用 24 小时格式 值应采用 12 小时制 包含 am 和 pm 在数据库中我想存储 24 小时格式 有没有一种快速的方法来创建数组而不是每小时键入 example 00
  • 将错误保存到 MySQL 数据库

    我有一个 php 查询来更新 MySQL 数据库 请参见下文 sql update hr payroll set payroll number payroll number tax code tax bacs ref bacs ref pa
  • setImageCompressionQuality 与 setCompressionQuality 之间有什么区别 - Imagick

    我在Imagick中找到了两种设置图像压缩质量的方法 A 设置图像压缩质量 B 设置压缩质量 所以我想知道哪一个是最好的以及为什么在以下条件下 我读到了setCompressionQuality方法仅适用于新图像 我正在尝试压缩文件 jpe
  • 我需要一个 jQuery Autocomplete 使用 ajax 返回 id 和 name 的示例

    我需要一个示例 说明如何编写 jQuery 自动完成代码来填充product id 同时显示调用ajax 页面 remote php 的product name
  • MYSQL:SQL查询获取自增字段的值

    我有一张桌子 主键是id及其自动递增 现在 当我插入新记录时 我需要获取更新记录的 id 我怎样才能做到这一点 如果我使用查询 select max id from table name 执行后我可以获得id 但我能确定它是刚刚插入的记录的
  • proc_open() 失败并显示“权限被拒绝”

    我正在尝试使用proc open 执行程序并打印结果 但是 我不断收到 许可被拒绝 的消息 已将脚本和可执行文件的 chmod 设置为 0777 但无济于事 ini get safe mode 是假的 可能出什么问题了 我正在使用 Cent
  • ASCII“../”是 PHP 中指示目录遍历的唯一字节序列吗?

    我有一个 PHP 应用程序 它使用 GET参数来选择文件系统上的 JS CSS 文件 如果我拒绝输入字符串包含的所有请求 或者可见 7 位 ASCII 范围之外的字节 当路径传递到 PHP 的底层 基于 C 文件函数时 这是否足以防止父目录
  • 在 wampserver 2.2 上安装 php_imagick.dll PHP 扩展

    我使用的是 32 位操作系统的 Windows 7 我安装了 ImageMagick 6 8 7 Q16Link https www imagemagick org script download php windows我能够从命令行 转换

随机推荐