How to programmatically set the billing and shipping details in WooCommerce

In this tutorial we will look into how to set shipping and billing address of WooCommerce order programmatically. Shipping address is set by the user in WooCommerce checkout page. However, there may be a case where you wish to set shipping address in WooCommerce site programatically (depending upon user role for example) for your ecommerce store.

Shipping address is set by the customer in WooCommerce checkout page after the WooCommerce cart. However, there may be a case where you wish to set shipping address in WooCommerce site programatically (depending upon user role for example) for your WooCommerce store.

WooCommerce allows setting shipping address for orders programmatically through its’ action & filter hooks. In this guide, we will be discussing how to set shipping address in Woocommerce using one of the built-in filters. This will allow you to change shipping address and WooCommerce shipping options in bulk at a time (which is not easily possible using set-cart feature).

woocommerce set shipping address programmatically

At its core, the billing address and shipping fields are just custom user fields. And although you can use the WC_Customer class to set each of them individually, it’s much easier to simply use the WordPress functions to update user meta.

That’s because each individual field has its own method in the WC_Customer class. On the other hand, we can use the same update_user_meta function if we use the WordPress core only.

 

We have these fields to update:

  • Address ( billing_address_1 , billing_address_2 , shipping_address_1 , shipping_address_2 )
  • City ( billing_city , shipping_city )
  • State ( billing_state , shipping_state )
  • Country ( billing_country , shipping_country )
  • Postcode ( billing_postcode , shipping_postcode )

And it can be done with an array, interacting through all the valid fields we provide.

Here is an example:

And this is how you can call this function to update your WooCommerce customer address with a different address:

Likewise, you can get all address-related fields with this function:

 

But just creating a customer and updating their data is not enough. We need to add orders to these customers. Let’s do it now.

Unlock the Full Potential of Your WooCommerce Customer Data

Conclusion

When a store is just starting out, it’s not uncommon for the store owner to handle all online customer service themselves and personally ship products to customers. As sales pick up and orders begin to flood in however, this can become a very time consuming and costly business practice. With time store owners should focus more on automation and optimization in order to make their business more efficient. Today we dived into how you can set shipping address in WooCommerce with code. This allows you to programmatically set shipping address and edit them. We hope this helps you to automate some of your process.