Redirect SKU from URL Directly to Specific Product Variation in WooCommerce

This code redirects a SKU from the URL directly to a specific product variation in WooCommerce. Add the code to the end of your theme’s functions.php file or in a custom functionality/code snippets plugin.

Or, you can download the file.

The PHP code uses the template_redirect action hook to check if the current page is a single product page. It then retrieves the product ID and the requested SKU from the URL. If a SKU is provided, it searches for a matching variation and redirects to the variation’s permalink.

Note: This code assumes that the SKU is passed as a query parameter in the URL (e.g., example.com/product-page/?sku=your-sku). Modify the code accordingly if your URL structure is different.

The code also works for simple products.

Was This Tutorial Helpful?

Free

$0

Access only to all free tutorials per month.



Monthly

$75

Access to 10 premium tutorials per month.


Tutorial Request


Includes code guarantee and coding support.

Yearly

$500

Access to 15 premium tutorials per month.


Monthly Tutorial Request


Includes code guarantee and priority coding support.