Skip Navigation

[Resolved] Button to search businesses that are Open Now and display Open Now text

This support ticket is created 3 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 23 replies, has 3 voices.

Last updated by Omek 3 years, 4 months ago.

Assisted by: Waqar.

Author
Posts
#2089951

For a business directory I set up Days of week/office hours with repeatable field groups based on this tutorial https://toolset.com/course-lesson/creating-and-displaying-repeatable-field-groups

How can I search a business that is Open Now?
Also how to display a business as "Open Now" or "Closed" in search results and on each business page?

I'm using shortcodes/classic editor for the search. I'm using View blocks for the business page template

#2090299

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Could you please clarify exactly what the data structure is that you've used?

You created a custom post type for businesses, and then how do the repeatable field groups look?

I have to say that it may not be possible to search for businesses that are open in this way, because repeatable field groups are implemented via child posts, and we currently do not support filtering/searching by fields of related posts (which is how the repeatable field groups are implemented under the hood).

#2092155

Yes I created a custom post type for businesses and I followed the instructions in the tutorial to use repeatable field groups for office hours.

How can I display that a business is Open Now in the results and on the business page template?

If repeatable field groups is not the way to go, what other method, if any, can be used to search for businesses that are open now?

#2092277

Hi,

Thank you for sharing these details.

I'm afraid, there is no built-in feature available to achieve this, so it will require some workaround.

Can you please share temporary admin login details, so that I can see how this repeatable field group is set up?

I'll be in a better position to guide you with some pointers accordingly.

Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.

regards,
Waqar

#2095931

Any update or suggestions ?

#2096297

Thank you for waiting.

I've been performing some tests with a couple of different approaches and will be able to share my findings with you today.

Thank you for your patience.

#2098693

Hello, still waiting on the findings from the tests you performed

#2098801

Thank you for waiting and here are my findings.

The use of repeatable field groups is useful when working with items that are infinite or variable in numbers. However, in the case of weekdays which are fixed in number (7), it can result in an unnecessary complication.

On my test website, I added 7 single line type custom fields for each day of the week (without any repeatable field group):

- monday-hours
- tuesday-hours
- wednesday-hours
- thursday-hours
- friday-hours
- saturday-hours
- sunday-hours

In these fields, I added the opening and closing hours in the same format as they're added on your website, e.g.

9am-5pm
4am-7pm
10pm-11pm
etc.

Next, I needed the custom shortcode, which can get the custom field value from the field which has today's day in its slug and then compare the current time with the opening and closing hours on that day. If the current time falls within that hour range, then it returns 'yes' (means it is open) and if not, it returns 'no' (means not open):


