避免WooCommerce Checkout付款的形式,以覆盖缺省WordPress的用户数据

问题描述:

在数据库中,我们已经包含metauser表:
first_namelast_name领域
避免WooCommerce Checkout付款的形式,以覆盖缺省WordPress的用户数据

这些都是默认的WordPress的第一个和最后一个名称。

而且我们有:
billing_first_namebilling_last_name

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

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

woocommerce_before_checkout_billing_form 

woocommerce_after_checkout_billing_form 

也试图与更新元:

update_user_meta() 

但它不工作。

我希望它不会覆盖默认FIRST_NAME和姓氏, 但保留新值仅在billing_first_name和billing_last_name

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

任何帮助的这个好吗?

的方式是使用woocommerce_checkout_update_customer动作钩子钩住定制funtion:

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+

+0

这与我合作。非常感谢你 –

+0

有什么方法可以将帐单数据存储在新的数据库表中吗? –

+0

@YahyaEssam,你应该更好地创建定制相关的用户元数据,它会更容易和负担得起......你不觉得吗? – LoicTheAztec