【问题标题】:Disable create account during WooCommerce checkout when guest checkout is enabled for specified products为指定产品启用访客结帐时,在 WooCommerce 结帐期间禁用创建帐户
【发布时间】:2021-01-19 18:03:44
【问题描述】:

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


    【解决方案1】:

    钩子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 已启用

    【讨论】:

      【解决方案2】:

      其实我可以在这里用这段代码解决我的问题

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

      【讨论】:

        猜你喜欢
        • 2015-02-03
        • 2015-05-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-13
        • 1970-01-01
        • 2015-11-15
        • 1970-01-01
        • 2015-01-17
        相关资源
        最近更新 更多