How can I specify that if a product needs to be registered, email will be required؟

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

Solutions

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.

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;

}

Similar questions

Can I create a loop with multiple post types and specify different $args for each post type?
I'd like to create a custom loop that includes all Post types: Post and Video. Then I'd like to specify to show all posts of the post type Video, but only show posts that are in the category "Videos." Here's what I'm thinking: I know it sounds strange but this is a work around because the custom post types are new and it's going to take time before...
Can I specify that custom.css gets loaded at Appearance > Editor instead of style.css?
I want to specify which css file gets loaded when the user clicks on "Appearance > Editor". However, its always loading style.css for some reason. Any way to enforce that a specific file gets loaded there?
How can I specify the width and height of the Featured Image in TwentyTwelve theme?
I am trying to get set the width, height and position of the Featured image for a new page I created.
Can I explicitly specify ENGINE=InnoDB in Wordpress?
I converted all my tables to InnoDB, but the MySQL server (which I can't access, as I'm on shared hosting) has MyISAM as the default, so every new table that Wordpress creates (e.g. when I add a new site on my multisite) uses the MyISAM engine and I have to alter it manually. Can I somehow set Wordpress up to enforce the InnoDB engine when creating...
Can I specify post__in for one particular post type in the query?
I want to do something like this where I can specify which post type the post__in applies to. For example, I want to get all the posts from cpt_1, but I only want the cpt_2 ones that are specified in post__in. Is there an SQL hook (like posts_where) that can help me accomplish this? If not I guess I'll have to do two queries?
How can I specify the position of an admin bar item added with $wp_admin_bar->add_menu() or add_node()?
I want to add a link to my site in the admin bar, and make that link the leftmost item in the admin bar. I can add a link with this in a plugin function: But I'd like to make it the leftmost link in the admin bar, i.e. all the way in the top left corner. Is there a way to do this?

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.