Bert
Bert

Reputation: 55

Set WooCommerce user billing and shipping country programmatically

I use WooCommerce and I build some custom functions. The last thing what I want is set the country for billing and shipping programmatically.

I removed the input billing_country and shipping_country from the checkout page.

Is there a way to set the billing_country and shipping_country programmatically?

I tried WC()->customer->set_shipping_country('US'); this set only the country for the cart and checkout page but does not send the country by submit.

Upvotes: 2

Views: 3143

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253859

You missed the WC_Customer save() method…

You can use the following from a static or dynamic user ID (the user Id is mandatory):

$country_code = 'US';

// Get the WC_Customer instance object from user ID
$customer = new WC_Customer( $user_id );

$customer->set_billing_country( $country_code );
$customer->set_shipping_country( $country_code );
$customer->save();

Now it's saved as user meta data in the database.

Upvotes: 3

Related Questions