Skip to content

Instantly share code, notes, and snippets.

@stevegrunwell
Created May 19, 2020 04:50
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/ab8a7a2036f993c3c09c6504acda96eb to your computer and use it in GitHub Desktop.
Save stevegrunwell/ab8a7a2036f993c3c09c6504acda96eb to your computer and use it in GitHub Desktop.
Add an "Every 15 minutes" option to Limit Orders for WooCommerce
<?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 );
@stevegrunwell
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment