autoship_update_order_schedule_action_handler (function)

( src/scheduled-orders.php) The main handler for the for updating Scheduled Orders schedule (Frequency, Frequency Type, Next Occurrence Date). It takes the (Scheduled) $order_id, and the $data needed to perform the action as parameters and returns whether the update was successful or not.

Function

/**
 * The main handler for the autoship_update_order_schedule action
 * Updates the Schedule ( Frequency, Frequency Type, and/or Next Occurrence ) for the supplied order ID
 *
 * @param int $order_id       The Autoship Scheduled Order ID
 * @param mixed $data         The Data necessary to perform this action.
 * @return int|bool|WP_Error  True if successful or WR_Error|false on failure.
 */
function autoship_update_order_schedule_action_handler( $order_id, $data ){

  $scheduled_order_label = autoship_translate_text( 'Scheduled Order' );

  //Verify it needs to be updated if possible
  $frequency_unchanged = $frequency_type_unchanged = $next_occurrence_unchanged = false;
  if ( isset( $data['original_autoship_order'] ) && !empty( $data['original_autoship_order'] ) ) {

    // Check to see if the frequency should be updated.
    $frequency_unchanged       = $data['frequency']       == $data['original_autoship_order']['frequency'];
    $frequency_type_unchanged  = $data['frequency_type']  == $data['original_autoship_order']['frequencyType'];
    $next_occurrence_unchanged = $data['next_occurrence'] == $data['original_autoship_order']['nextOccurrenceUtc'];

    if ( $frequency_unchanged && $frequency_type_unchanged && $next_occurrence_unchanged ){

      do_action( 'autoship_after_update_scheduled_order_schedule_handler_nochange', $order_id, $action , $data );
      wc_add_notice( __( sprintf( 'No changes updated for %s #%s.', $scheduled_order_label, $order_id ), 'autoship' ), 'notice' );
      return true;

    }

  }

  $error = '';
  $updated = $result = false;
  if ( !$frequency_unchanged || !$frequency_type_unchanged ){

    // Run the Set method
    // Method returns WP_Error on failure.
    $result   = autoship_set_scheduled_order_frequency ( $order_id, $data['frequency_type'], $data['frequency'] );
    $error    = __( sprintf( "A problem was encountered updating the %s's Frequency.", $scheduled_order_label ), 'autoship');
    $updated  = !is_wp_error( $result );

  }

  if ( !$next_occurrence_unchanged && !is_wp_error( $result ) ){

    // Run the Set method
    // Method returns WP_Error on failure.
    $result = update_scheduled_order_next_occurrence ( $order_id, $data['next_occurrence'] );
    $error = $updated ? __("The {$scheduled_order_label}'s Frequency was updated successfully, however, there was a problem updating the Next Occurrence Date.") :
    __("A problem was encountered updating the {$scheduled_order_label}'s Next occurrence Date.", 'autoship');

  }

  // Dynamically the error message to the actions message filter.
  if ( !empty( $error ) )
  add_filter( 'default_autoship_update_order_schedule_action_messages', function( $messages ) { return array_merge( $messages, array(
    'error' => $error,
  )); }, 11, 1 );

  return $result;

}

Still need help? Contact Us Contact Us