Documentation

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
 * @return bool
 */
function iconic_check_for_cart_item_in_category( $categories = array() ) { 
	if ( empty( $categories ) ) { 
		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']; 

		if ( has_term( $categories, 'product_cat', $product->get_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.

Are you using WooCommerce Delivery Slots yet?

Start your 14-day free trial right now and get instant access to the features described in this article.

or learn 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.

WooCommerce Delivery Slots

WooCommerce Delivery Slots

Not got the plugin yet? Get instant access right now, free for 14 days.

or learn more

Still need help?

If you haven't found what you're looking for in our documentation, please contact support.

Get Support