2017-08-17 78 views
0

在數據庫中,我們已經包含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

任何幫助的這個好嗎?

回答

1

的方式是使用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