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

问题描述 投票:0回答:3

我的 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

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

php wordpress woocommerce checkout user-registration
3个回答
0
投票

注意:此答案假设

Allow customers to place orders without an account
已启用


钩子

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() {
    // 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 );

0
投票

实际上我可以用这里的代码解决我的问题

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

0
投票

有一种简单的方法可以在结账时禁用“创建帐户”,无需任何编码。

在 Woocomerse-> 设置选项卡下,有一个“帐户创建”部分,其中包含“允许客户在结帐时创建帐户”复选框。如果未选中,用户将无法在结账时创建帐户。

希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.