Created
December 8, 2025 10:45
-
-
Save shameemreza/e5c49467ebc4585597afb17b9f001b7f to your computer and use it in GitHub Desktop.
Adds a GTIN column to the WooCommerce Products table, making it sortable and searchable.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Add GTIN Column to WooCommerce Products List Table | |
| * | |
| * Adds a sortable, searchable GTIN column to Products > All Products. | |
| */ | |
| // Add GTIN column to products list (positioned after SKU). | |
| add_filter( 'manage_edit-product_columns', function( $columns ) { | |
| // Find SKU position and insert after it. | |
| $sku_position = array_search( 'sku', array_keys( $columns ), true ); | |
| if ( false !== $sku_position ) { | |
| return array_slice( $columns, 0, $sku_position + 1, true ) | |
| + array( 'gtin' => __( 'GTIN', 'woocommerce' ) ) | |
| + array_slice( $columns, $sku_position + 1, null, true ); | |
| } | |
| // Fallback: Insert after Name if SKU doesn't exist. | |
| $name_position = array_search( 'name', array_keys( $columns ), true ); | |
| if ( false !== $name_position ) { | |
| return array_slice( $columns, 0, $name_position + 1, true ) | |
| + array( 'gtin' => __( 'GTIN', 'woocommerce' ) ) | |
| + array_slice( $columns, $name_position + 1, null, true ); | |
| } | |
| $columns['gtin'] = __( 'GTIN', 'woocommerce' ); | |
| return $columns; | |
| }, 20 ); | |
| // Populate GTIN column with data. | |
| add_action( 'manage_product_posts_custom_column', function( $column_name, $post_id ) { | |
| if ( 'gtin' !== $column_name ) { | |
| return; | |
| } | |
| $product = wc_get_product( $post_id ); | |
| if ( ! $product ) { | |
| echo '<span class="na">–</span>'; | |
| return; | |
| } | |
| $gtin = $product->get_global_unique_id(); | |
| echo $gtin ? esc_html( $gtin ) : '<span class="na">–</span>'; | |
| }, 10, 2 ); | |
| // Make GTIN column sortable. | |
| add_filter( 'manage_edit-product_sortable_columns', function( $columns ) { | |
| $columns['gtin'] = 'by_gtin'; | |
| return $columns; | |
| } ); | |
| // Handle GTIN column sorting. | |
| add_action( 'pre_get_posts', function( $query ) { | |
| if ( ! is_admin() || ! $query->is_main_query() ) { | |
| return; | |
| } | |
| if ( 'by_gtin' === $query->get( 'orderby' ) ) { | |
| $query->set( 'meta_key', '_global_unique_id' ); | |
| $query->set( 'orderby', 'meta_value' ); | |
| } | |
| } ); | |
| // Enable search by GTIN. | |
| add_filter( 'woocommerce_product_search_fields', function( $fields ) { | |
| $fields[] = '_global_unique_id'; | |
| return $fields; | |
| } ); | |
| // Add CSS for proper column width. | |
| add_action( 'admin_head', function() { | |
| $screen = get_current_screen(); | |
| if ( ! $screen || 'edit-product' !== $screen->id ) { | |
| return; | |
| } | |
| ?> | |
| <style> | |
| .widefat .column-gtin { | |
| width: 10%; | |
| min-width: 80px; | |
| } | |
| </style> | |
| <?php | |
| } ); |
Author
shameemreza
commented
Dec 8, 2025

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