Skip to content

Instantly share code, notes, and snippets.

@braddalton
Created November 8, 2023 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save braddalton/6754784eb12920b19e3f5df560d89be3 to your computer and use it in GitHub Desktop.
Save braddalton/6754784eb12920b19e3f5df560d89be3 to your computer and use it in GitHub Desktop.
Random Free Product For Each Day in WooCommerce https://wpsites.net/?p=114123
// Schedule the function to run hourly
add_action('init', 'schedule_update_random_free_product');
// Function to select and update a random free product
function update_random_free_product() {
$random_product_id = get_random_free_product();
if ($random_product_id) {
update_option('random_free_product_id', $random_product_id);
update_option('random_free_product_date', current_time('Y-m-d'));
}
}
// Hook the function to run when the scheduled event fires
add_action('update_random_free_product_event', 'update_random_free_product');
function schedule_update_random_free_product() {
if (!wp_next_scheduled('update_random_free_product_event')) {
wp_schedule_event(current_time('timestamp'), 'hourly', 'update_random_free_product_event');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment