As of July 1st 2022, any order that’s subject to Colorado Sales or Use Tax and will be delivered by a motor vehicle will also be subject to a 27¢ Retail Delivery Fee to help offset the emissions, traffic, and wear and tear on Colorado’s roads from all of those Amazon, FedEx, UPS, and DoorDash drivers. WooCommerce’s hooks and actions system makes adding the Colorado Retail Delivery Fee at checkout (fairly) painless—here’s how:

Who Needs to Add the Colorado Retail Delivery Fee?

If you collect Sales or Use Tax in Colorado, and your taxable items are delivered via a motor vehicle, then you need to start collecting the Retail Delivery Fee on July 1st 2022. You should read the Colorado Department of Revenue’s official notice for all the details, but the key things to be aware of are:

  • The Retail Delivery Fee must be listed as a separate item called “retail delivery fees” on any receipt or invoice issued to the purchaser
  • The Retail Delivery Fee is not refundable, so if a customer returns some or all of an order subject to the Fee they shouldn’t get their 27¢ back

How to Add the Colorado Retail Delivery Fee to Orders in WooCommerce

WooCommerce has an action called woocommerce_cart_calculate_fees that can be used to add fees to an order. We’ll be using this action to add the Retail Delivery Fee with a PHP function. This function can go in a few places, depending on your situation and level of comfort with editing PHP files:

  • It can go in your theme’s functions.php file if you’re using a Child Theme, or a custom theme you’ve developed yourself—otherwise it will get wiped out the next time you update your theme
  • You can add it as a custom plugin
  • You can add it using a WordPress plugin that lets you add PHP code snippets

Here’s the basic function:

function dbdc_maybe_add_crdf() {
	if (is_admin() && !defined('DOING_AJAX')) {
		return;
	}
	
	$country = 'US';
	$state = 'CO';
	$fee = 0.27;

	if ( WC()->customer->get_shipping_country() === $country && 
	     WC()->customer->get_shipping_state() === $state
	   ) {
		WC()->cart->add_fee(__('Colorado Retail Delivery Fee'), $fee, false, 'standard' );
	}
}
add_action('woocommerce_cart_calculate_fees', 'dbdc_maybe_add_crdf');

Breaking it down, we first check to make sure we’re are not in the Admin area, and that we are updating the cart totals with DOING_AJAX.

Next we define the Billing Country (US) and Billing State (CO) that this fee should apply to, as well as the value of the fee (0.27—this example function assumes your shop currency is set to US Dollars).

Then we make sure that the Billing Country and State of the current customer match the ones we’re looking for. If they do, we add the fee.

Finally we add the function to the action woocommerce_cart_calculate_fees so that it runs whenever fees are calculated, and we’re done!

Advanced Options: Local Pickup and Digital Products

If your shop only offers delivery of physical products you don’t need to go any further. But what if you offer Local Pickup or virtual products? The Retail Delivery Fee shouldn’t be applied to those orders. To handle this we need to check what the selected Shipping Method is, like so:

$chosen_shipping_method = null;
if (!is_null( WC()->session->get('chosen_shipping_methods') )) {
	$chosen_shipping_method = WC()->session->get('chosen_shipping_methods')[0];
}

First we define our $chosen_shipping_method variable. Then, we check to see if there in fact is a shipping method chosen; if there is, we assign its name to the $chosen_shipping_method variable.

Then we add that variable as a condition for applying the fee, checking to make sure the chosen shipping method isn’t Local Pickup (or if you have a custom method, you can check for its label here too):

if ( WC()->customer->get_shipping_country() === $country && 
     WC()->customer->get_shipping_state() === $state &&
     !is_null($chosen_shipping_method) && strpos($chosen_shipping_method,'local_pickup') === false
   )

We can also look for products of a certain type (virtual or downloadable) or category (if all your non-shipped products are in their own category) and make sure the order only contains that type of product:

$is_physical_product = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
	$is_digital_category = has_term( 'digital', 'product_cat', $cart_item['product_id'] );
	$this_item = wc_get_product($cart_item['product_id']);
	if ( $is_digital_category === false && !$this_item->is_virtual() && !$this_item->is_downloadable() ) {
		$is_physical_product = true;
		break;
	}
}

If there are no physical products to deliver, then we don’t need to add the Retail Delivery Fee.

Putting it All Together

Here’s the complete function, with all of the extra conditions:

function dbdc_maybe_add_crdf() {
	// Make sure we're not in Admin and we are processing an ajax cart update request
	if (is_admin() && !defined('DOING_AJAX')) {
		return;
	}
	
	// Get the currently selected shipping method so we can make sure it isn't
	// Local Pickup
	$chosen_shipping_method = null;
	if (!is_null( WC()->session->get('chosen_shipping_methods') )) {
		$chosen_shipping_method = WC()->session->get('chosen_shipping_methods')[0];
	}

	// Check to see if there are any physical products in the cart
	$is_physical_product = false;
	foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
		$is_digital_category = has_term( 'digital', 'product_cat', $cart_item['product_id'] );
		$this_item = wc_get_product($cart_item['product_id']);
		// If the item isn't Virtual or in a category that only contains non-shippable
		// items, then we have at least one physical item and the order is subject
		// to the fee
		if ( $is_digital_category === false && !$this_item->is_virtual() && !$this_item->is_downloadable() ) {
			$is_physical_product = true;
			break;
		}
	}

	// Define the locations we're looking for and the value of the fee
	$country = 'US';
	$state = 'CO';
	$fee = 0.27;

	// Check our conditions
	if ( WC()->customer->get_shipping_country() === $country && 
	     WC()->customer->get_shipping_state() === $state &&
     	 !is_null($chosen_shipping_method) && 
     	 strpos($chosen_shipping_method,'local_pickup') === false &&
     	 $is_physical_product === true
	   ) {
		WC()->cart->add_fee(__('Colorado Retail Delivery Fee'), $fee, false, 'standard' );
	}
}
add_action('woocommerce_cart_calculate_fees', 'dbdc_maybe_add_crdf');

Still Have Questions?

If you have questions or comments about this code, or if you’d like help adding the Colorado Retail Delivery Fee to your shop, send me a note!