How to Conditionally Hide Checkout Fields Based on role or cart items

Why Hide Fields in Checkout form?

Hiding unnecessary fields improves user experience, reduces confusion, and speeds up checkout.

  • Provides a smarter UX
  • Helps create segmented checkout flows
  • Makes your store feel more customized and personal

To set default values in Woocommerce checkout form Use Below Code

Add code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Code snippets plugin. Avoid adding custom code directly to your parent theme’s functions.php file as this will be wiped entirely when you update the theme.

Hide Fields Based on User Role

wholesale_customer is custom role for this blog you can create your own.

Here’s how to hide the billing company field for customers, but show it for another roles for example (wholesale_customer)

add_filter('woocommerce_checkout_fields', 'hide_checkout_fields_by_user_role');

function hide_checkout_fields_by_user_role($fields) {
    $user = wp_get_current_user();

    if (in_array('customer', $user->roles)) {
        unset($fields['billing']['billing_company']);
    }
    return $fields;
}

This code will:

  • If the current user is a customer, it removes the billing company field.
  • For wholesale_customer, the field remains.

Hide Fields Based on Cart Contents

Example: Hide Shipping Fields if Only Virtual Products Are in Cart

add_filter('woocommerce_checkout_fields', 'hide_shipping_for_virtual_products');

function hide_shipping_for_virtual_products($fields) {
    $virtual_only = true;

    foreach (WC()->cart->get_cart_contents() as $item) {
        if (!$item['data']->is_virtual()) {
            $virtual_only = false;
            break;
        }
    }

    if ($virtual_only) {
        unset($fields['shipping']); // Hides all shipping fields
    }

    return $fields;
}

Hide a Field If Product from Specific Category Is in Cart

Let’s hide the billing phone field if a product from category gift-card is in the cart.

add_filter('woocommerce_checkout_fields', 'hide_fields_based_on_cart_category');

function hide_fields_based_on_cart_category($fields) {
    $hide = false;

    foreach (WC()->cart->get_cart_contents() as $item) {
        $product_id = $item['product_id'];
        if (has_term('gift-card', 'product_cat', $product_id)) {
            $hide = true;
            break;
        }
    }

    if ($hide) {
        unset($fields['billing']['billing_phone']);
    }

    return $fields;
}

Advanced Example: Combine Role + Cart Logic

Hide a custom field (e.g., billing_reference_number) only if the user is a subscriber AND cart has product from “services” category:

add_filter('woocommerce_checkout_fields', 'hide_field_for_role_and_category');

function hide_field_for_role_and_category($fields) {
    $user = wp_get_current_user();
    $hide = false;

    if (in_array('subscriber', $user->roles)) {
        foreach (WC()->cart->get_cart_contents() as $item) {
            if (has_term('services', 'product_cat', $item['product_id'])) {
                $hide = true;
                break;
            }
        }
    }
    if ($hide) {
        unset($fields['billing']['billing_reference_number']);
    }
    return $fields;
}

Add and Hide a Custom Field

Let’s say you add a custom field but only show it to wholesale_customer:

add_filter('woocommerce_checkout_fields', 'conditionally_add_custom_field');

function conditionally_add_custom_field($fields) {
    $user = wp_get_current_user();

    if (in_array('wholesale_customer', $user->roles)) {
        $fields['billing']['billing_wholesale_id'] = array(
            'type'        => 'text',
            'label'       => __('Wholesale ID'),
            'required'    => true,
            'class'       => array('form-row-wide'),
            'priority'    => 120
        );
    }
    return $fields;
}

🧠 Pro Tips for Readers

  • Always wrap your logic in checks like is_user_logged_in() or WC()->cart->get_cart_contents() to avoid fatal errors.
  • Use has_term() to check product category/tags.
  • Use role checks with in_array() and $user->roles.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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