skip to Main Content

I am using WooCommerce membership for one product and the rest of the WooCommerce products are generic products.

I use this code for this topic

I use this code to customize the email on the WooCommerce checkout page. How can I specify that if a product needs to be registered, email will be required?

    add_filter( 'woocommerce_billing_fields', 'ts_unrequire_wc_email_field');
function ts_unrequire_wc_email_field( $fields ) {
$fields['billing_email']['required'] = false;
return $fields;
}

2

Answers


  1. Chosen as BEST ANSWER

    I want the email to be required when one of the products needs to be registered using this code:

    // Code goes in theme functions.php or a custom plugin
    add_filter( 'pre_option_woocommerce_enable_guest_checkout', 'conditional_guest_checkout_based_on_product' );
    function conditional_guest_checkout_based_on_product( $value ) {
      $restrict_ids = array( 1, 2, 3 ); // Replace with product ids which cannot use guest checkout
    
      if ( WC()->cart ) {
        $cart = WC()->cart->get_cart();
        foreach ( $cart as $item ) {
          if ( in_array( $item['product_id'], $restrict_ids ) ) {
            $value = "no";
            break;
          }
        }
      }
    
      return $value;
    }
    

    And in other cases where the product can be purchased as a guest, email is not required.


  2. Try This:

    add_filter('woocommerce_checkout_fields', 'woo_email_validation', 20);
    
    function woo_email_validation($fields){
    
        $fields['billing']['billing_email']['required'] = true; // true => required | false => not required
    
        return $fields;
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search