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).
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:
function my_update_wc_user_fields( $id, $args = array() ) { if ( ! empty( $args ) ) { foreach ( $args as $key => $value ) { update_user_meta( $id, $key, $value ); } } }
And this is how you can call this function to update your WooCommerce customer address with a different address:
$args = array( 'billing_address_1' => '', 'billing_address_2' => '', 'billing_city' => 'My city', 'billing_company' => '', 'billing_country' => '', 'billing_email' => '', 'billing_first_name' => '', 'billing_last_name' => '', 'billing_phone' => '', 'billing_postcode' => '', 'billing_state' => 'My State', 'shipping_address_1' => '', 'shipping_address_2' => '', 'shipping_city' => '', 'shipping_company' => '', 'shipping_country' => '', 'shipping_email' => '', 'shipping_first_name' => '', 'shipping_last_name' => '', 'shipping_postcode' => '', 'shipping_state' => '', ); my_update_wc_user_fields( 13, $args );
Likewise, you can get all address-related fields with this function:
function my_get_wc_user_fields( $id, $echo = false ) { $fields = array( 'billing_address_1', 'billing_address_2', 'billing_city', 'billing_company', 'billing_country', 'billing_email', 'billing_first_name', 'billing_last_name', 'billing_phone', 'billing_postcode', 'billing_state', 'shipping_address_1', 'shipping_address_2', 'shipping_city', 'shipping_company', 'shipping_country', 'shipping_email', 'shipping_first_name', 'shipping_last_name', 'shipping_postcode', 'shipping_state', ); $return = array( 'billing' => array(), 'shipping' => array(), ); $meta = get_user_meta( $id ); if ( ! empty ( $id ) && ! empty ( $meta ) ) { foreach ( $fields as $field ) { if ( is_array( $meta[ $field ] ) && ! empty( $meta[ $field ][0] ) ) { $key = explode('_', $field); if ( ! empty( $key[1] ) && is_array( $return[ $key[0] ] ) ) { $return[ $key[0] ][ $key[1] ] = $meta[ $field ][0]; } $return[$field] = $meta[ $field ][0]; if ( $echo ) { echo "$field : {$meta[ $field ]} <br />"; } } } } return $return; } // Use it as $var = my_get_wc_user_fields( $id ); or simply my_get_wc_user_fields( $id, 1 );
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.