Nicola Mustone

Happiness Lead @ Automattic



How to Display Custom Product Attributes on the Edit Order Screen

rows of bottles of wine covered with dust

In WooCommerce, the admin order page is your command center. It’s where you glean insights at a glance and manage the heart of your e-commerce operations. But could it offer more? What if you could augment it with additional product details and direct links to the products themselves? In this post, I’ll walk you through a WooCommerce snippet that does precisely that, adding a layer of convenience and efficiency to your workflow.

Dive into the Code: A Peek Behind the Curtain

Originally, this snippet was not meant for the world – it was a personal solution to an efficiency problem I faced in my own store, Elemental Beacon, where I sell Dungeons & Dragons miniatures. With a catalog of detailed products, finding the exact location of product files was a daily challenge. That is until I embedded the solution directly into the WooCommerce Orders screen.

Seamless Integration for Enhanced Product Management

Before implementing any code changes, always ensure your site is backed up. This will help you restore things in case of unforeseen errors. I recommend using Jetpack Backup for real-time backups and one-click restores.

To take advantage of this enhancement, add this snippet to your theme’s functions.php file. If you’re not familiar with code, fear not. This file is readily accessible through your WordPress dashboard under Appearance > Theme Editor, or via an FTP client if you’re technically inclined.

<?php
add_action( 'woocommerce_after_order_itemmeta', 'admin_order_add_product_info_and_link', 10, 3 );
function admin_order_add_product_info_and_link( $item_id, $item, $_product ) {
if ( ! $_product ) return;
$product_link = get_permalink( $_product->get_id() );
$attributes_to_display = ['slug_1', 'slug_2', 'slug_3']; // Specify the attribute slugs you want to display
$target_product = $_product;
if ($_product->is_type('variation')) {
$parent_id = $_product->get_parent_id();
$target_product = wc_get_product($parent_id);
}
$attributes = $target_product->get_attributes();
echo '<hr>';
echo '<table cellspacing="0" class="display_meta"><tbody>';
foreach ( $attributes_to_display as $attr_slug ) {
if ( isset( $attributes['pa_' . $attr_slug] ) ) {
$attribute = $attributes[ 'pa_' . $attr_slug ];
$attr_label = wc_attribute_label( 'pa_' . $attr_slug );
if ( $attribute->is_taxonomy() ) {
$values = wc_get_product_terms( $target_product->get_id(), 'pa_' . $attr_slug, array( 'fields' => 'names' ) );
if ( ! empty( $values ) ) {
echo '<tr><th>' . $attr_label . '</th><td>' . join( ', ', $values ) . '</td></tr>';
}
} else {
// For non-taxonomy based attributes
$values = array_map( 'trim', explode( WC_DELIMITER, $attribute->get_options() ) );
echo '<tr><th>' . $attr_label . '</th><td>' . join( ', ', $values ) . '</td></tr>';
}
}
}
echo '</tbody></table>';
echo '<a href="' . esc_url( $product_link ) . '" target="_blank">' . __( 'View Product', 'woocommerce' ) . '</a>';
}
view raw functions.php hosted with ❤ by GitHub

Adapting the Snippet to Your WooCommerce Store

What makes this snippet truly shine is its adaptability. The array $attributes_to_display is your customizable key. Simply replace slug_1, slug_2, and slug_3 with the slugs of the attributes you need to track (make sure not to include the pa_ prefix it has). These could be any attributes relevant to your product management. You can add as many more or as few as you need.

The snippet also adds a “View Product” link to the order items that link to the product page on your site.

This is how it would look like, from my Elemental Beacon store:

A screenshot of the order items with custom product attributes from ElementalBeacon.com

A Snippet That Speaks Your Language

This snippet does more than just show additional data; it speaks the language of your business operations. Whether you deal in miniatures, apparel, or any niche, the fundamentals of e-commerce dictate that the right information at the right time is invaluable. By adopting this snippet, you’ll find your WooCommerce order management transformed, and the tedious task of order processing will become a journey worth taking each day.

Advertisements

Subscribe to This Blog

Receive new articles from this blog directly in your inbox! No spam guaranteed!

Join 651 other subscribers

Contribute to Improving This Blog

Did you enjoy this article? Was it helpful? Contribute to help me write more articles! The funds collected from this blog are reinvested directly into improving my skills or the blog so that I can provide more and better content!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

€5.00
€15.00
€100.00
€5.00
€15.00
€100.00
€5.00
€15.00
€100.00

Or enter a custom amount


Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly
Advertisements

More Posts That You Might Like…


Leave a Reply

Advertisements

Categories

Newsletter

Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Join 651 other subscribers

About The Author

Hi, I’m Nico! Support Lead at Automattic, championing WordPress.com & WooCommerce. Off-duty, I’m at Elemental Beacon, leading epic D&D adventures. Let’s connect, whether it’s about WordPress or a quest!

Advertisements

Don't Miss a Thing!

Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Join 651 other subscribers

Continue Reading

%d bloggers like this: