微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我的 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'],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',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',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','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'],true ) == 'yes' ) {
                $value = "yes";
            } else {
                $value = "no";
                break;
            }
        }
    }
    return $value;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?