-
-
Save bryceleue/4c9fa74f7aa547aac8234c31abff48c3 to your computer and use it in GitHub Desktop.
WooCommerce Disable guest checkout for certain products
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment