Last active
November 17, 2022 21:57
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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