2013-10-03 9 views
11

を設定しますか?このための機能はありますか?WooCommerce:取得し、私はwoocommerceに郵便番号(ジップコード)を取得/設定するにはどうすればよいの配送&請求先住所の郵便番号

すなわち、私は、任意の関数を使用して郵便番号を設定することができますか?私も知りたいのですが

、どのユーザーがログインしていない場合、私のデータ(546621を言う)と、このフィールドを移入するには?

答えて

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には格納されません。 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'を使って、別の名前が使用されている場合にも、請求先住所の名前にアクセスすることができます:) –

関連する問題