How to Display Checkout Fields In WooCommerce

Posted on by AJ Morris | Updated:
Reading Time: 4 minutes

WooCommerce out of the box is great. It helps you quickly build a checkout process, gives you basic reporting, provides you with a way to add products to your store, and even helps setup shipping for you. But what happens if you want a little more control over your checkout process? What if you want to push the boundaries a bit and hide some checkout fields from the checkout process on specific products or product categories where those fields don’t make sense?

That’s what we’re going to cover in this tutorial.

The only way I currently know how to remove checkout fields is by doing it with code. Let’s start out simple by setting up a simple function to remove a checkout field. This is very basic. Simply add the following to your theme’s functions.php file (or create a simple plugin that you could add this to.)

<?php

function wc_ninja_remove_checkout_field( $fields ) {
	unset( $fields['billing']['billing_company'] );

	return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'wc_ninja_remove_checkout_field' );
// list of fields can be found here, https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/

What’s great about this code snippet is that you can add any fields you want removed from your checkout process.

Note: You do want to be careful with this though. Removing fields from your checkout process can cause you to not get enough information from the purchaser.

While this removes a checkout field, it doesn’t do it for specific products or product categories. Let’s move into that next.

Product Specific Conditional

Ok, when you want to conditionally remove a field based on if a product is in the cart or not, we need to do a little more work. We’ll be building off of the initial function we just went over.

We first need to create a new function that checks to see if a specific product ID is in the cart. We’ll have an array in there, so we’ll be looking for multiple products, which is helpful if you have multiple products that you don’t want to have a field/s show up in the checkout process.

After we have the array of product IDs, we then create an array of products that are in the cart. We’ll loop through the IDs and see if any of our product IDs are in there. If we find one (or multiple), we return true. This way we can use that true in our removal checkout field function.

Here’s the function for our products and the products that are in the cart.

<?php
/**
 * Check if a specific product ID is in the cart
 */
function wc_ninja_product_is_in_the_cart() {
	// Add your special product IDs here
	$ids = array( '45', '70', '75' );;

	// Products currently in the cart
	$cart_ids = array();

	// Find each product in the cart and add it to the $cart_ids array
	foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
		$cart_product = $values['data'];
		$cart_ids[]   = $cart_product->id;
	}

	// If one of the special products are in the cart, return true.
	if ( ! empty( array_intersect( $ids, $cart_ids ) ) ) {
		return true;
	} else {
		return false;
	}
}

In the first array, you can change those product IDs to match the IDs of the products you’re looking for.

With this additional function, we now need to go back to our other function and make a slight change. We need to check to see if our function returns true or false. Here’s that logic added to our first function.

<?php
/**
 * Conditionally remove a checkout field based on products in the cart
 */
function wc_ninja_remove_checkout_field( $fields ) {
	if ( ! wc_ninja_product_is_in_the_cart() ) {
		unset( $fields['billing']['billing_company'] );
	}

	return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'wc_ninja_remove_checkout_field' );

That’s it. We can now search for specific products and remove checkout fields if those products are in the cart. In the next section, we’ll look at adapting our code to look for specific categories.

Product Category Conditional

Using similar code that we did previously, we can modify one function to check for categories instead of product IDs.

The biggest difference here is that we initially need to have an array of categories instead of IDs. We then need to figure out the cart products and what categories they belong too. This is a bit more complicated so there are a few foreach blocks to get to the categories.

We then compare the product categories we have in our array with the product categories that are in the cart. If our categories exist, we’ll return true, otherwise we’ll return false. Here’s that code with some comments for assistance.

<?php
/**
 * Check if a specific product category is in the cart
 */
function wc_ninja_category_is_in_the_cart() {
	// Add your special category slugs here
	$categories = array( 'clothing', 'posters' );

	// Products currently in the cart
	$cart_ids = array();

	// Categories currently in the cart
	$cart_categories = array();

	// Find each product in the cart and add it to the $cart_ids array
	foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
		$cart_product = $values['data'];
		$cart_ids[]   = $cart_product->id;
	}

	// Connect the products in the cart w/ their categories
	foreach( $cart_ids as $id ) {
		$products_categories = get_the_terms( $id, 'product_cat' );

		// Loop through each product category and add it to our $cart_categories array
		foreach ( $products_categories as $products_category ) {
			$cart_categories[] = $products_category->slug;
		}
	}

	// If one of the special categories are in the cart, return true.
	if ( ! empty( array_intersect( $categories, $cart_categories ) ) ) {
		return true;
	} else {
		return false;
	}
}

And all we need to do is look at our second function and replace wc_ninja_product_is_in_the_cart() with our new function wc_ninja_category_is_in_the_cart().

Give us a call at 800.580.4985, or open a chat or ticket with us to speak with one of our knowledgeable Solutions Team or an experienced Hosting Advisors today!

Avatar for AJ Morris

About the Author: AJ Morris

AJ Morris is the Product Innovation and Marketing Manager at SolidWP. He’s been involved in the WordPress community for over a decade focusing on building, designing and launching WordPress websites and businesses.

Latest Articles

Blocking IP or whitelisting IP addresses with UFW

Read Article

CentOS Linux 7 end of life migrations

Read Article

Use ChatGPT to diagnose and resolve server issues

Read Article

What is SDDC VMware?

Read Article

Best authentication practices for email senders

Read Article