WooCommerce Tips: Conditionally Hiding Order Notes for Virtual Products

Order notes are a convenient feature in WooCommerce that allows customers to add additional comments or instructions for the order during the checkout process. However, in some cases, you may want to hide this field when all products in the cart are virtual products, as they may not require any additional information. You can use this code snippet to disable the order notes when there are only virtual products in the cart.

<?php
/**
 * Hide order notes if all the products in the cart are virtual products.
 *
 * @param bool $show_notes Show notes.
 *
 * @return bool
 */
function hide_order_notes_for_virtual_products( $show_notes ) {

	$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 ) {
		return false;
	}

	return $show_notes;
}

add_filter( 'woocommerce_enable_order_notes_field', 'hide_order_notes_for_virtual_products', 101 );


add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );

You can add this code snippet to the functions.php of your active child theme or use the Code Snippets plugin.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Up Next:

WooCommerce - How to maintain the state of "Ship to a different address" Checkbox

WooCommerce - How to maintain the state of "Ship to a different address" Checkbox