A simple, powerful and independent e-commerce platform.
Sell anything with ease.

Simplify checkout if only virtual products

If the cart only contains virtual products there is no point making a customer fill out the address fields. This code checks if there are only virtual products in the cart and, if so, hides the unnecessary fields.

		/**
 * Simplify checkout if only virtual products
 */

add_filter('woocommerce_checkout_fields', 'cc_simplify_virtual_checkout');

function cc_simplify_virtual_checkout($fields) {

  $only_virtual = true;

  foreach(WC()->cart->get_cart() as $cart_item_key => $cart_item) {
    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;

}
	
View Raw Code ID: 91070