phpwordpresswoocommercecheckoutuser-registration

Allow guest checkout for specific products only in WooCommerce


The following code add a custom field to admin product settings to manage guest checkout at product level:

// 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;
}

But it doesn't work actually. What I am doing wrong? How can I fix it?

I am trying to allow guest purchases for specific products. The admin custom field display and save custom field value is working (the 2 first functions), But login/register never comes up on checkout page, even if there are products in cart that doesn't allow guest checkout.


Solution

  • The filter hook enable_guest_checkout_based_on_product doesn't exist anymore and has been replaced by another hook a bit different.

    So your code is going to be:

    add_filter( 'woocommerce_checkout_registration_required', 'change_tax_class_user_role', 900 );
    function change_tax_class_user_role( $registration_required ) {
        if ( ! WC()->cart->is_empty() ) {
            $registration_required = false; // Initializing (allowing guest checkout by default)
            
            // 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 $registration_required;
    }
    

    Code goes in functions.php file of your active child theme (or active theme). It should works.

    Related continuation: Redirection for non checkout guest allowed in WooCommerce