add_shortcode('custom_open_check', 'custom_open_check_func');
function custom_open_check_func( $atts ) {
	global $post;

	$a = shortcode_atts( array(
		'id' => $post->ID
	), $atts );

	$day = strtolower(date('l'));

	$field_slug = $day.'-hours';
	$field_value = types_render_field( $field_slug, array( 'item' => $a['id'], 'output' => 'raw' ) );

	$field_value_arr = explode('-', $field_value);

	$current_time = current_time( 'timestamp' );
	
	$startdatetime = new DateTime($field_value_arr[0]);
	$enddatetime = new DateTime($field_value_arr[1]);

	if(($startdatetime->format('U') <= $current_time) && ($current_time <= $enddatetime->format('U'))) {
		return 'yes';
	}
	else
	{
		return 'no';
	}
	
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

After that, you can use this shortcode in the conditional blocks in your content template or views to show or hide particular content or results based on whether the practitioner is open or not.

I hope this helps and let me know if any point is not clear.

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

#2101693
conditional-test2.png
conditional-test1.png

Thank you. It seems I have gotten it to work but maybe with some glitches. This past weekend when changing the Sunday time for testing purposes the Open Now/Close Now conditional wouldn't change. Currently it seems to be working correctly so I'm not sure what the issue could have been.

These are my Views conditional shortcodes:
[wpv-conditional if="( '[custom_open_check]' eq 'yes' )"]Open Now[/wpv-conditional]
[wpv-conditional if="( '[custom_open_check]' eq 'no' )"]Closed Now[/wpv-conditional]

The attached images are how I setup it up for content template, they both seem to work, should it be done with or without the shortcode attribute?

Are my shortcodes correct? Is equaling a static value of yes or no the correct way to make it work?
How do I incorporate it into Search to display only Open Now businesses along with the other filters?

Entering the word Closed in the Custom Field caused the site to break. My solution is to use a conditional to display the word Closed if a day of the week Custom Field is left blank. Is this the proper course of action? Is there a simpler way to show Closed on a particular day of the week?

#2101859

Now its not working.
I had one business set to 3pm-11pm, earlier it displayed correctly as Open Now, it is not yet 11pm but it is displayed as Closed Now. Manually changing the business hours on other businesses doesn't display Open Now, although I was able to do so earlier.
It seemed to be working fine in earlier hours but after maybe 9pm I begin to have issues. This also happened on Sunday.
What could be wrong?

#2102649

Trying to pinpoint the issue...
For today (Tuesday) I set each business to a different closing time, 7pm, 8pm, 9pm, 10pm etc. It worked throughout the day.
I checked a few minutes before 8pm it was working
I checked a few minutes after 8pm and all businesses were "Closed Now" regardless if the closing time was set to much later.

What could cause this? Please advise

#2102887

Thanks for the update.

The custom shortcode accepts only one attribute:


[custom_open_check id='1234']

This will check the open/close status for the post with the ID "1234".

However, when using it inside the single post content template or the loop of a view, you don't need to provide the ID attribute and it can automatically pick it from the current post.


[custom_open_check]

An important point to remember is that the code is designed to use the "Timezone" set in the WordPress settings as the reference.
( WP Admin -> Settings -> General )

Please make sure that you're setting the hours based on that timezone set in your WordPress setting and also testing the open/closing hours with respect to that time.

Your visitor can be coming from different time zones, so you can add a note on your website that all the opening and closing time information follows a specific timezone.

#2103457
tmezone.jpg

My timezone settings are correct for my area. Attached are my timezone settings. Should they be set differently?
I've also tried UTC and UTC-4. The issue of defaulting to Closed Now after 8 pm and not changing until the following day still remains.

Wordpress also states that current_time( 'timestamp' ) is not recommended. What would be the issue?

Also I've yet to receive any info how to incorporate this into search once this actually gets working.

#2104455

Please advise on the how to incorporate this into search?

Please advise on the timezone issue?

#2104529

To exclude "closed" posts from the view's results, you'll need to use "wpv_filter_query" filter, with this newly registered custom shortcode:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

Example:


add_filter( 'wpv_filter_query', 'custom_filter_open_close', 1000 , 3 );
function custom_filter_open_close( $query_args, $view_settings ) {
	// list of view's to process
	$target_views = array(1234, 5678);
	
	// process if specific view
	if ( ( isset($view_settings['view_id']) &&  in_array($view_settings['view_id'], $target_views)) ) {

		$args = array(
			'post_type'        => $query_args['post_type'],
			'posts_per_page'   => -1,
			'post_status'      => 'publish',
		);

		$posts_array = get_posts( $args );

		foreach ($posts_array as $post) {
			$open_close_val = do_shortcode('[custom_open_check id='.$post->ID.']');
			if($open_close_val == 'no') {
				$close_arr[] = $post->ID;
			}
		}

		$query_args['post__not_in'] = $close_arr;
		
	}

	return $query_args;
}

Note: You'll replace the comma-separated list "1234, 5678" with the actual IDs of the target views to process this filter on.

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

I've performed further testing on my website, but I'm still unable to find any cases where the custom shortcode fails or performs unexpectedly.

The old admin access authentication link no longer works. Can you please share temporary admin login details again, along with:

1. The exact time in your system when you performed the failed test
2. The screenshot of "Timezone" set in the WordPress settings at the time the test fails
3. The exact post for which the test failed

I've set your next reply as private.