Skip Navigation

[Resolved] Need an exposed filter for a date range

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.

This topic contains 17 replies, has 2 voices.

Last updated by martham 4 months, 1 week ago.

Assigned support staff: Waqar.

Author
Posts
#2253903

We are trying to emulate the page located here:

hidden link

Our Toolset built version of this page is located here:

hidden link

The question I have for this support ticket is to ask for assistance with adding the date range filter that we have on the original version of the page, if that is doable using your plugin. I assume that this is possible, but I'm not seeing how to do it.

We would like the admin user accessing this report page to have the ability to show all results by default, but then to be able to add a date range to limit the results to orders within that range. Please let me know if this is a feature that we can do with your plugin.

#2254091

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

I couldn't access the example page, because it was showing the "Access Denied" message.

To add a date range search filter in your view, you can follow these steps:

1. In the view's "Search and Pagination" section, click the "New filter" button.
( example screenshot: hidden link )

2. On the next screen, select the date type custom field that you'd like to use for the search filter.
( example screenshot: hidden link )

3. In the settings for the filter, select the field type "Datepicker", compare value as "Number" and comparison type as "Between".
( example screenshot: hidden link )

As a result, you'll see two date picker fields on the front-end, which can be used to set a start and end date range for the search filtering.

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2254101

On step #2 of your instructions, I don't have a date type custom field. These are Woocommerce orders that we're listing here. I wanted to add a date range that uses the order creation date. Please let me know if that's doable and if so, how to do it. Thanks.

Sorry about you not being able to access the page. Brain glitch on my part forgetting that the original pages are admin access only.

#2254185

I figured out a way to use a field called paid_date . I thought this would work.

We currently have 3 test orders present in our store, all from December 6. I set the start date as December 15, 2021 and the end date as December 31, 2021 and the 3 orders were still present on the page. So the filter isn't working to remove results outside the range. Please advise if there's something else we should be doing here.

#2254925

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

The Toolset's date type custom fields store the selected date's value in the UNIX timestamp format (when no time is specified 12 am is used by default). The date type search filters in views are designed to work with this same format too.

The "paid_date" and other WooCommerce fields don't use the same format to store the dates, so the Toolset's view's date filters can't be used with those fields.

To overcome this, you can add a new Toolset date type custom field with the name "Order Completion Date" and slug "order-completion-date" for the post type "Orders".

Next, you can use some custom code that can ensure that whenever the status of an order is changed to "Completed", today's date value in the right format is automatically updated in that Toolset date type custom field.

For example:


function custom_fill_order_completion_date( $order_id ) {
	// get today's date time at midnight
	$date = new DateTime('today midnight');
	// get today's date time at midnight in UNIX timestamp
	$timestamp = $date->getTimestamp();
	// update the value in the "order-completion-date" field
	update_post_meta( $order_id, 'wpcf-order-completion-date', $timestamp );
}
add_action( 'woocommerce_order_status_completed', 'custom_fill_order_completion_date', 10, 1 );

add_action( 'save_post', 'custom_fill_order_completion_date_save_post', 10,3 );
function custom_fill_order_completion_date_save_post( $post_id, $post, $update ) {
	// exit if if new post is being created
	if ( !$update ){
		return;
	}

	// exit if post type is not "shop_order"
	if ( 'shop_order' !== $post->post_type ) {
		return;
	}

	// execute only if order's status is being changed to "completed"
	if( ($_POST['original_post_status'] != 'wc-completed') && ($_POST['order_status'] == 'wc-completed') ) {
		// get today's date time at midnight
		$date = new DateTime('today midnight');
		// get today's date time at midnight in UNIX timestamp
		$timestamp = $date->getTimestamp();
		// update the value in the "order-completion-date" field
		$_POST['wpcf']['order-completion-date']['datepicker'] = $timestamp;
	}
}

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.

This way, the Toolset's date type field will stay up-to-date and you'll be able to use it for the view's search filter, as explained in my last message.

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/

#2254987

OK. At least I don't feel stupid for not being able to get it to work. 😉 Wow, that's more complicated than I expected it to be. Thanks so much for providing that code. I'm really glad you guys are willing to guide us through these issues because there is no way in the world I'd have ever figured that out as what's needed to get it to work. 😉

I have added the code to the functions.php file successfully.

