Skip to content

Instantly share code, notes, and snippets.

@grantambrose
Created February 12, 2024 02:49
Show Gist options
  • Save grantambrose/7a8a09e217013c4627241342433dd361 to your computer and use it in GitHub Desktop.
Save grantambrose/7a8a09e217013c4627241342433dd361 to your computer and use it in GitHub Desktop.
Redirect to Course if customer purchased a Woo Product
function c_has_purchased_product_ids($product_ids){
// debugging
// echo "Customer has bought product ids: " . $ids;
if ( ! $product_ids ) return false;
$current_user = wp_get_current_user();
// always show for admins
if(current_user_can('administrator')) return true;
if(is_array($product_ids)){
foreach ( $product_ids as $product_id ) {
if ( wc_customer_bought_product( wp_get_current_user()->user_email, get_current_user_id(), $product_id ) ) {
return true;
}
}
} elseif ( wc_customer_bought_product( wp_get_current_user()->user_email, get_current_user_id(), $product_ids ) ) {
return true;
}
return false;
}
add_action( 'template_redirect', 'c_redirect_to_purchased_course' );
function c_redirect_to_purchased_course() {
if( is_product() && get_field('c_related_online_course')){
$current_user = wp_get_current_user();
$related_course_id = get_field('c_related_online_course');
if ( wc_customer_bought_product( wp_get_current_user()->user_email, get_current_user_id(), get_the_ID() ) ) {
wp_redirect( get_permalink($related_course_id['0']) );
exit;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment