Created
June 4, 2021 11:04
-
-
Save Tusko/e44a150234772b92f2f2e911105664e4 to your computer and use it in GitHub Desktop.
Get access by product SKU for Woocommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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