Skip to content

Instantly share code, notes, and snippets.

@kuokoad
Forked from mikejolley/functions.php
Created August 30, 2023 10:36
Show Gist options
  • Save kuokoad/bff730762b7dbfebdb57f9b343332d0c to your computer and use it in GitHub Desktop.
Save kuokoad/bff730762b7dbfebdb57f9b343332d0c to your computer and use it in GitHub Desktop.
WooCommerce - Redirect external products offsite (disable single listings)
<?php // Do not include this if already open!
/**
* Code goes in theme functions.php.
*/
add_action( 'template_redirect', 'redirect_external_products' );
function redirect_external_products() {
global $post;
if ( is_singular( 'product' ) && ! empty( $post ) && ( $product = wc_get_product( $post ) ) && $product->is_type( 'external' ) ) {
wp_redirect( $product->get_product_url() );
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment