Scheduled Orders: Change Next Occurrence if Date Falls on Weekend

If the Scheduled Order's next_occurence_date falls on a weekend, moves the next_occurence_date to Monday. 

Important: Always test customizations to your site on a staging environment before making changes to your live / production site.  If you are not experienced with testing changes on a staging site, here is a good article to review.

 
/**
 * Globally change Scheduled Orders' next occurrence date to a Monday if date falls on a Sat or Sun
 *
 * @param string       $next_occurrence The current assigned next occurrence date
 * @param string       $frequency_type. The frequency type assigned to this product
 * @param int          $frequency.      The actual frequency duration.
 * @param string       $creation_date   The creation date for the order.
 *                                      Date should be in Y-m-d H:i:s format ( 2019-07-19 15:21:39 )
 * @return string The filtered next occurrence date should be in Y-m-d\TH:i:s format.
 */
function xx_change_next_occurrence_date ($next_occurrence, $frequency_type, $frequency, $creation_date, $wc_order){

    // Call the API to get the calculated next occurrence date based off creation.
    $calculated_date = autoship_calculate_scheduled_order_next_occurrence ( $frequency_type, $frequency, $creation_date );
  
    // Check for API errors.
    if ( is_wp_error( $calculated_date ) )
    return $next_occurrence;
  
    // Get the day for the current date time string.
    $day = date("D", strtotime( $calculated_date ));
  
    // if day is Sat add 2 days to next occurrence
    // else if day is Sun add 1 day to next occurrence
    if ( $day == 'Sat' ){
      $next_occurrence = date("Y-m-d\TH:i:s", strtotime( $calculated_date ) + 172800);
    } else if ( $day == 'Sun' ){
      $next_occurrence = date("Y-m-d\TH:i:s", strtotime( $calculated_date ) + 86400);
    }
  
    return $next_occurrence;
  
  }
  add_filter('autoship_create_scheduled_order_next_occurrence', 'xx_change_next_occurrence_date', 10, 5);

Still need help? Contact Us Contact Us