It's straightforward to required registration globally in WooCommerce settings but for most of the products we sell, it's not necessary and I would rather restrict the logged in users.
However for one product I would like to require registration.
Something like
$product_id = $product->get_id();
if ($product_id !== 1024) {
remove_action( 'woocommerce_before_checkout_registration_form', 'action_woocommerce_checkout_registration_form', 10, 1 );
}
but it's obviously not that simple. Any ideas?
My Answer:
You can achieve this by using the `woocommerce_checkout_process` hook to check if the product being purchased is the specific product that requires registration. If it is, you can then check if the user is logged in and if not, redirect them to the login/register page.
Here's an example code snippet to achieve this:
php
add_action( 'woocommerce_checkout_process', 'require_registration_for_specific_product' );
function require_registration_for_specific_product() {
// Get the current product being purchased
$product_id = 1024;
// Check if the current product being purchased is the specific product that requires registration
if (WC()->cart->find_product_in_cart( $product_id )) {
// Check if user is not logged in
if (!is_user_logged_in()) {
// Redirect user to login/register page
wp_redirect( get_permalink( get_option( 'woocommerce_myaccount_page_id' ) ) );
exit;
}
}
}
You can add this code to your theme's `functions.php` file or a custom plugin. This code will check if the product being purchased is the specific product with ID 1024 and if the user is not logged in, it will redirect them to the login/register page.
Rate this post
3 of 5 based on 5827 votesComments