当为指定产品启用访客结账时,在 WooCommerce 结账期间禁用创建帐户

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

我的 wordpress 网站上有一个 WooCommerce 商店,我正在尝试为我商店中的特定产品启用访客结账功能,并要求为其他产品创建帐户。

我的 functions.php 文件中有以下代码,用于对我指定的产品启用访客结账,但是当我导航到启用访客结账的产品结账时,WooCommerce 结账页面仍然需要一个创建一个帐户。

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

我认为帐户创建字段仍列在结帐页面上的原因是因为我在 WooCommerce > 设置 > 帐户和隐私页面下启用了 Allow customers to create an account during checkout

所以我的问题是:当我通过在创建产品时标记访客结账框来启用访客结账时如何禁用此设置(请参阅我的访客结账复选框代码).

钩子woocommerce_checkout_registration_required决定结账时是否需要注册

但是你应该使用的钩子 覆盖 Allow customers to create an account during checkout 设置是 woocommerce_checkout_registration_enabled.

所以你得到:

// Display Guest Checkout Field
function action_woocommerce_product_options_general_product_data() {
    // Checkbox
    woocommerce_wp_checkbox( array( 
        'id'             => '_allow_guest_checkout', // Required, it's the meta_key for storing the value (is checked or not)
        'wrapper_class'  => 'show_if_simple', // For simple products
        'label'          => __( 'Checkout', 'woocommerce' ), // Text in the editor label
        'desc_tip'       => false, // true or false, show description directly or as tooltip
        'description'    => __( 'Allow Guest Checkout', 'woocommerce' ) // Provide something useful here
    ) );
}
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 );

注:此答案假设Allow customers to place orders without 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 );
}

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