Home / Disable / Remove unwanted checkout fields when all products are downloadable
Duplicate Snippet

Embed Snippet on Your Site

Remove unwanted checkout fields when all products are downloadable

With this snippet you can hide/remove default Woocommerce checkout fields when all products are downloadable

Code Preview
php
<?php
<?php
// Prevent direct access to this file
defined("ABSPATH") || exit();
add_filter( 'woocommerce_checkout_fields' , 'woo_remove_billing_checkout_fields' );
/**
 * Remove unwanted checkout fields
 *
 * @return $fields array
*/
function woo_remove_billing_checkout_fields( $fields ) {
  
    if( woo_cart_virtual_downloadable_product_only() == true ) {
	    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']);
	    unset($fields['billing']['billing_address_2']);
	    unset($fields['billing']['billing_postcode']);
	    unset($fields['billing']['billing_company']);
	    unset($fields['billing']['billing_city']);
    }
    
    return $fields;
}
/**
 * Check if the cart contains virtual/downloadable product only
 *
 * @return bool
*/
function woo_cart_virtual_downloadable_product_only() {
  
  global $woocommerce;
  
  // By default, virtual/downloadable product only
  $virtual_downloadable_products_only = true;
  
  
  // Get all products in cart
  $products = $woocommerce->cart->get_cart();
  
  // Loop through cart products
  foreach( $products as $product ) {
	  // Get product ID
	  $product_id = $product['product_id'];
	  // is variation downloadable
	  $is_downloadable = $product['data']->downloadable;
	  // is variation virtual
	  $is_virtual = $product['data']->virtual ;
	  
	  // Update $virtual_downloadable_products_only if product is not virtual or downloadable and exit loop
	  if( $is_virtual == 'no' && $is_downloadable == 'no' ){
		 $virtual_downloadable_products_only = false;
		 break;
	  }
  }
  
  return $virtual_downloadable_products_only;
}

Comments

Add a Comment