WooCommerce: Populate Billing & Shipping When Adding A New User @ WP Dashboard

If you do a lot of manual work such as creating WordPress users for a B2B WooCommerce site (because they can only shop if they have an account, and you’ve disabled registration on the frontend), you may want to populate some Billing and Shipping fields so that you can save some time.

For example, imagine if all customers are based in Florida, USA; you could automatically populate their billing country, billing state, shipping country and shipping state!

So, let’s see how to approach this. Enjoy!

As soon as I create a customer from the WP dashboard, the fields marked in red are already populated for me! In this way, you can save a lot of time in case you manually create WooCommerce users

PHP Snippet: Assign Billing/Shipping Values Upon Add New Customer @ WP Dashboard

This code is a WordPress action that triggers when a new user registers on a WooCommerce site. It populates certain billing and shipping information for the newly registered user.

  • The first line sets up an action hook user_register, which fires when a new user registers on the site. It calls the function bbloomer_populate_billing_shipping_new_customer when this action occurs.
  • This function bbloomer_populate_billing_shipping_new_customer accepts one parameter $user_id, which represents the ID of the newly registered user.
  • The array $fields contains the names of the fields that need to be populated for billing and shipping information.
  • Depending on the field (whether it’s a state or country for billing or shipping), it sets a default value. For states, it sets ‘FL’ (Florida), and for countries, it sets ‘US’ (United States).
  • The last line saves the changes made to the customer object, effectively updating the user’s billing and shipping information in the WooCommerce database.

Overall, this code ensures that certain billing and shipping fields are populated with default values for newly registered users on a WooCommerce site.

/**
 * @snippet       Shipping/Billing Defaults @ WooCommerce New User
 * @tutorial      Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 8
 * @community     Join https://businessbloomer.com/club/
 */

add_action( 'user_register', 'bbloomer_populate_billing_shipping_new_customer' );

function bbloomer_populate_billing_shipping_new_customer( $user_id ) {
	$fields = array( 'billing_state', 'billing_country', 'shipping_state', 'shipping_country' );
	$customer = new WC_Customer( $user_id );
	foreach ( $fields as $prop ) {
		if ( is_callable( array( $customer, 'set_' . $prop ) ) ) {
			if ( $prop == 'billing_state' || $prop == 'shipping_state' ) {
				$customer->{"set_$prop"}( 'FL' );
			} elseif ( $prop == 'billing_country' || $prop == 'shipping_country' ) {
				$customer->{"set_$prop"}( 'US' );
			}
		}
	}
	$customer->save();
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: “You Only Need $$$ to Get Free Shipping!” @ Cart
    This is a very cool snippet that many of you should use to increase your average order value. Ecommerce customers who are near the “free shipping” threshold will try to add more products to the cart in order to qualify for free shipping. It’s pure psychology. Here’s how we show a simple message on the […]
  • WooCommerce: Weight-Based Shipping Methods
    With WooCommerce you get 3 default shipping methods: Flat Rate, Free Shipping, Local Pickup. For each one you can define a cost, however there is no way to set up some “weight” restrictions. So, what if you want to display a rate for orders below 10 kg, and another shipping rate for orders above that […]
  • WooCommerce: Hide Shipping Method If Shipping Class Is In The Cart
    Our goal is to check if a Product with a specific Shipping Class is in the Cart, and consequently disabling a shipping rate such as Free Shipping if this is true. This is super useful when there are multiple items in the cart and you don’t want to give free shipping for certain orders for […]
  • WooCommerce: Hide Shipping Rates if Free Shipping Available
    If Free Shipping is available, you possibly don’t want to show the other premium shipping options. WooCommerce shows by default all shipping rates that match a given shipping zone, so it’s not possible to achieve this from the settings alone. Thankfully, the “woocommerce_package_rates” filter allows us to manipulate the shipping rates before they are returned […]
  • WooCommerce: Hide Shipping If Local Pickup Is Selected
    Let’s talk about checkout UX: if a user is willing to pick up the item in store, why should there be a shipping form on the checkout? Well, let’s see how we can hide this dynamically with a bit of PHP and JS!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *