【问题标题】:Avoid WooCommerce Checkout Billing form to overwrite default Wordpress user data避免 WooCommerce Checkout Billing 表单覆盖默认的 Wordpress 用户数据
【发布时间】:2017-08-17 09:16:50
【问题描述】:

在数据库中,我们有 metauser 表,其中包含:
first_namelast_name 字段

这些是默认的 Wordpress 名字和姓氏。

我们还有:
billing_first_namebilling_last_name

现在,当用户填写帐单并进行结帐流程时, Woocommerce 使用账单名称字段中的新值更新这两个字段 (默认)。

我已经尝试了很多使用动作的东西,例如:

woocommerce_before_checkout_billing_form

woocommerce_after_checkout_billing_form

还尝试使用以下内容更新元:

update_user_meta()

但它不起作用。

我希望它不会覆盖默认的 first_name 和 last_name, 但仅在 billing_first_name 和 billing_last_name 中保留新值

我觉得默认流程是这样的
https://gist.github.com/claudiosanches/ae9a8b496c431bec661b69ef73f1a087

请问有什么帮助吗?

【问题讨论】:

    标签: php wordpress woocommerce checkout user-data


    【解决方案1】:

    方法是在woocommerce_checkout_update_customer动作钩子中使用自定义函数:

    add_action('woocommerce_checkout_update_customer','custom_checkout_update_customer', 10, 2 );
    function custom_checkout_update_customer( $customer, $data ){
    
        if ( ! is_user_logged_in() || is_admin() ) return;
    
        // Get the user ID
        $user_id = $customer->get_id();
    
        // Get the default wordpress first name and last name (if they exist)
        $user_first_name = get_user_meta( $user_id, 'first_name', true );
        $user_last_name = get_user_meta( $user_id, 'last_name', true );
    
        if( empty( $user_first_name ) || empty( $user_last_name ) ) return;
    
        // We set the values by defaul worpress ones, before it's saved to DB
        $customer->set_first_name( $user_first_name );
        $customer->set_last_name( $user_last_name );
    }
    

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

    在 WooCommerce 3+ 中测试和工作

    【讨论】:

    • 有没有办法将账单数据存储在新的数据库表中?
    • @YahyaEssam,您应该更好地创建自定义相关的用户元数据,这将更加容易和负担得起……您不觉得吗?
    • 为什么 WooCommerce 会改变这一点?他们有 billing_first_name 和 billing_last_name 是有原因的,为什么要搞乱 Wordpress 的 first/last_name 元数据。愚蠢的。感谢您的 sn-p,它有效。
    猜你喜欢
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 2018-03-25
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多