How to Update WooCommerce Order Address Dynamically

How to Update WooCommerce Order Address Dynamically

Adam Morgan

When you are working on JavaScript during a WordPress redesign you often come across problems.  In this case, there was a problem relating to order addresses using the WooCommerce plugin.  This problem came with a simple solution, however, it can take a while to implement successfully.  Here, we’ll talk you through to update your WooCommerce plugin order address dynamically.

What is WooCommerce?

For those of you unfamiliar with the WooCommerce plugin, it is one of the best free eCommerce plugins available on the WordPress platform.  This plugin enables you to sell services and products through your WordPress website.  It is a free WordPress plugin and the available features it offers come as paid add-ons.  WooCommerce makes selling a product affordable and accessible to any WordPress user.  With the help of this plugin you are able to sell both digital and physical products, manage your inventory and shipping, process secure payments and sort out any taxes automatically.  Yes, this really is a wonder service!  Plus, using this plugin enables you to keep complete control over all of your WordPress data and means you control the scalability of your site.

Updating WooCommerce Plugin Order Address Dynamically

If you choose to remove some of the default fields during the checkout process in your WooCommerce plugin, you can run into problems when it comes to downloading an invoice.  If you are selling a digital product you won’t require shipping details and the only billing details you will need is the country and VAT Code, alongside your email and password.

The issue with this is that if you want to download an invoice it will not contain some of the necessary information.  A quick fix would be for users to update their address in the My Account area, then your order details will update as well.  This is not possible using the WooCommerce plugin.  In WooCommerce, any billing and shipping details are stored in the post meta.  This means you will have to hook in when a user saves their billing address and update WooCommerce’s post meta to reflect this.

The Solution

WordPress users can create a code that will hook into the woocommerce_customer_save_address action hook.  This will then provide access to the customer user ID.  Next, you will need to retrieve the meta date for your customer using get_user_meta().  In addition, you will then need to find the order for that customer using get_posts().  From there, you can loop through the orders and update the meta data to match the customer’s saved billing address.

It’s important to point out that WooCommerce chooses to use post meta data for a reason.  If a user chooses to update their billion information, it doesn’t necessarily mean they want to replace their past billing information or replace the billing address for any orders they have placed.  This is a quick solution to a very specific problem.  However, it does enable you to update your WooCommerce plugin order address.

Have you come across this same issue when using the WooCommerce plugin?  Let us know in the comments below.

Adam Morgan

Author Adam Morgan

More posts by Adam Morgan