do it simply - Help and Advice

How to select the date first on Woocommerce Bookings

I know I am not alone in wanting the functionality for Woocommerce Bookings.  This just makes logical sense, choose a date, then find out what resources are available on the date chosen.  Bookings doesn’t do this out of the box.

I am currently in the process of writing a plugin for this functionality. Would you please send me your wish list for the having this on your site? I will email you when the plugin is done.

I needed to create this functionality for a client.   They hire mountain bikes out by the 1/2 day or full day.  Who wants to select bikes again and again to see if they are available on any given day?  No-one!  So I rebuilt how Woocommerce bookings works.  Essentially I flipped the booking process on it’s head.  Reversing the events.

  • 1. Select date
  • 2. Select length of hire (booking)
  • 3. Choose from available resources (bikes)

My solution was an indepth build of a custom plugin, but if you understand the core WC_Product_Booking which extends WC_Product used in the Woocommerce Plugin, you should be able to solve this for yourself.  You find this class in your core Woocommerce Bookings Plugin woocommerce-bookings->includes->class-wc-product-booking.php.  If you haven’t explored plugin files before, I encourage you to do it.  It is the quickest way to understand how something is built and how you can use built in functionality to build something bespoke for your site.

To see my solution in action, please visit the bike hire page on Pedalabikeaway.

Within this class you have a function called, get_available_bookings, this is what you want to use.  The answer is in the name.  You will need to send 4 variables to this function to get your resource availability.

  • 1 and 2. the Date (yeah! that is the whole point of this exercise), you will need to send this in UNIX and you will need the start and end time of what you are checking.
  • 3. the Product ID of the item you are checking
  • 4. the Resource ID of the resource you are checking

Because my client had the option of hiring a bike for the morning, afternoon, or full day, I had 3 different date groups to send to this function.  I then compared all 3 to find out if they were all equal to 0 or more, only then did my query produce an available bike for that date.


I call this function for each product (bike) that I want to check.  It returns an array I can use to show how many bikes are available for a selected date and duration.  I then use this array to create my drop down list of all bikes available selected on the given date and duration.  Using the function above is really all you need.  But sometimes having the full context that something is used in helps to explain things fully.  You can stop here or carry on for my full solution.

To check what products are available on any given day we have to start out with the right group of products.  Assuming you give your customer a choice here you need to create a post object based on these.  For me, it was selecting a category of bikes, and then what the hire period is.  You can ignore much of this other then the WP post query as the rest are my ajax calls to get the selected variables.

Using these arguements to filter I get a selection of products that I want to check resources against on the chosen date and period.

Also notice the call to the global variable $wpdb, this allows me to connect to the Woocommerce databases to select the exact products and resources I am looking to check.  We need to do a little dirty database querying here.  Nothing to be nervous of.  Once you try it and use it, you’ll wonder why you didn’t use it before.

Please review this working on my clients site.  This is my first attempt at writing up my solutions for Woocommerce Bookings.  I would greatly appreciate feedback and questions.  I will answer all that I can.

Good luck!


Posted in DevelopersTagged , ,

3 responses to “How to select the date first on Woocommerce Bookings”

  1. Marcus J Wilson says:

    Hi Beth –

    Nice work! I agree – in many cases, it totally makes sense to allow the user to choose a date first, and then see if there’s any resource available. Do you know if your solution is still working in the latest version of WooCommerce/Woo Bookings?

    Best wishes

    • beth says:

      Thanks Marcus. I think WooBookings created the plugin, then got too deep into it when they realised that date first is a must. They would need to rewrite the whole plugin to make the functionality part of the core..

      My code hasn’t changed and is still working through all of the updates this past year. I’ve tidied my code a bit since this, but the functionality is the same. If you have any questions or get stuck give me a shout.

      Best – Beth

  2. […] before seeing what they are able to book. You can see my write up to that selecting the date first, here.  The other big one […]

Leave a Reply

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