WooCommerce: Editing the Variation / Product Meta on the Admin Order Page

August 4, 2021
Developers, PHP, Wordpress
LinkedInTwitterFacebookEmail

WooCommerce: love it or hate it, it’s the largest eCommerce plugin for WordPress out there. The best thing about WooCommerce, in my opinion, is the vast amount of filters and actions in the plugin that allow customization across the board.

In this article, I’ll show you how to use two useful ones.

Admin Order Page

Customizing the way Product Meta, including variations, shows on the admin’s order page (post-new.php?post_type=shop_order page) is rendered possible by two neat little filters added into the get_formatted_meta_data¬†function in WooCommerce and /includes/class-wc-ajax.php file.

Why would you want to customize the variation for the admins?

Before we dive in, let’s talk about WHY you’d need to customize the variation name for the admins of the site. Perhaps your variations use numbers or unix timestamps (like we worked with!). Or maybe you have variations that are difficult to distinguish from one another. Maybe you need to add an alert to the order if it has a variation.

All of these (and more!) can be done using the filters below.

Both of the options below are specifically on a SINGLE ORDERS page in the backend of a WordPress with WooCommerce site.

Admin Orders Page: Filter for the “Add Product” Search Results

This first filter is going to filter how a product name shows up when you click to ADD ITEMS > ADD PRODUCT(S). This filter is found: /includes/class-wc-ajax.php on line 1600, at the end of the json_search_products function.

Clicking the Add Item > Add Product Buttons

This change is primarily for if the administrator is creating orders in the backend of the site. In the code below, you’ll need to cycle through the products returned and change the entire name of the product + variation.

function wfc_product_name( $products ) {
    foreach($products as $i => $p){
        $newProductNameVariation = 'however you want to manipulate the product name goes here';
        $products[$i] = $newProductNameWithVariation;
    }
    return $products;
}
add_filter( 'woocommerce_json_search_found_products', 'wfc_product_name', 10, 3 );

This filter is found when the product name is returned to the search results. The $products variable is automatically passed to it. This array variable contains the entire title and variation of each product in each item ($p above).

You’ll need to manipulate the product name (line 3) and then add the new product name into the $products variable (line 4) before you return it (line 7).

That’s it! Easy enough once you know the filter name.

Admin Orders Page: Filter the Variation Name

This second filter will display how the meta values (like the variation) is displayed on the Single Order page. This filter is found: get_formatted_meta_data function in WooCommerce. You can spot the filter right at the end of that function.

As you can see below, the product name “Testing a new test” (brilliant, I know), has a Performance Date variation below. However, that variation is stored as a unix timecode to be easily manipulated. I need to display it as a readable human time, and so the filter:

showing the variation of the product on the Order Page
function wfc_productNameOrder( $display_value, $meta ) {
    $display_value = 'manipulate as you want here';
    return $display_value;
}
add_filter( 'woocommerce_order_item_display_meta_value', 'wfc_productNameOrder', 10, 2 );

This order item filter is fairly straightforward. It passes the $display_value and $meta variables to the function automatically. The $display_value is simply the actual variation name (simple string or number), so manipulate away.

Summary

It can take some work to track down the filters needed in the WooCommerce plugin, but you might find it easier than you’d expect. And those filters are life savers when it comes to manipulating product names, order products, or anything in the WooCommerce system.

Join the Discussion

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>