Woocommerce 是一個很是好的WordPress 電子商務插件,可是結帳頁面用戶地址不是很適合國內用戶習慣,這裏介紹怎麼自定義結帳頁面地址信息顯示順序。php
用戶進入結帳(checkout)頁面,用戶地址信息顯示效果以下:html
對於國內用戶,姓確定在名以前,地址信息排列通常爲:國家-》省-》市區-》詳細地址ide
在你的主題的functions.php文件中添加以下代碼:spa
1 // Change checkout page address 2 function custom_override_checkout_fields( $fields ) { 3 // billing address 4 $fields['billing']['billing_first_name']['class'] = array('form-row-last'); 5 $fields['billing']['billing_last_name']['class'] = array('form-row-first'); 6 $fields['billing']['billing_phone']['class'] = array('form-row-wide'); 7 8 $fields['billing']['billing_email']['class'] = array('form-row-wide'); 9 10 $fields['billing']['billing_state']['class'] = array('form-row-wide'); 11 12 13 14 15 // billing address order 16 $billing_order = array( 17 "billing_last_name", 18 "billing_first_name", 19 "billing_country", 20 "billing_state", 21 "billing_city", 22 "billing_address_1", 23 "billing_address_2", 24 "billing_phone", 25 "billing_email", 26 ); 27 28 29 foreach($billing_order as $field) 30 { 31 $billing_ordered_fields[$field] = $fields["billing"][$field]; 32 } 33 34 $fields["billing"] = $billing_ordered_fields; 35 36 return $fields; 37 } 38 39 add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
代碼解釋:插件
1 // 設置郵箱地址,在獨佔一行顯示 2 3 $fields['billing']['billing_email']['class'] = array('form-row-wide'); 4 5 // 設置姓在前顯示,名在後顯示,可是兩個在同一行 6 7 $fields['billing']['billing_first_name']['class'] = array('form-row-last'); 8 $fields['billing']['billing_last_name']['class'] = array('form-row-first'); 9 10 // 這個設置的是顯示順序,排在前面的,就在前面顯示。 11 // 先顯示姓,而後顯示名,後面是國家、省份、城市、地址1、地址2、電話號碼、郵箱 12 // 在這裏不設置的字段,在前臺不會顯示出來 13 $billing_order = array( 14 "billing_last_name", 15 "billing_first_name", 16 "billing_country", 17 "billing_state", 18 "billing_city", 19 "billing_address_1", 20 "billing_address_2", 21 "billing_phone", 22 "billing_email", 23 );