Created
May 19, 2020 04:50
-
-
Save stevegrunwell/ab8a7a2036f993c3c09c6504acda96eb to your computer and use it in GitHub Desktop.
Add an "Every 15 minutes" option to 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 - 15min Intervals | |
* Description: Add an "Every 15 minutes" option to Limit Orders for WooCommerce. | |
* Author: Nexcess | |
* Author URI: https://nexcess.net | |
*/ | |
/** | |
* Add "Every 15min" to the list of intervals. | |
* | |
* @param array $intervals Available time intervals. | |
* | |
* @return array The filtered array of intervals. | |
*/ | |
add_filter( 'limit_orders_interval_select', function ( $intervals ) { | |
// Return early if it already exists. | |
if ( isset( $intervals['15min'] ) ) { | |
return $intervals; | |
} | |
$intervals['15min'] = __( 'Every 15min (Resets every quarter hour)', 'limit-orders' ); | |
return $intervals; | |
} ); | |
/** | |
* Get a DateTime object representing the beginning of the current quarter-hour. | |
* | |
* @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 top of the current hour or $start, if the | |
* current $interval is not "15min". | |
*/ | |
add_filter( 'limit_orders_interval_start', function ( $start, $interval ) { | |
if ( '15min' !== $interval ) { | |
return $start; | |
} | |
return $start->setTime( | |
(int) $start->format( 'G' ), | |
floor( (int) $start->format( 'i' ) / 15 ) * 15, // Determine which quarter-hour we're currently in. | |
0 | |
); | |
}, 10, 2 ); | |
/** | |
* Filter the DateTime at which the next interval should begin. | |
* | |
* @param \DateTime $start A DateTime representing the start time for the next interval. | |
* @param \DateTime $current A DateTime representing the beginning of the current interval. | |
* @param string $interval The specified interval. | |
* | |
* @return \DateTime The DateTime at which the next interval should begin, or $start if the | |
* current $interval is not "15min". | |
*/ | |
add_filter( 'limit_orders_next_interval', function ( $start, $current, $interval ) { | |
if ( '15min' !== $interval ) { | |
return $start; | |
} | |
return $current->add( new \DateInterval( 'PT15M' ) ); | |
}, 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looking to use a custom interval other than 15min? This follow-up post in the WordPress.org forum breaks down the key lines in this plugin.