2013-10-03 52 views

回答

10

您可以執行以下操作來獲取/設置計費/航運郵政編碼,

設置值,

$customer = new WC_Customer(); 
$customer->set_postcode('123456');  //for setting billing postcode 
$customer->set_shipping_postcode('123456'); //for setting shipping postcode 

如果你只是想獲取的郵政編碼,即可獲取它來自用戶元表本身,

$shipping_postcode = get_user_meta($current_user->ID, 'shipping_postcode', true); 
$billing_postcode = get_user_meta($current_user->ID, 'billing_postcode', true); 
4

您可以使用提供此功能的WC_Customer類。它被加載到Woocommerce類中。這些信息存儲在當前會話中。

function set_shipping_zip() { 
    global $woocommerce; 

    //set it 
    $woocommerce->customer->set_shipping_postcode(12345); 
    $woocommerce->customer->set_postcode(12345); 

    //get it 
    $woocommerce->customer->get_shipping_postcode();  
    $woocommerce->customer->get_postcode(); 
} 

這個類的完整文檔:http://docs.woothemes.com/wc-apidocs/class-WC_Customer.html

希望這有助於。

+0

這也適用。謝謝! – Rao

+2

我也想指出,這種方法只設置會話變量中的數據,而不是存儲在數據庫中。如果你想在db中設置它,你必須在'wp_usermeta'表中設置'billing_postcode'和'shipping_postcode'。 – Rao

+0

@Rao,這正是我想要的:-)我想問我的主頁上的送貨地址(如果我不在這裏發貨,請提前告訴用戶),並且已經在Checkout頁面上設置了它。 –

4

謝謝@rao!我正在尋找這個好幾個小時......我能夠拿走你的代碼並用它來拉取用戶的整個地址 - 這樣我就可以使用每個地址字段來預填充我在別處創建的地址表單。

$fname = get_user_meta($current_user->ID, 'first_name', true); 
$lname = get_user_meta($current_user->ID, 'last_name', true); 
$address_1 = get_user_meta($current_user->ID, 'billing_address_1', true); 
$address_2 = get_user_meta($current_user->ID, 'billing_address_2', true); 
$city = get_user_meta($current_user->ID, 'billing_city', true); 
$postcode = get_user_meta($current_user->ID, 'billing_postcode', true); 

echo $fname . "<BR>"; 
echo $lname . "<BR>"; 
echo $address_1 . "<BR>"; 
echo $address_2 . "<BR>"; 
echo $city . "<BR>"; 
echo $postcode . "<BR>"; 
+0

我知道這是一箇舊帖子,但您也可以使用'billing_first_name'和'billing_last_name'來訪問賬單地址的名稱,以防萬一使用不同的名稱:)也應該適用於送貨地址 –

相關問題