Skip to content

Instantly share code, notes, and snippets.

@stevegrunwell
Last active November 17, 2022 21:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevegrunwell/3de6aecc6bc7af9ca1c610d6435f05e9 to your computer and use it in GitHub Desktop.
Save stevegrunwell/3de6aecc6bc7af9ca1c610d6435f05e9 to your computer and use it in GitHub Desktop.
Set a custom start time for the "daily" interval when using Limit Orders for WooCommerce.
<?php
/**
* Plugin Name: Limit Orders for WooCommerce - Custom Daily Interval
* Description: Restart daily order limiting at a time other than midnight.
* Author: Nexcess
* Author URI: https://nexcess.net
*/
/**
* Get a DateTime object representing the start of the daily interval.
*
* @param \DateTime $start The DateTime representing the start of the current interval.
* @param string $interval The type of interval being calculated.
*
* @return \DateTime A DateTime object representing the starting point of the daily interval.
*/
add_filter( 'limit_orders_interval_start', function ( $start, $interval ) {
if ( 'daily' !== $interval ) {
return $start;
}
/*
* Set the time the interval should begin.
*
* - $hours should be 0-23 and will be based on the store's timezone.
* - $minutes should be 0-59.
* - $seconds should be 0-59.
*/
$hours = 20;
$minutes = 0;
$seconds = 0;
// If the given time occurs after right now, adjust it by -24 hours.
$now = current_datetime();
$then = $now->setTime( $hours, $minutes, $seconds );
if ( $then > $now ) {
$hours -= 24;
}
// Set the interval start time.
return $start->setTime( $hours, $minutes, $seconds );
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment