To sell visual products, you do not need to receive the customer’s postal address. In this post, you will learn how to remove unnecessary fields in WooCommerce for visual products.

WooCommerce checkout fields for physical products
WooCommerce checkout fields for physical products
WooCommerce checkout fields for virtual products
WooCommerce checkout fields for virtual products

Remove Checkout Fields for Virtual products without plugins

In the WordPress dashboard, go to Appearance ➡ Theme File Editor and copy the following code into the theme’s functions.php file and update it.

You must create a child theme before making any changes to functions.php file. Otherwise, the applied changes will be lost after each update.
Create child theme in WordPress step by step [without plugin]

As an alternative method, you can use the Code Snippets plugin to insert your codes into WordPress.

/**
 * Remove Checkout Fields for Virtual products in WooCommerce
 * @WPCookie
 */
 
add_filter( 'woocommerce_checkout_fields', 'simplify_checkout_virtual' );
  
function 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;
}

By inserting the above code on the site when purchasing visual products, all fields related to the address will be hidden in the checkout page.
Delete any fields you need from the code above to add them back to the checkout page. For example, to add the phone field, delete the following line in the above code.

      unset($fields['billing']['billing_phone']);

If this article is difficult for you to read in text, you can watch the video version below.

Share this post
Maya
Maya

Hi, my name is Maya and I’m a WordPress plugin developer. I created this website to share some of the helpful codes that I’ve used in my own projects.
If you’re looking for a custom plugin for your website, you can contact me by clicking on Hire a developer in the menu. I’d love to hear from you.

Articles: 54

Leave a Reply

Your email address will not be published. Required fields are marked *