Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rest Api /products/slots/ cannot get slots from the past #340

Open
2 tasks
rtpHarry opened this issue Jun 5, 2023 · 1 comment
Open
2 tasks

Rest Api /products/slots/ cannot get slots from the past #340

rtpHarry opened this issue Jun 5, 2023 · 1 comment
Labels
priority: low The issue/PR is low priority—not many people are affected or there’s a workaround, etc. type: enhancement The issue is a request for an enhancement.

Comments

@rtpHarry
Copy link

rtpHarry commented Jun 5, 2023

What I expected

I want to be able to view all slots back to the $min_date, regardless of whether it is in the past.

There is a $get_past_times param which makes it look like it is possible, but that is just a holdover from the underlying plugin. (Update: see my reply below, this does actually seem to be the key to this working)

I am building an admin utility for a client and I need to be able to display historical slots.

It is just three lines of code that are stopping this, and without it, I have to adopt hundreds of lines of code into my admin plugin, and maintain it.

File: wp-content/plugins/woocommerce-bookings/includes/api/class-wc-bookings-rest-products-slots-controller.php

	/**
	 * Get available bookings slots.
	 *
	 * @param WP_REST_Request $request Full details about the request.
	 * @return WP_REST_Response|WP_Error
	 */
	public function get_items($request)
	{
		// ... snip ...

		// 1: add a new param
		$allow_past_slots = isset($request['allow_past_slots']) && 'true' === $request['allow_past_slots'] ? true : false;

		// ... snip ...

		// 2: pass it in via the `use` block
		// Calculate partially booked/fully booked/unavailable days for each product.
		$booked_data = array_values(array_map(function ($bookable_product) use ($min_date, $max_date, $resource_ids, $get_past_times, $timezone, $hide_unavailable, $allow_past_slots) {

			// ... snip ...

			// 3: update the `if` statement
			// Reset $min_date if it's before the minimum bookable date/time in the future.
			if (!$allow_past_slots && ($min_date < $min_bookable_date)) {
				$min_date = $min_bookable_date;
			}

			// ... snip ...

		}

		// ... snip ...

	}

That if statement just simply needs to check if the $allow_past_slots param is false & (the rest of its check).

What happened instead

The slots are clamped to only show the minimum bookable date for the product.

Instead I have had to store a hash of the file in my plugin, and I'm comparing it to alert me to if there are ever any changes to the WC Bookings Accommodation plugin in the future.

I've had to adopt 300 lines of code from the plugin into mine to recreate this rest api with the fix applied.

This isn't ideal from an ongoing site stability viewpoint, as I now have to maintain it for the clients site to stay functional.

Steps to reproduce the issue

  1. Make a rest request to /wc-bookings/v1/products/slots/ with a $min_date before the current date

  • Issue assigned to next milestone.
  • Issue assigned a priority (will be assessed by maintainers).
@vikrampm1 vikrampm1 added priority: low The issue/PR is low priority—not many people are affected or there’s a workaround, etc. type: enhancement The issue is a request for an enhancement. labels Jun 6, 2023
@rtpHarry
Copy link
Author

rtpHarry commented Jun 6, 2023

While implementing this plan I have discovered that the existing $get_past_times param must be set to true for it to actually pull in the previous slots.

I wasn't sure if the naming "times" meant something else, but it seems like it does mean "slots".

If that is the case then I would probably change my report to this being a bug, rather than a feature request.

And the fix is to check the existing $get_past_times param in the min date if statement:

if (!$get_past_times && ($min_date < $min_bookable_date)) {

So just a one line fix, if anyone can confirm that $get_past_times is supposed to mean slots, and doesn't have some other implications?

It is passed down into the WC Bookings plugin layers so it would be down in there where any gotchas may lurk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: low The issue/PR is low priority—not many people are affected or there’s a workaround, etc. type: enhancement The issue is a request for an enhancement.
Projects
None yet
Development

No branches or pull requests

2 participants