• Resolved wearefoolsgold

    (@wearefoolsgold)


    Hi! I read the documentation for the daily reset time and tried to tweak that for what I need with no luck. Would greatly appreciate help with the following:
    I’m currently using the plugin to ‘restock’ weekly on my site, resetting my available number of orders on Sunday. I would like to make this happen at 5pm each week, rather than midnight. Is there a way to do this?

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Brian Watson

    (@bswatson)

    I have not had a chance to test this, so I apologize if it doesn’t work without some tweaking. Here’s an example plugin that you could try that would add a “Weekly at 5pm” option.

    Hope this helps get you going in the right direction!

    <?php
    /**
     * Plugin Name: Limit Orders for WooCommerce - Weekly Intervals at 5pm
     * Description: Add a "Weekly at 5pm" option to Limit Orders for WooCommerce.
     * Author:      Nexcess
     * Author URI:  https://nexcess.net
     */
    
    /**
     * Add "Annually" 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['weekly_at_5pm'] ) ) {
    		return $intervals;
    	}
    
    	$intervals['weekly_at_5pm'] = sprintf(
            /* Translators: %1$s is the first day of the week, based on site configuration. */
            _x( 'Weekly (resets every %1$s) at 5pm', 'order threshold interval', 'limit-orders' ),
            $wp_locale->get_weekday( get_option( 'start_of_week' ) )
        );
    
    	return $intervals;
    } );
    
    /**
     * Get a DateTime object representing the beginning of the week at 5pm.
     *
     * @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 "weekly_at_5pm".
     */
    add_filter( 'limit_orders_interval_start', function ( $start, $interval ) {
    	if ( 'weekly_at_5pm' !== $interval ) {
    		return $start;
    	}
    
        $start = current_datetime();
    
        $start_of_week = (int) get_option( 'week_starts_on' );
        $current_dow   = (int) $start->format( 'w' );
        $diff          = $current_dow - $start_of_week;
    
        // Compensate for values outside of 0-6.
        if ( 0 > $diff ) {
            $diff += 7;
        }
    
        // A difference of 0 means today is the start; anything else and we need to change $start.
        if ( 0 !== $diff ) {
            $start = $start->sub( new \DateInterval( 'P' . $diff . 'D' ) );
        }
    
        return $start->setTime( 17, 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 "weekly_at_5pm".
     */
    add_filter( 'limit_orders_next_interval', function ( $start, $current, $interval ) {
    	if ( 'weekly_at_5pm' !== $interval ) {
    		return $start;
    	}
    
    	return $current->add( new \DateInterval( 'P7D' ) );
    }, 10, 3 );
    
    Thread Starter wearefoolsgold

    (@wearefoolsgold)

    Hi Brian!

    Thank you so much for this! I will test this out and see what happens.

    Best,

    Meredith

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Reset Time (Weekly)’ is closed to new replies.