*.meta type of placeholders - Display any value from WordPress
Among placeholders, there's one that has "superpowers" of accessing any data stored in a meta table.
What is metadata?
A brief explanation for those, who aren't familiar with WordPress's process of adding metadata to objects (posts, users, products). When it comes to the post family (which consists of post, page, but also order and product for WooCommerce) the basic data is stored in the wp_posts
table in the database, and any extra data is associated with the post in the wp_postmeta
table.
Usually, the most interesting data is stored there. Think of it as a container for anything related to your customers, orders, or products. For example, the billing email or billing name is stored in wp_postmeta
table, exactly as metadata associated with the order you need.
Classic WordPress and WooCommerce metadata isn't the only use case of this behavior. Advanced Custom Fields also uses extensively meta table to save your data.
How to access metadata?
When you want to access metadata from PHP, i.e. in functions.php
of your theme, usually you would write this as:
(This is only a mere example of retrieving post meta, don't actually copy that to your production code!)
/** \WP_Post $product Instance of WP_Post related with the product you need. */
function get_product_color_name( $product ) {
$id = $product->ID;
$meta = get_post_meta($id, '_product_color', true);
return $meta;
}
As you have the ID of the post you want metadata for, the most important element is the key to get it. In the example, we've used _product_color
as our meta key. Pay attention to the underscore starting the name of the key - saving any value this way treats meta as private. Private meta keys aren't easily accessed through standard WooCommerce methods (think of $product->get_meta('product_color')
), that is because WooCommerce intentionally restricts your access to data saved as private and there's a good reason to do so.
In the case of using our meta placeholders it doesn't matter whether it's private or public, as long as you remember to use the proper name - never forget the underscore if data is saved with one!
Using *.meta placeholders
*.meta type of placeholders enables you to reach for any value you need, which makes it a perfect example for programmatically enhanced use cases.
Let's start simple, to acquaint you with the usage. You can access standard WooCommerce data using this way. {{ order.billing_email }}
can product the same output as {{ order.meta | key: '_billing_email' }}
. As mentioned before, most of the data is stored in meta tables, so actually placeholder billing_email (or PHP call $order->get_billing_email()
) in fact gets meta value associated with key _billing_email
.
This type of placeholder has one required parameter: key. It works the same way as the aforementioned PHP function get_post_meta
call, where you need to specify the key of meta. Make sure to use the correct name, paying attention to the underscore, if it should be present.
Available *.meta placeholders
ShopMagic consists of multiple *.meta placeholders for different use cases. The most valuable is {{ order.meta }}
but there are also:
{{ product.meta }}
- used in Review Requests add-on{{ subscription.meta }}
- used in WooCommerce Subscriptions add-on{{ membership.meta }}
- used in WooCommerce Memberships add-on{{ bookings.meta }}
- used in WooCommerce Bookings add-on
Usage constraints
*.meta placeholder restricts you to usage only of simple variable types in the meta field. You cannot access an array through this placeholder, so metadata like the following is forbidden.
$meta = [
'name' => 'My Name',
'phone' => '123123123',
// more values in an array
];
It is because any complex data structure is serialized before being saved to the database and for the time being, it would be burdensome to access a specific field from meta placeholder. Anyway, if you still feel the need to access array data, we encourage you to create a custom placeholder, following our guide, which will enable you to process the data, the way you want!
Where to find the meta key?
Most of the time, you will likely use your own code solutions for saving meta value to the database. Then, you definitely know best how your meta key is named, and it will be easy for you to reference it in a placeholder call.
Yet, sometimes you may rely on external tools like Advanced Custom Fields, which already have its own way of storing data in the database. In such cases, there are two ways of finding out what key you should specify in the placeholder.
First is simple - just get down to documentation of the tool you use, and try to find out how they save data. Here's an example of ACF.
Unfortunately, sometimes documentation lacks that information, so your best bet would be to dig into the database. Try to find the ID of the item you are interested in and look through the wp_postmeta
table for any keys associated with the specified ID.