WooCommerce: Redirect URL With SKU To Single Product Page

Let’s say your WooCommerce product has the following permalink: example.com/shop/t-shirts-tops-and-blouses/t-shirt-casual-v-neck-longsleeve-green-medium-cotton-stretch

Wouldn’t it be shorter, more fun and neater if you could also reach the same exact single product page by using example.com/sku00001 in the browser bar, where “sku00001” is the product SKU?

Well, in today’s snippet we will create a simple redirect from a custom URL that contains the product SKU to the product page, so that you can use the URL “shortener in your email marketing, display advertising, blog posts and anywhere you want to show a “pretty link” instead of a full, long, boring product page URL.

Enjoy!

If I enter example.com/SKU I want to be redirected to the single product page. Let’s code this!

PHP Snippet: Redirect “example.com/sku” To The WooCommerce Single Product Page

/**
 * @snippet       Redirect SKU in URL to Woo Product Page
 * @tutorial      Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 8
 * @community     Join https://businessbloomer.com/club/
 */

add_action( 'template_redirect', 'bbloomer_redirect_sku_in_url_to_product' );

function bbloomer_redirect_sku_in_url_to_product() {
	if ( is_404() && ( $sku = $GLOBALS['wp']->request ) && ( $id = wc_get_product_id_by_sku( $sku ) ) ) {
		wp_redirect( get_permalink( $id ) );
		exit;
	}
}
  • The first line hooks the function bbloomer_redirect_sku_in_url_to_product to the template_redirect action. This means the function will be called whenever a WordPress template is redirected, which is typically on every page load.
  • The bbloomer_redirect_sku_in_url_to_product function itself is commented to explain its purpose, which is to redirect URLs containing a Stock Keeping Unit (SKU) to the corresponding product page.
  • Inside the function, we first check if the current page is a 404 error page using the is_404() function. This is because we only want to redirect URLs that result in a 404 error, which might be the case if someone types in a product SKU directly in the browser address bar.
  • If it is a 404 page, we then try to retrieve the request string from the global $wp object.
  • We then use the WooCommerce function wc_get_product_id_by_sku($sku) to try to get the product ID for the SKU in the request string.
  • If a product ID is found for the SKU, we use the WordPress function wp_redirect(get_permalink($id)) to redirect the user to the product’s permalink. The get_permalink($id) function retrieves the permanent URL for the product with the given ID.
  • Finally, we use the exit function to stop the function from executing any further code after the redirect.

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

9 thoughts on “WooCommerce: Redirect URL With SKU To Single Product Page

  1. Can you please check this code works fine in the latest version?

    1. Why, it doesn’t work for you? What’s your SKU format?

      1. I’m Using a 404 template for my “Not Found” pages and it causes the problem.
        When I try your code with

        is_404() &&

        removed, it works fine!
        Is there any other condition or function that detects it’s an 404 page template instead of a simple 404?

      2. SUKs are 6 digits.
        I’m using this code below for now (but I think it might cause problem without 404 check)

        add_action( 'template_redirect', 'bbloomer_redirect_sku_in_url_to_product' );
        
        function bbloomer_redirect_sku_in_url_to_product() {
           if ( ( $sku = $GLOBALS['wp']->request ) && ( $id = wc_get_product_id_by_sku( $sku ) ) ) {
              wp_redirect( get_permalink( $id ) );
              exit;
           }
        }
  2. Hi Rolf. This is really helpful.
    To tidy it up i am trying to put a message on the 404 page “We dont have ” $sku ” is the catalog” etc etc but cant get it to work. If i makethe url 404.php?12345 it works but i cant seem to retrieve the $sku from the 404 page which shows the url as mysite.com/12345

    any suggestions?

    1. Hello Peter, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. Once I disabled the media pages (which were blocking the required slugs, due to the product images being named after the SKUs, using this helpful little plugin: “disable-media-pages”), the code above worked like a charm!

    Thanks a ton for sharing! 🙂

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *