Skip to content

Instantly share code, notes, and snippets.

@corsonr
Last active December 16, 2020 01:48
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save corsonr/7215762 to your computer and use it in GitHub Desktop.
Save corsonr/7215762 to your computer and use it in GitHub Desktop.
WooCommerce: Hide Checkout Fields For Virtual Products
<?php
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_has_virtual_product() == 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['order']['order_comments']);
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 product
*
* @return bool
*/
function woo_cart_has_virtual_product() {
global $woocommerce;
// By default, no virtual product
$has_virtual_products = false;
// Default virtual products number
$virtual_products = 0;
// Get all products in cart
$products = $woocommerce->cart->get_cart();
// Loop through cart products
foreach( $products as $product ) {
// Get product ID and '_virtual' post meta
$product_id = $product['product_id'];
$is_virtual = get_post_meta( $product_id, '_virtual', true );
// Update $has_virtual_product if product is virtual
if( $is_virtual == 'yes' )
$virtual_products += 1;
}
if( count($products) == $virtual_products )
$has_virtual_products = true;
return $has_virtual_products;
}
@Katamo
Copy link

Katamo commented Apr 9, 2014

Hi Remi.
There's an uptade for the function to check if a product is a digital variation of a standard product.
https://gist.github.com/Katamo/10243188

@worldsdream
Copy link

I can confirm this is working! Just at line 54, remove "". So it becomes:

  $is_virtual = get_post_meta( $product_id, '_virtual', true );

@hughc
Copy link

hughc commented Mar 11, 2016

@worldsdrream is right, but it wasn't clear to me what the character he was referring to was.

There's a character in there that doesn't render in the view above, but this gist breaks when copy pasted, it appears as

$is_virtual = get_post_meta( $product_id, ' _virtual', true );

which results in no metadata being retrieved.

@AskWelmoed
Copy link

Hi! I'm not good with code... but I added the above code to the functions.php of a site with WooCommerce, and when I just have a virtual product in my cart, I still see all the address options, phone number etc. I'm probably doing something wrong here ;-) Any ideas?

@offordscott
Copy link

I have LearnDash installed, and it comes with a new "Course" option in the Product Data / Product Type section:

(as seen in this screenshot)

https://d3vv6lp55qjaqc.cloudfront.net/items/2o3R2P0h1V1E2O0e303T/Screen%20Shot%202017-11-21%20at%202.39.16%20PM.png

When I select Course as the product type, the ability for me to check the "Virtual" checkbox disappears.

Since the Course is technically a product that doesn't need to be shipped, how would I make hide the fields in the same way it does with virtual products? I want to hide the shipping fields and such the same way it would if I had selected "Virtual", as seen here:

https://d3vv6lp55qjaqc.cloudfront.net/items/3P2d0Y250w0K3T3b142Y/Screen%20Shot%202017-11-21%20at%202.46.27%20PM.png

@mattpramschufer
Copy link

I'm not sure if it a new function, but why not use the is_virtual() function for the WC_Product_Simple Object ? For instance if you look at https://gist.github.com/mattpramschufer/957039753ea7a0bc3344e2f817304156 I simply have

function woo_cart_has_virtual_product() {
	foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
		if($cart_item['data']->is_virtual()){
			return true;
		}
	}
	return false;
}

@AcidHardcore
Copy link

mattpramschufer your code works, but I made some improvements for case if we have both product types in a cart
function woo_cart_has_virtual_product() {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
return $cart_item['data']->is_virtual() ? true : false;
}
return false;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment