How to Enable Delivery Slots for Selected Product Categories Only

WooCommerce Delivery Slots already gives you the option to exclude delivery slots for some selected product categories. But if you have ever-increasing categories and you want to enable delivery slots only for a selected few categories then the below code snippet would help you achieve that.

<?php

/**
 * Enable delivery slots only for the specified product categories.
 */
function iconic_enable_slots_for_specific_category( $allowed ) {
	// @todo Replace with your categories.
	$categories = array( 'shirt' );
	return iconic_check_for_cart_item_in_category( $categories );
}
add_filter( 'iconic_wds_delivery_slots_allowed', 'iconic_enable_slots_for_specific_category', 10, 1 );

/**
 * Check cart for product in category.
 *
 * @param array $categories Categories.
 *
 * @return bool
 */
function iconic_check_for_cart_item_in_category( $categories = array() ) {
	if ( empty( $categories ) || empty( WC()->cart ) ) {
		return false;
	}

	// Set our flag to be false until we find a product in that category.
	$has_item = false;

	// Check each cart item for our category.
	foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
		$product    = $cart_item['data'];
		$product_id = $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id();

		if ( has_term( $categories, 'product_cat', $product_id ) ) {
			$has_item = true;

			// Break because we only need one "true" to matter here.
			break;
		}
	}

	return $has_item;
}

In the `iconic_enable_slots_for_specific_category` function, replace ‘pizza’ with the slug of the category for which you want to enable delivery slots. You can add multiple categories in comma separated format

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

WooCommerce Delivery Slots

Choose a delivery date and time for each order. Add a limit to the number of allowed reservations, restrict time slots to specific delivery methods, and so much more.

Was this helpful?

Please let us know if this article was useful. It is the best way to ensure our documentation is as helpful as possible.