Order Tags stores tags as a WordPress taxonomy, copies each order’s tags into order meta for exports and integrations, and has a few filters and functions for custom code. Add filters in a small custom plugin or your theme’s functions.php. For setting up tags, see Get started with Order Tags.
How tags are stored
Tags are terms in the hierarchical wcot_order_tag taxonomy, registered on the shop_order type. This works with High-Performance Order Storage too, so tags are kept in WordPress’s standard term tables either way. Each tag’s colors and rules are stored as term meta.
Order Tags adds no REST API endpoint, block or shortcode, and nothing shows on the storefront or in emails.
Order meta for exports and integrations
Whenever an order’s tags change, Order Tags writes them to the order meta key _wcot_order_tags as a comma-separated list of tag slugs (each tag’s Unique reference), for example:
new-customer,order-over-100,free-gift
The key is removed when an order has no tags. Read it with the order object, which works with either order storage:
$order = wc_get_order( $order_id );
$tags = $order ? $order->get_meta( '_wcot_order_tags' ) : '';
Because it’s ordinary order meta, it’s also included in the meta_data of orders returned by the WooCommerce REST API (/wp-json/wc/v3/orders).
Because it’s one string, filter on it with a "contains" rule in export tools, such as _wcot_order_tags contains free-gift. Don’t edit the key yourself: Order Tags overwrites it the next time the order’s tags change.
To change tags from code, use the functions below rather than writing the meta.
Filters
wcot_order_tags_set_if_product_variations
Default false. Return true to make individual variations selectable in a tag’s Product condition and to match orders by variation.
add_filter( 'wcot_order_tags_set_if_product_variations', '__return_true' );
wcot_order_tags_set_if_customer_order_count_all
Default false. Return true to work out the Customer order count condition from every order with the same billing email, which includes guest orders.
add_filter( 'wcot_order_tags_set_if_customer_order_count_all', '__return_true' );
wcot_order_tags_customer_roles
Default [ 'customer' ]. The user roles listed in a tag’s Customer condition.
add_filter( 'wcot_order_tags_customer_roles', function( $roles ) {
$roles[] = 'wholesale_customer';
return $roles;
} );
wcot_order_tags_color_default
Default #000000. The background color of tags with no Color background.
wcot_order_tags_color_text_default
Default #ffffff. The text color of tags with no Color text.
wcot_order_tags_display_type
Default empty. Return minimal to show tags smaller in the orders list and on the order screen, without the × remove buttons.
add_filter( 'wcot_order_tags_display_type', function() {
return 'minimal';
} );
wcot_order_tags_bulk_actions_set_order_tags
Default true. Return false to hide the Set order tag options from the orders list’s bulk actions.
wcot_order_tags_bulk_actions_remove_order_tags
Default true. Return false to hide the Remove order tag options from the orders list’s bulk actions.
wcot_order_tag_taxonomy_args
Filters the arguments passed to register_taxonomy() for wcot_order_tag.
Functions
| Function | Returns |
|---|---|
wcot_order_tags_get_all_order_tags( $hierarchical = false ) | Every tag, as an array of term objects (nested when $hierarchical is true) |
wcot_order_tags_get_order_tags( $order_id, $hierarchical = false ) | An order’s tags, as an array of term objects |
wcot_order_tags_set_order_tag( $order_id, $term_id ) | Adds a tag to an order. Returns an array of term taxonomy IDs, a WP_Error, or false when the tag’s exclusions match the order |
wcot_order_tags_remove_order_tag( $order_id, $term_id ) | Removes a tag from an order. Returns true, false or a WP_Error |
wcot_order_tags_make_hierarchical( $order_tags ) | The given term objects nested by parent, each with a children array |
wcot_order_tags_get_order_tags_html( $order_tags, $markup = '', $depth = 0, $order_id = 0 ) | HTML for a list of tags, grouped by parent |
wcot_order_tags_get_order_tag_html( $order_tag, $order_id = 0 ) | HTML for one tag |
The functions don’t check user capabilities, so check them yourself before acting on a request.
Keep learning
- Get started with Order Tags (creating tags, tagging by hand and the bulk page)
- Order Tags automatic tagging rules (every Set on and Set if option)
