Created
December 8, 2025 10:52
-
-
Save shameemreza/bbe9fa1f494cc4055649f98bebc526ee to your computer and use it in GitHub Desktop.
Adds a GTIN field to the WooCommerce Quick Edit panel, allowing store managers to view and update GTIN values directly from the product list
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 Field to WooCommerce Quick Edit Panel | |
| * | |
| * Allows viewing and editing GTIN from the Quick Edit dropdown. | |
| */ | |
| /** | |
| * Get the GTIN field label (same as product settings). | |
| * | |
| * @return string | |
| */ | |
| function wcs_get_gtin_label() { | |
| // translators: %1$s GTIN %2$s UPC %3$s EAN %4$s ISBN. | |
| return sprintf( | |
| __( '%1$s, %2$s, %3$s, or %4$s', 'woocommerce' ), | |
| '<abbr title="' . esc_attr__( 'Global Trade Item Number', 'woocommerce' ) . '">' . esc_html__( 'GTIN', 'woocommerce' ) . '</abbr>', | |
| '<abbr title="' . esc_attr__( 'Universal Product Code', 'woocommerce' ) . '">' . esc_html__( 'UPC', 'woocommerce' ) . '</abbr>', | |
| '<abbr title="' . esc_attr__( 'European Article Number', 'woocommerce' ) . '">' . esc_html__( 'EAN', 'woocommerce' ) . '</abbr>', | |
| '<abbr title="' . esc_attr__( 'International Standard Book Number', 'woocommerce' ) . '">' . esc_html__( 'ISBN', 'woocommerce' ) . '</abbr>' | |
| ); | |
| } | |
| // Add hidden GTIN data for Quick Edit JavaScript. | |
| add_action( 'manage_product_posts_custom_column', function( $column_name, $post_id ) { | |
| if ( 'name' !== $column_name ) { | |
| return; | |
| } | |
| $product = wc_get_product( $post_id ); | |
| if ( ! $product ) { | |
| return; | |
| } | |
| $gtin = $product->get_global_unique_id( 'edit' ); | |
| ?> | |
| <div class="hidden wcs_gtin_inline_data"> | |
| <span class="gtin"><?php echo esc_html( $gtin ); ?></span> | |
| </div> | |
| <?php | |
| }, 99, 2 ); | |
| // Add GTIN field to Quick Edit form. | |
| add_action( 'woocommerce_product_quick_edit_start', function() { | |
| ?> | |
| <label class="wcs-gtin-quick-edit"> | |
| <span class="title"><?php echo wp_kses_post( wcs_get_gtin_label() ); ?></span> | |
| <span class="input-text-wrap"> | |
| <input type="text" name="_global_unique_id" class="text" value=""> | |
| </span> | |
| </label> | |
| <br class="clear" /> | |
| <?php | |
| } ); | |
| // JavaScript to populate GTIN field when Quick Edit opens. | |
| add_action( 'admin_footer', function() { | |
| $screen = get_current_screen(); | |
| if ( ! $screen || 'edit-product' !== $screen->id ) { | |
| return; | |
| } | |
| ?> | |
| <script type="text/javascript"> | |
| jQuery(function($) { | |
| $('#the-list').on('click', '.editinline', function() { | |
| var $row = $(this).closest('tr'); | |
| var $gtinData = $row.find('.wcs_gtin_inline_data'); | |
| var gtin = $gtinData.length ? $gtinData.find('.gtin').text() : ''; | |
| setTimeout(function() { | |
| $('input[name="_global_unique_id"]', '.inline-edit-row').val(gtin); | |
| }, 10); | |
| }); | |
| }); | |
| </script> | |
| <?php | |
| } ); | |
| // Save GTIN from Quick Edit - hook after WooCommerce saves. | |
| add_action( 'woocommerce_product_quick_edit_save', function( $product ) { | |
| // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified by WooCommerce. | |
| if ( ! isset( $_POST['_global_unique_id'] ) ) { | |
| return; | |
| } | |
| // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| $new_gtin = sanitize_text_field( wp_unslash( $_POST['_global_unique_id'] ) ); | |
| $old_gtin = $product->get_global_unique_id( 'edit' ); | |
| // Only update if changed. | |
| if ( $new_gtin === $old_gtin ) { | |
| return; | |
| } | |
| $post_id = $product->get_id(); | |
| // If empty, allow clearing. | |
| if ( '' === $new_gtin ) { | |
| $product->set_global_unique_id( '' ); | |
| $product->save(); | |
| return; | |
| } | |
| // Validate uniqueness before setting. | |
| if ( wc_product_has_global_unique_id( $post_id, $new_gtin ) ) { | |
| $product->set_global_unique_id( $new_gtin ); | |
| $product->save(); | |
| } | |
| }, 10, 1 ); |
Author
shameemreza
commented
Dec 8, 2025

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