Search code examples
phpwordpresswoocommercecheckoutuser-registration

Disable create account during WooCommerce checkout when guest checkout is enabled for specified products


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).


Solution

  • Note: This answer assumes that Allow customers to place orders without an account is enabled


    The hook woocommerce_checkout_registration_required determines if registration is required during checkout

    However the hook you should use to to overwrite the Allow customers to create an account during checkout setting is woocommerce_checkout_registration_enabled.

    So you get:

    // Display Guest Checkout Field
    function action_woocommerce_product_options_general_product_data() {
        // Add checkbox
        woocommerce_wp_checkbox( array( 
            'id'             => '_allow_guest_checkout',
            'wrapper_class'  => 'show_if_simple',
            'label'          => __( 'Checkout', 'woocommerce' ),
            'desc_tip'       => false,
            'description'    => __( 'Allow Guest Checkout', 'woocommerce' )
        ) );
    }
    add_action( 'woocommerce_product_options_general_product_data', 'action_woocommerce_product_options_general_product_data', 10, 0 );
            
    // Save Field
    function action_woocommerce_admin_process_product_object( $product ) {
        // Isset, yes or no
        $checkbox = isset( $_POST['_allow_guest_checkout'] ) ? 'yes' : 'no';
    
        // Update meta
        $product->update_meta_data( '_allow_guest_checkout', $checkbox );
    }
    add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );
    
    // Remove registration from WooCommerce checkout
    function filter_woocommerce_checkout_registration_enabled( $registration_enabled ) {
        // WC Cart
        if ( WC()->cart ) {
            // NOT empty
            if ( ! WC()->cart->is_empty() ) {
                // Loop through cart items
                foreach ( WC()->cart->get_cart() as $cart_item ) {
                    // Get meta
                    $allow_guest_checkout = $cart_item['data']->get_meta( '_allow_guest_checkout', true );
                    
                    // Compare
                    if ( $allow_guest_checkout == 'yes' ) {
                        $registration_enabled = false;
                        break;
                    }
                }
            }
        }
        
        return $registration_enabled;
    }
    add_filter( 'woocommerce_checkout_registration_enabled', 'filter_woocommerce_checkout_registration_enabled', 10, 1 );