62
If you sell one-off products like online courses, lifetime memberships or unique pieces that can only be purchased once by a given customer, maybe you’d better redirecting the logged in customer who has purchased that product to a custom URL, such as the shop page, the “my courses” page for online courses or another customer-specific section.
With this easy snippet you’ll learn how to see if a user is logged in and has purchased a given product ID and then how to do a safe PHP redirect. Enjoy!
PHP Snippet: Redirect Customers Who Purchased Away From the Single Product Page
/**
* @snippet Redirect Customers to Custom URL - WooCommerce
* @how-to Get tutoraspire.com FREE
* @author Tutor Aspire
* @compatible WooCommerce 4.6
* @donate $9 https://tutoraspire.com
*/
add_action( 'template_redirect', 'tutoraspire_single_product_redirect_logged_in_purchased' );
function tutoraspire_single_product_redirect_logged_in_purchased() {
if ( ! is_product() && ! is_user_logged_in() ) return;
$current_user = wp_get_current_user();
$product_id = get_queried_object_id();
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id ) ) {
wp_safe_redirect( '/custom-url' );
exit;
}
}