Skip to content

Instantly share code, notes, and snippets.

@stevegrunwell
Created June 19, 2020 14:48
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/f9cc978cd5c23f744142798b7b772ba6 to your computer and use it in GitHub Desktop.
Save stevegrunwell/f9cc978cd5c23f744142798b7b772ba6 to your computer and use it in GitHub Desktop.
Add a "Forever" option to Limit Orders for WooCommerce - https://wordpress.org/support/topic/all-time-interval/
<?php
/**
* Plugin Name: Limit Orders for WooCommerce - Never-ending Interval
* Description: Add a "Forever" option to Limit Orders for WooCommerce.
* Author: Nexcess
* Author URI: https://nexcess.net
*/
/**
* Add "Forever" 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['forever'] ) ) {
return $intervals;
}
$intervals['forever'] = __( 'Forever (never resets)', 'limit-orders' );
return $intervals;
} );
/**
* Get a DateTime object representing some time in the past.
*
* @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 some time in the past or $start, if the current $interval is
* not "forever".
*/
add_filter( 'limit_orders_interval_start', function ( $start, $interval ) {
if ( 'forever' !== $interval ) {
return $start;
}
// Start at the top of the previous hour.
return $start->setTime( (int) $start->format( 'G' ) - 1, 0, 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 "forever".
*/
add_filter( 'limit_orders_next_interval', function ( $start, $current, $interval ) {
if ( 'forever' !== $interval ) {
return $start;
}
return $current->add( new \DateInterval( 'P100Y' ) );
}, 10, 3 );
@stevegrunwell
Copy link
Author

"Forever" is relative; if your store is online for 100 years, well, here's to your health! 🍻

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