Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce - Allow guest checkout for certain products when Guest checkout is Disabled globally
/*--------------------------------------
Woocommerce - Allow Guest Checkout on Certain products
----------------------------------------*/
// Display Guest Checkout Field
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Checkbox
woocommerce_wp_checkbox(
array(
'id' => '_allow_guest_checkout',
'wrapper_class' => 'show_if_simple',
'label' => __('Checkout', 'woocommerce' ),
'description' => __('Allow Guest Checkout', 'woocommerce' )
)
);
echo '</div>';
}
// Save Guest Checkout Field
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_checkbox = isset( $_POST['_allow_guest_checkout'] ) ? 'yes' : 'no';
update_post_meta( $post_id, '_allow_guest_checkout', $woocommerce_checkbox );
}
// Enable Guest Checkout on Certain products
add_filter( 'pre_option_woocommerce_enable_guest_checkout', 'enable_guest_checkout_based_on_product' );
function enable_guest_checkout_based_on_product( $value ) {
if ( WC()->cart ) {
$cart = WC()->cart->get_cart();
foreach ( $cart as $item ) {
if ( get_post_meta( $item['product_id'], '_allow_guest_checkout', true ) == 'yes' ) {
$value = "yes";
} else {
$value = "no";
break;
}
}
}
return $value;
}
@nsarrasin

This comment has been minimized.

Copy link

@nsarrasin nsarrasin commented Apr 29, 2017

Hi,
I am not a coder but I would like to choose which of my Woocommerce products will be buyable through guest ckeckout or not. I use Learndash to sell training, so I don't have choice to not use guest checkout, but I want to reduce buying friction for my ebooks and so want to keep the guest checkout activated for them. Here are few questions please:

  1. If I add your code in the functions.php file of Woocommerce, does it could make me choose in the admin which product will offer guest checkout and which will not?

  2. If yes to the question 1, when I'll update the Woocommerce plugin, do I'll have to re-add this code every time in the functions file or is there a way to keep it in the plugin even after updates?

  3. If a customer wants to buy 2 products, one with the guest checkout activated and the other product not, does the "member checkout" will be applied by default and the person will have to create an account to buy the 2 products?

Thanks a lot!

@JonnyMoore

This comment has been minimized.

Copy link

@JonnyMoore JonnyMoore commented Jul 11, 2018

Any update on the above comment from nsarrasin?

Thanks!
Jonny

@sctebnt

This comment has been minimized.

Copy link

@sctebnt sctebnt commented Jul 27, 2018

I added the above code and the functionality is as expected. For simple products, you have a check box to indicate if the product is available for guests. When checked, the product can be purchased by customers who either do not bother to login or do not have an account. Curing checkout, if the cart contains only products that allow for guest checkout, then the guest can complete their order. If the cart contains at least one item that does "not" allow for guest checkout, then the customer is required to login or create an account.

Thanks for the code sample!

@atanasantonov

This comment has been minimized.

Copy link

@atanasantonov atanasantonov commented Apr 14, 2020

Thank you!

@TechyTack

This comment has been minimized.

Copy link

@TechyTack TechyTack commented Feb 18, 2021

Hello,

love this code, works great. Can it be modified to support WooCommerce variable products? I'm not clear about how/if the line 'wrapper_class' => 'show_if_simple' can be modified.

Thanks

@scotishsunrise

This comment has been minimized.

Copy link

@scotishsunrise scotishsunrise commented Jun 21, 2021

Is there any chance this could be updated to add the "allow guest checkout" checkbox to show up on variable products, too?

As luck would have it, all of my simple products are the ones that should not allow guest checkout and all of my variable products (mostly merch) are the ones that should allow it.

TIA!

@KevTone

This comment has been minimized.

Copy link

@KevTone KevTone commented Jun 24, 2021

Amazing! Works as expected. I can now sell courses to users with an account (or creating one) and other products to guests. Thanks so much for this.

@tylermrolfe

This comment has been minimized.

Copy link

@tylermrolfe tylermrolfe commented Jul 16, 2021

If you want the guest checkout button to appear on all types of products rather than just simple, just remove this line:

'wrapper_class' => 'show_if_simple',

@SergeyCodeJs

This comment has been minimized.

Copy link

@SergeyCodeJs SergeyCodeJs commented Jul 17, 2021

Hi. Everything works as expected, but on my checkout page for products with guest registration allowed still appear fields for registration: username, email, password, and also checkbox with question: "register?"

How can i remove this fields for certain products, with guest registration allowed?

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