I believe I added the custom field correctly within Orders, but I'm not 100% sure of that. After adding the custom field, I tried adding the new field to the loop for the view. I added it successfully, but when I look at the results on the page, the field is not showing any data. I'm guessing that the code you provided should work for converting date field data from FUTURE orders but it's not built to convert date field data from previously existing orders. Is that correct?

Go here to see what I'm seeing in case I'm wrong with this guess:

hidden link

I've left the Post Date field present in the view next to the new Order Completion Date for now and will remove it once I see everything working properly with this field and with the date range filter.

#2255271

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for the update and I'm glad I could help.

Yes, as the code executes whenever an order status is set to complete, it will work for future orders, but not for the ones which are already completed.

To test, you can place a new order and then set its status to completed and then see if the "Order Completion Date" field's value is automatically populated or not.

#2255591

I must have done something wrong. I just placed a test order and the field does not have the date in it.

Go here:

hidden link

#2255895

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Can you please share temporary admin login details for your website, along with the exact testing steps that you followed?

I'll also need to know exactly where/how you've added the custom code.

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

#2257403

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing these details.

The custom code that I shared, automatically fills the value in the "Order Completion Date" field, when an order's status is changed to "completed".

When an order is placed, its status is set to "Processing".
( screenshot: hidden link )

When the payment for the order is completed or if the admin manually changes its status to "completed" from the order's edit screen, the order's status is changed to "Completed".
( screenshot: hidden link )

From the order edit screen of your most recent order "#1750 Kenneth Moore", I changed its status to "Completed" and the "Order Completion Date" field's value was filled automatically with today's date.
( screenshot: hidden link )

This means that the custom is working as expected and you can see that value showing the in view's results too.
( screenshot: hidden link )

#2257503

GRRRRRR. And no, I'm not GRRRRing at you. I'm GRRRRing at Woocommerce. Sorry I didn't realize the order was stuck in processing status again. I encountered that problem previously and found that I needed to change the product to a Virtual product to avoid this. I did that for all the products in here and just assumed the problem was solved by that. I didn't even think to check it on this test order. So now that problem is still not solved.

I'm happy with what you've done here as I now see that it works perfectly. I've elminated the Post Date field from the view now.

So now the next step is getting the date filter set up and working. I'll try that next. OK, I've set up a filter on this custom Order Completion Date field. When I'm on the page where the view is displayed, I see the filter present at the top. There is only one order present with the date field filled and the date is today January 3, 2022. So I chose January 3, 2022 as my start date and January 4, 2022 as my end date for the range thinking that would limit the results on the page to only the most recent test order. But all 4 test orders are still showing. I'm not seeing a button to apply the filter as I was expecting, so I'm assuming your filter just automatically applies and should update the page after the selection is made. I then tried changing the start date to January 1, 2021 and the end date to January 21, 2021 figuring that should change the results to no results on the page since we have no orders between those dates. But it's still showing all 4 orders. Can you look and see what I'm doing wrong here?

hidden link

hidden link

#2257505

I'll have to get with Woocommerce support on the orders being stuck in processing status problem.

#2257521

Just letting you know that I found some code to add to functions.php that should automatically change Woocommerce virtual product orders from Processing to Pending. I found that Woocommerce only does this by default for products marked Virtual AND Downloadable. These products are not downloadable, so I didn't mark them as such. That's why I still had the same order status problem that you found. I'm hoping that's now fixed on my next test order. But I'll wait until tomorrow before placing that test order so we can apply that test to making sure this date range filter is working properly. That way it will be a January 4, 2022 order and we can test just using that date as the start and end date to see if that is the only order that comes up after getting the January 3, 2022 one working.

#2257523

Oops. I meant to say change from Processing to Complete above, not Processing to Pending. Brain glitch on my part there.

#2258281

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing these updates.

In the view's "Custom Search Settings", the option "Full page refresh when visitors click on the search button" is selected, which means that the view will wait for the clicking on the submit button, to perform the search.

I've added the submit button and the date range search is working, when it is clicked.
( screenshot: hidden link )

You can adjust the option in "Custom Search Settings", if you'd like to trigger the search without the submit button.

For the questions related to the WooCommerce plugin's functionalities, it would be best to consult their official support, for the most accurate information.