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.

This plugin is now ready for Beta release.  Please go to my Plugin Site to register your interest.

Or read on for how to do it yourself.

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.

I'll send you the plugin when it's ready

Please note: we do not share your information with anyone else. We will only use it to share helpful information regarding our plugin development.

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 , ,

11 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 […]

  3. Gerold says:

    Hi,
    you got the point!
    Pretty sure I am not the only one who needs to have a “check availabilty” widget for his website. I run a mountain hut in the alps and are building a new hp with woobooking right now for next season and a plugin that could do that would be really great!
    Seems you are so deep in this, maybe it would be possible for you to offer a plugin that does exactly this:
    # show availabilty widget
    # show all available rooms for the selected date
    # that´s all! Nothing more needed!

    Best Regards

  4. Larry says:

    Thanks for the initiation of this plugin. I would think that Beth is right on the money about the devs acting before thinking (modeling) the Bookings add-on; picking a date would always be a first asked request. I would love to help develop this with you. Can you fork it on github as of yet… or do you plan on beta testers only with sales intent—could be a good route to go.

    • beth says:

      Thanks Larry. No, I haven’t committed it to GitHub yet. My plan is to build a more functional version as a plugin for sales. But as you found, you can download this Beta test and do with it whatever you like. I’ll keep you updated as things progress. Thanks so much for commenting.

  5. Sassi says:

    It would be awesome, if f.e. you could have a calendar showing directly if any and how many bikes are available on each date visible in the calendar picker.

    So people would not have to check a gazillion dates to find one where a bike is actually available or four bikes if a group is looking to rent one per person at the same time.

    • beth says:

      The difficulty with this is processing time. Allowing the user the see 1 day or even a few days availability would be okay, but a full 30 day calendar view would take more than a minute. This is one of the reasons that Woo Bookings is built the way it is.

      I am working on something that allows a static view, see my post about availability calendars

      • Sassi says:

        Thanks for you reply!

        I do have another question:

        I would like to display the following first (__ is being selected by the customer:
        Rental Start Date: __
        Rental End Date: __

        Then the tool should display all available resource during that time frame.

        Is there a solution available from you, Beth?

  6. beth says:

    Hi Sassi. I have created a mini-plugin for this. You can see it here, https://plugins.doitsimply.co.uk/example-of-simple-date-first/ or you can read the write up I did of it below. If you have any questions please be in touch.

    Thanks.

    https://plugins.doitsimply.co.uk/2018/06/10/super-simple-availability-search-for-woocommerce-bookings/

Leave a Reply

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