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.
Leave a Reply