Skip to content

Instantly share code, notes, and snippets.

@Tusko
Created June 4, 2021 11:04
Show Gist options
  • Save Tusko/e44a150234772b92f2f2e911105664e4 to your computer and use it in GitHub Desktop.
Save Tusko/e44a150234772b92f2f2e911105664e4 to your computer and use it in GitHub Desktop.
Get access by product SKU for Woocommerce
<?php
//place code in the end of functions.php in theme
add_action('template_redirect', 'wc_sku_redirect');
function wc_sku_redirect() {
$parseUrl = explode('/', $wp->request);
if(isset($parseUrl[0]) && $parseUrl[0] === 'product') {
$pid = wc_get_product_id_by_sku($parseUrl[1]);
if($pid) {
wp_safe_redirect(get_permalink($pid));
exit;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment