Join

Hide WooCommerce Checkout Fields If No Payment Required

Don't want to mess with code snippets? This is a feature of MyListing Pro.

Instructions

  1. Create a new PHP code snippet.
  2. Copy the contents of code snippet below.
  3. Paste the contents into your code snippet.
  4. Review any notes that I’ve provided.
  5. Save and enable the code snippet.
  6. Test.

Snippet

This snippet removes all but the Name and Email Address fields.

MyListing Pro takes this code snippet to a whole new level, giving you simple checkboxes to determine which fields to show and which to hide.

PHP
<?php 

add_filter( 'woocommerce_checkout_fields', function( $fields ) {
   if ( WC()->cart && ! WC()->cart->needs_payment() ) {
      $free_fields = [];
      foreach ( $fields['billing'] as $key => $value ) {
         if ( in_array( $key, [ 'billing_email', 'billing_first_name' ] ) ) {
            $free_fields['billing'][$key] = $value;
         }
      }
      return $free_fields;
   }
   return $fields;
}, 99999999 );