One of our WooCommerce addons allows the admin to reset the download limits for a given customer OR for all customers. It is usually used with WooCommerce virtual and/or downloadable products. It’s called Orbisius Reset Download Limits for WooCommerce.

Here's a video explanation of this article:

 

A client contacted us recently to let me know that he has about 5,000+ orders and their number keeps increasing which is a good thing. That means at the marketing machine is working great.

The way the this plugin has worked until now was to process the records right away when the form was submitted. That meant that the plugin had to finish its thing before the admin sees any information. This is fine in 80-90% of the cases. In this client’s specific case that was going to be a problem because php scripts can run for a given period of time which is usually between 30 and 180 seconds depending on the server configuration. This can be extended with some white magic.

To execute the long running process we’ve decided to use WP-Cron. That way we didn’t have to create a complicated solution that would require our customers to do lot of work on their end.

Right now, if the admin has selected to Reset the download limits for all customers then we’ll schedule that to happen behind the scenes and the page will inform the admin that the operation was scheduled.

If the admin has selected only one customer then the regular functionality would run.

Normally, WordPress WP-Cron is used to schedule repeated tasks/events. In our case we needed this to happen only once upon request.

The function name as you have probably guessed it is called: wp_schedule_single_event

https://codex.wordpress.org/Function_Reference/wp_schedule_single_event

This is how we've used this (this is portion of the WordPress plugin)

$reset_params = array(
	'user_id' => $user_id,
	'product_id' => $product_id,
	'reset_exp_date' => $reset_exp_date,
	'remove_exp_date' => $remove_exp_date,
);

if ($reset_all) {
	$status_rec['msg'] = 'Scheduled';
	$status_rec['status'] = 1;
	wp_schedule_single_event( time() + 2, 'orbisius_woocommerce_ext_reset_download_limits_action_reset_all', array( $reset_params ) );
} else {
	$status_rec = orbisius_woocommerce_ext_reset_download_limits( $reset_params );
}
		
		
add_action( 'orbisius_woocommerce_ext_reset_download_limits_action_reset_all', 'orbisius_woocommerce_ext_reset_download_limits_bg_worker' );		

function orbisius_woocommerce_ext_reset_download_limits_bg_worker($reset_params) {
	// Do some awesome work in background.
}

As a WordPress plugin developer how do you run a time consuming process?

 

Disclaimer: The content in this post is for educational purposes only. Always remember to take a backup before doing any of the suggested steps just to be on the safe side.
Referral Note: When you purchase through a referral link (if any) on this page, we may earn a commission.
If you're feeling thankful, you can buy me a coffee or a beer