if(!is_user_logged_in()):
add_filter( 'woocommerce_checkout_fields', 'wmbz_simplify_checkout_virtual' );
endif;
  
function wmbz_simplify_checkout_virtual( $fields ) {
   $only_virtual = true;
   $lidmaatschap = false;
   
   foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
	  // Check if there are non-virtual products
	  // If Cart has tag "lidmaatschap or proefperiode", set $lidmaatschap to true
	   if ( has_term( 'lidmaatschap', 'product_tag', $cart_item['product_id'] ) || has_term( 'proefperiode', 'product_tag', $cart_item['product_id'] ) ) {
		  $lidmaatschap = true;
		  //echo 'product met lidmaatschap of proefperiode aanwezig.';
	   }
	  if ( ! $cart_item['data']->is_virtual() ) $only_virtual = false;
   }
   if ( $only_virtual && !$lidmaatschap ) {
	   // Unset billing fields only if they have been set already
		  if (isset($fields['billing']['billing_company'])) {
			  unset($fields['billing']['billing_company']);
		  }
		  if (isset($fields['billing']['billing_address_1'])) {
			  unset($fields['billing']['billing_address_1']);
		  }
		  if (isset($fields['billing']['billing_address_2'])) {
			  unset($fields['billing']['billing_address_2']);
		  }
		  if (isset($fields['billing']['billing_city'])) {
			  unset($fields['billing']['billing_city']);
		  }
		  if (isset($fields['billing']['billing_postcode'])) {
			  unset($fields['billing']['billing_postcode']);
		  }
		  //if (isset($fields['billing']['billing_country'])) {
			  //unset($fields['billing']['billing_country']);
		  //}
		  if (isset($fields['billing']['billing_state'])) {
			  unset($fields['billing']['billing_state']);
		  }
		  if (isset($fields['billing']['billing_phone'])) {
			  unset($fields['billing']['billing_phone']);
		  }
	  add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
   }
   return $fields;
}
Geef een reactie 0