I have a WooCommerce store on my wordpress website, and I am trying to enable guest checkout for specific products in my store, and require account creation for other products.
I have the following code in my functions.php
file to enable guest checkout on products I specify, but when I navigate to checkout with the product that has guest checkout enabled, the WooCommerce checkout page still is requiring one to create an account.
// 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 );
}
// Custom conditional function that checks if checkout registration is required
function is_checkout_registration_required() {
if ( ! WC()->cart->is_empty() ) {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $item ) {
// Check if there is any item in cart that has not the option "Guest checkout allowed"
if ( get_post_meta( $item['product_id'], '_allow_guest_checkout', true ) !== 'yes' ) {
return true; // Found: Force checkout user registration and exit
}
}
}
return false;
}
add_filter( 'woocommerce_checkout_registration_required', 'change_tax_class_user_role', 900 );
function change_tax_class_user_role( $registration_required ) {
return is_checkout_registration_required();
}
add_action( 'template_redirect', 'checkout_redirect_non_logged_to_login_access');
function checkout_redirect_non_logged_to_login_access() {
if( is_checkout() && !is_user_logged_in() && is_checkout_registration_required() ){
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
exit;
}
}
I believe the reason why the account creation fields are still listed on the checkout page is because I have enabled Allow customers to create an account during checkout
under WooCommerce > Settings > Accounts & Privacy page.
So my question is: how can I disable this setting when I enabled the guest checkout by marking the guest checkout box upon product creation (See my code for the guest checkout checkbox).
2
Answers
Actually I was able to solve my problem with this code here
Note: This answer assumes that
Allow customers to place orders without an account
is enabledThe hook
woocommerce_checkout_registration_required
determines if registration is required during checkoutHowever the hook you should use to
to overwrite the
Allow customers to create an account during checkout
setting iswoocommerce_checkout_registration_enabled
.So you get: