17

How do I set/get the Postcode(Zip-code) in woocommerce? Is there a function for this?

ie., can I set the zip code through any function?

I would also like to know, how to populate this field with my data(say 546621) if the user is not logged in?

Nagendra Rao
  • 7,016
  • 5
  • 54
  • 92

3 Answers3

22

You can do the following to get/set billing/shipping postcodes,

To set the values,

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

If you just want to fetch the postcodes, you can fetch it from the user meta table itself,

$shipping_postcode = get_user_meta( $current_user->ID, 'shipping_postcode', true );
$billing_postcode = get_user_meta( $current_user->ID, 'billing_postcode', true );
Nagendra Rao
  • 7,016
  • 5
  • 54
  • 92
12

Thanks @rao! I was looking for this for hours...I was able to take your code and use it to pull the user's entire address - so I can use each address field to pre-populate an address form I'm creating elsewhere.

$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>";
jmiller
  • 578
  • 11
  • 28
  • I know it's an old post but you can access the Name of the Billing Address as well by using `billing_first_name` and `billing_last_name`, just in case a different name is used :) Should apply to the Shipping Address as well – j.grima May 03 '17 at 08:25
7

You can use the WC_Customer class which provides this function. Its loaded inside the Woocommerce class. This information is stored inside the current session.

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();
}

The complete documentation for this class: http://docs.woothemes.com/wc-apidocs/class-WC_Customer.html

Hope this helps.

  • 3
    I also want to point out that this method only sets the data in the session variable and its not stored in the DB. If you would like to set it in the db, you have to set the `billing_postcode` and `shipping_postcode` in `wp_usermeta` table. – Nagendra Rao Nov 23 '13 at 01:01
  • @Rao, this is exactly what I want :-) I'd like to ask the delivery address on my home page (to tell users in advance if I don't ship here), and have it already set on the Checkout page. – Marco Marsala Mar 23 '16 at 11:34
  • 1
    What if you want to get/set shipping company and name? And what about custom billling/shipping fields already created? – MrCalvin May 04 '16 at 15:06