WooCommerce and Custom Meta

...
Bryce Adams

One of the cooler things about WooCommerce (and WordPress) is the ability to save custom meta for orders, customers, subscriptions, and more.

While it has its problems - like with performance (since each bit of meta data takes up its own row in your database) - it offers you a level of flexibility you can’t quite get with other platforms, allowing you to store any data you’d like with ease.

Whenever an order is created, quite a lot is stored in the order’s meta data. A lot of it is your standard WooCommerce data like the billing address, order items, order total, which you’re probably going to want to leave alone. But you’ll also find that quite a few plugins, like the Stripe Payment Gateway, WooCommerce Subscriptions, Shipment Tracking, etc. store some data there.

So in this post I’m going to cover a few different aspects of custom meta, including:

  • Storing it (through the dashboard, Metorik, and code).

  • Reading it (through the dashboard, Metorik, and code).

  • Exporting it.

  • Segmenting by it.

  • Using it in email automations.

Storing & Reading It

In the WordPress dashboard, you’ll normally find the **Custom Fields** area at the bottom of each post on the left.

If you can’t see it, you may need to toggle the Screen Options for Custom Fields at the top of the page, like so:

From there, you can easily edit & delete existing meta, as well as add new meta:

If you’re using Metorik, you’ll also find it quite easy to store and update custom meta. Similarly, you’ll find it towards the bottom of order & customer pages and from there you can add new meta as well as edit existing meta.

When it comes to storing and reading custom meta through code, it’s a bit more work but surprisingly easy. You’ll want to familiarise yourself with 4 PHP functions provided by WordPress:

Let's look at the code required to add some custom meta to an order:

In the above example, we're adding some custom meta to the order #125 with the key payment_gateway_fee and the value 1.60.

Now if we wanted to use that post meta, we'd use the following code:

The 3rd argument of true is important when you know the custom meta is a single value. Otherwise, get_post_meta will return an array, even though the value is just a single value.

Updating and deleting post meta is quite similar, in that first you'll pass in the order ID, and then the key being updated or deleted.

When it comes to custom meta for customers, the process is similar but the functions you'll use are a little different as customers are actually WordPress users (and not WordPress posts like orders are):

However, as WooCommerce moves towards having custom tables, it's better to use the data stores provided by it. Please see this Wiki doc for more info on them. We'll be updating the code above soon to show some examples of using it.

Exporting It

Perhaps you've written a piece of code that sets some custom meta when an order is created. But now you have 1000's of orders with the custom meta and need a way to get it out of your system. The solution? Exporting it into a CSV.

You have a couple options. The first: Metorik. When performing an export (either an instant one or when setting up a recurring one), you can click the Settings tab and then choose which custom meta you'd like to include in the export:

You'll find this setting for orders, customers, and subscriptions. Each custom meta key you select will have its own column in the export.

We've written more about exporting WooCommerce data in this blog post here, if you're interested.

Another option is the WooCommerce Customer / Order CSV Export Plugin. This is a great option if you're looking to run the export on your own site, but has some limitations regarding performance (it can fail or impact your store when exporting a large amount of orders/customers and meta).

Segmenting by it

Segmenting is an interesting topic, worthy of its own post like one I recently wrote on the WooCommerce blog, but the idea behind it is quite simple. Through segmenting, we can find a subset of orders, customers, or subscriptions, that meet a certain set of rules (like only customers with a lifetime value over $100).

But what about segmenting by custom meta? For example, a custom meta field for 'How Did You Find Us?'. With Metorik, it's quite easy and only takes a moment. Here's an example of segmenting orders by a How Did You Find Us field and the value being Google:

Since Metorik's segmenting system allows for infinite stacked filters, you could add additional filters for other custom meta fields so it only shows us orders that match all of our filters:

Best of all, even though segmenting by custom meta is quite a technical and high-resource activity, when you do it with Metorik, it's all happening on Metorik's servers so your site is not impacted at all.

Using it in email automations

One last thing I wanted to mention is about using custom meta in email automations. With Metorik Engage, you can set up automated emails that send when an order, customer, or subscription has custom meta that matches a certain rule (eg. automatically follow up with customers that said they found you by Google), using the segmenting system from above.

More so, when it comes to writing the actual emails that get sent to customers, you can even use the custom meta values in the email templates:

Conclusion

You have options, but you'll need to consider what your ultimate goal is (eg. just segmenting or maybe exporting too). Either way, Metorik can definitely help when it comes to custom meta, especially for stores that have a lot of it. You can also learn more about Metorik's custom meta support in this help document.

Want to try Metorik today?

We're so sure you'll love Metorik that you can start right now, for free, without a credit card - and use the entire product for 30 days.