Sometimes you want to “Force login before checkout pages only for specific Products” and sometime you want following functions also in your wordpress site or Ecommerce site.
1)you want to Registeration page before checkout only for specific products.
2) you want to restrict login before checkout only for specific products.
3)you want login page only for digital Products not for all products.
4)you want login or registeration Page only for membership products.
5)you want to register your user only for some wocommerce products.
6)You want to completely restrict your wocommerce product to registered user only.
7)You want to enable Login before checout button on some products.
8)you want to “Force login before checkout page” for all products.
If all or One of these are your requirements for your user registeration. You can
Follow following steps.
1)Go to your WordPress Admin Dashboard.
2)Go to Appearance section.
3)click on Theme editor section in appearance section.
4)Now select functions.php.
5)Now paste following code in last of functions.php code.
6)change product ids given in code with your those products ids which you want to restrict for registered users only.
// Code goes in theme functions.php or a custom plugin
add_filter( 'pre_option_woocommerce_enable_guest_checkout', 'conditional_guest_checkout_based_on_product' );
function conditional_guest_checkout_based_on_product( $value ) {
$restrict_ids = array( 1, 2, 3 ); // Replace with product ids which cannot use guest checkout
if ( WC()->cart ) {
$cart = WC()->cart->get_cart();
foreach ( $cart as $item ) {
if ( in_array( $item['product_id'], $restrict_ids ) ) {
$value = "no";
break;
}
}
}
return $value;
}
Guest Checkout Settings In woocommerce.
please note that you have to enable guest checkout in wocommerce settings.
So for Doing This Follow these Steps.
1)Go to Admin Dashboard.
2) Then Go to wocommerce Settings.
3)Then Go to “Account And Privacy” Section in wocommerce settings section.
4)Now tick the “Allow customers to place orders without an account” in Guest Checkout Area.
5)then Tick the “Allow customers to create an account during checkout” and “Allow customers to create an account on the “My account” page”.
6) Now click On “Save Changes” button given below.
Now login restriction before checkout is off for other products but on for those products which are selected in code by putting Product ids.
If you are facing any wordpress issues you can ask for solution in comments or you can contact us through Contact Us section Given in Menu.
This was the complete blog post in which we tell you how to Force login before checkout pages only for specific products.
Click here for submitting your issue or problem now.
You can like our Facebook page Also.