Home » WooCommerce: Hide Checkout Fields if Virtual Product @ Cart

WooCommerce: Hide Checkout Fields if Virtual Product @ Cart

by Tutor Aspire

If you sell downloadable/virtual products and need to simplify your WooCommerce checkout when such product type is in the Cart, you’ve come to the right place!

Here’s a simple snippet to check if there are only “virtual” products in the Cart and if yes, all the billing fields and order notes are hidden (but name and email address). Go test this on your development environment and let me know if this works!

Simplify the WooCommerce checkout by removing billing fields if cart contains virtual products

PHP Snippet: Remove Billing Fields if Cart contains Virtual Products @ WooCommerce Checkout

/**
 * @snippet       Hide Fields if Virtual @ WooCommerce Checkout
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 7
 * @donate $9     https://www.tutoraspire.com
 */

add_filter( 'woocommerce_checkout_fields', 'tutoraspire_simplify_checkout_virtual' );
 
function tutoraspire_simplify_checkout_virtual( $fields ) {
   $only_virtual = true;
   foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
      // Check if there are non-virtual products
      if ( ! $cart_item['data']->is_virtual() ) $only_virtual = false;
   }
   if ( $only_virtual ) {
      unset($fields['billing']['billing_company']);
      unset($fields['billing']['billing_address_1']);
      unset($fields['billing']['billing_address_2']);
      unset($fields['billing']['billing_city']);
      unset($fields['billing']['billing_postcode']);
      unset($fields['billing']['billing_country']);
      unset($fields['billing']['billing_state']);
      unset($fields['billing']['billing_phone']);
      add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
   }
   return $fields;
}

You may also like