Skip to content

Instantly share code, notes, and snippets.

@mikejolley
Created March 10, 2016 09:39
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save mikejolley/5fadb94dd9eff11ab512 to your computer and use it in GitHub Desktop.
Save mikejolley/5fadb94dd9eff11ab512 to your computer and use it in GitHub Desktop.
WooCommerce Disable guest checkout for certain products
<?php
// 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;
}
@bradthebluefish
Copy link

bradthebluefish commented Jun 8, 2022

6 years old and still works for me. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment