2014-11-14 75 views
3

我正在開發一種新的運輸方法,可根據包裹重量,體積和客戶郵政編碼計算出運費... 我在檢索購物車信息時沒有問題calculate_shipping函數,但是當我嘗試從客戶檢索郵政編碼時,它似乎返回一個空值。 如何在運行時檢索此信息以計算最終成本?Woocommerce:獲取發貨郵政編碼以計算運輸成本

public function calculate_shipping($package) { 
    GLOBAL $woocommerce; 
    $items = $woocommerce->cart->get_cart(); 
    $cliente = $woocommerce->customer; 
    $peso = $woocommerce->cart->cart_contents_weight; 
    $customer_postcode = $woocommerce->customer->get_shipping_postcode(); 
} 

解決:

$customer_postcode = get_user_meta(get_current_user_id(), 'shipping_postcode', true);

+2

能否請你告訴我們一些你的代碼?你說它返回「一個空值」,我們不能在沒有看到代碼的情況下修復它。當然 – Drown 2014-11-14 17:48:26

+0

是:
公共職能calculate_shipping($包){
\t \t \t GLOBAL $ woocommerce;
\t \t \t \t \t $ items = $ woocommerce-> cart-> get_cart();
\t \t \t \t \t $ cliente = $ woocommerce-> customer;
\t \t \t \t \t \t \t \t \t $比索= $ woocommerce-> cart-> cart_contents_weight;
\t \t \t \t $ customer_postcode = $ woocommerce-> customer-> get_shipping_postcode();
\t \t \t \t}

的$ customer_postcode = $ woocommerce->客戶 - > get_shipping_postcode();是空的,我可以得到這個工作的唯一方法是,如果我去結賬頁面,並返回到購物車 – olken 2014-11-14 18:14:43

回答

1

我認爲這是在客戶類處理

計費郵編

WC()->customer->get_postcode();

航運郵編

WC()->customer->get_shipping_postcode();

+0

我有同樣的想法....但它似乎不會加載客戶送貨數據,直到你到結帳頁面...現在基本上現在唯一的辦法是我可以得到這個工作是如果我去結帳頁面,並返回到購物車 – olken 2014-11-14 18:24:02

0
global $woocommerce; 
$customer = new WC_customer(); 
$customer_id = $customer->ID 

$get_customer_shipping_pincode = get_user_meta($customer_id,"shipping_postcode",true); 
$get_customer_billing_pincode = get_user_meta($customer_id,"billing_postcode",true); 

如果用戶在(對寄存器的用戶)登錄,

$get_customer_shipping_pincode = get_user_meta(get_current_user_id(),"shipping_postcode",true); 
$get_customer_billing_pincode = get_user_meta(get_current_user_id(),"billing_postcode",true); 
2
global $woocommerce; 
$customer = new WC_Customer(); 
$woocommerce->customer->get_shipping_postcode(); 
+2

請考慮編輯您的帖子,以添加更多關於你的代碼做什麼以及爲什麼它可以解決問題的解釋。一個主要包含代碼的答案(即使它正在工作)通常不會幫助OP瞭解他們的問題。 – Miki 2015-09-11 13:06:09

4
// add this in your calculate_shipping function 
public function calculate_shipping($package) { 
    $postal_code = $package[ 'destination' ][ 'postcode' ]); 
} 
+1

這是正確的答案,get_user_meta(get_current_user_id(),'shipping_postcode',true);其實是一種錯誤的做法。 – Alice 2017-05-03 10:33:58