Skip to content

Instantly share code, notes, and snippets.

@shameemreza
Last active December 16, 2025 07:19
Show Gist options
  • Select an option

  • Save shameemreza/d5843f10c29fa46711d2c3cb903046d3 to your computer and use it in GitHub Desktop.

Select an option

Save shameemreza/d5843f10c29fa46711d2c3cb903046d3 to your computer and use it in GitHub Desktop.
Adds the product’s Global Unique ID to both the regular order view and the order preview in the WooCommerce admin. Useful for stores or integrations that rely on unique product identifiers beyond SKUs.
/**
* Display Global ID (GTIN) in WooCommerce Order Admin
* Works for both simple and variable products (variations)
*
* @since 1.0.0
*/
// Display Global ID in regular order view (order details page)
add_action( 'woocommerce_after_order_itemmeta', function( $item_id, $item, $product ) {
// Only show in admin and for line items
if ( ! is_admin() || ! $item->is_type( 'line_item' ) ) {
return;
}
// Get the product (for variable products, this returns the variation)
$product = $item->get_product();
if ( ! $product || ! method_exists( $product, 'get_global_unique_id' ) ) {
return;
}
$unique_id = $product->get_global_unique_id();
if ( $unique_id ) {
echo '<div class="wc-order-item-global-id" style="color: #888; font-size: 12px; margin-top: 4px;">';
echo '<strong>' . esc_html__( 'Global ID:', 'woocommerce' ) . '</strong> ' . esc_html( $unique_id );
echo '</div>';
}
}, 10, 3 );
// Filter the order preview HTML to add Global ID after SKU for each product
add_filter( 'woocommerce_admin_order_preview_get_order_details', function( $order_details, $order ) {
// Get order items
$line_items = $order->get_items();
// Build a map of item_id => global_id (using item_id, not product_id)
$item_global_ids = array();
foreach ( $line_items as $item_id => $item ) {
$product = $item->get_product();
if ( ! $product || ! method_exists( $product, 'get_global_unique_id' ) ) {
continue;
}
$unique_id = $product->get_global_unique_id();
if ( $unique_id ) {
$item_global_ids[ $item_id ] = $unique_id;
}
}
// Only proceed if we found any Global IDs
if ( empty( $item_global_ids ) ) {
return $order_details;
}
$html = $order_details['item_html'];
// For each item with a Global ID, find its specific row and add the Global ID
foreach ( $item_global_ids as $item_id => $global_id ) {
// Each item row has a unique class: wc-order-preview-table__item--{item_id}
// Find the SKU div within this specific row and add Global ID after it
$row_pattern = '/(<tr[^>]*wc-order-preview-table__item--' . preg_quote( $item_id, '/' ) . '[^>]*>.*?<div class="wc-order-item-sku">.*?<\/div>)/s';
$global_id_div = '<div class="wc-order-item-global-id" style="color: #888; font-size: 12px;"><strong>Global ID:</strong> ' . esc_html( $global_id ) . '</div>';
// Replace by adding Global ID div after the SKU div for this specific item
$html = preg_replace(
$row_pattern,
'$1' . $global_id_div,
$html,
1 // Only replace once per item
);
}
// Update the item HTML in the order details
$order_details['item_html'] = $html;
return $order_details;
}, 10, 2 );
@shameemreza
Copy link
Author

CleanShot 2025-12-10 at 07 05 51

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment