# Jump Straight to a Product When Searching Its SKU

> Sends shoppers directly to the product page when what they type in the search box is exactly a product's SKU or code.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, search, sku, redirects
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-exact-sku-search-redirect/

## Code

```php
add_action( 'template_redirect', function () {
    if ( ! is_search() || is_admin() ) {
        return;
    }

    $term = trim( get_search_query( false ) );

    if ( '' === $term || strlen( $term ) > 64 ) {
        return;
    }

    $id      = wc_get_product_id_by_sku( $term );
    $product = $id ? wc_get_product( $id ) : null;

    if ( ! $product || ( ! $product->is_visible() && ! $product->is_type( 'variation' ) ) ) {
        return;
    }

    $url = $product->get_permalink();

    if ( $url ) {
        wp_safe_redirect( $url );
        exit;
    }
} );
```

## Notes

Variation SKUs open the parent product with that variation preselected.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Jump Straight to a Product When Searching Its SKU" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
