autoship_add_to_scheduled_order_endpoint_wrapper (function)

( src/scheduled-orders.php) Used to wrap the functionality for adding an item to Scheduled Orders using the Autoship link builder. It takes the $action being performed as its parameter.

Function 

/**
 * Adds a WooCommerce item to a Scheduled Order using the Query Vars.
 * NOTE If a Frequency & Frequency Type is not supplied then the item will be
 * added to the next scheduled order.
 */
function autoship_add_to_scheduled_order_endpoint_wrapper ( $action ){

  // A product is required for this endpoint.
	if (  apply_filters( 'autoship_add_to_scheduled_order_endpoint', $action == 'add-to-scheduled-order', $action )  ) {

    // Default possible query vars which allows devs to set
    $link = autoship_get_scheduled_order_link_params( 'add_scheduled_order_item' );

    if ( !isset( $link['products'] ) )
    return false;

    $customer_id = 0;
    $notice = array( 'notice' => '', 'notice_type' => '' );

    if ( !isset( $link['customer_id'] ) )
    $notice = array( 'notice' => __( sprintf( 'A problem was encountered while attempting to add this item to the %s.  Please try again.', autoship_translate_text( 'scheduled order' ) ), 'autoship' ), 'notice_type' => 'error' );

    if ( empty( $notice['notice'] ) ){

      // Process the link data and add the item to the next order(s)
      $result = autoship_add_to_scheduled_order_link_handler( $link );

      if ( !$result )
      do_action( 'autoship_add_to_next_order_no_order_found', $link, $customer_id );

      if ( is_wp_error( $result ) )
      $notice = array( 'notice' => $result->get_error_message(), 'notice_type' => 'error' );

      // Check if no future orders exist
      if ( false != $result && !is_wp_error( $result ) ){

        $scheduled_orders_label = autoship_translate_text( 'scheduled orders' );
        $scheduled_order_label = autoship_translate_text( 'scheduled order' );

        // Multiple cycles or just one terminology
        $qty_string = $link['max'] > 1 ?
        __( sprintf( '%d item(s) were successfully added to your next %d %s.', $link['qty'], $link['max'], $scheduled_orders_label ), 'autoship' ) :
        __( sprintf( '%d item(s) were successfully added to your next %s.',  $link['qty'], $scheduled_order_label ), 'autoship' );

        $notice = array(
          'notice'      => $qty_string,
          'notice_type' => 'success' );

      }

    }

    // Allow filtering of redirect
    $redirect = apply_filters('autoship_add_to_next_order_notice_url', '', $notice['notice_type'], $link, $customer_id );

    // Let devs adjust notices and redirect.
    $notice = apply_filters( 'autoship_add_to_next_order_notice', $notice, $link, $customer_id );

    if ( !empty( $notice['notice'] ) )
    wc_add_notice( $notice['notice'] , $notice['notice_type'] );

    if ( !empty( $redirect ) ){
      wp_redirect( $redirect );
      exit();
    }

    return true;

  }

}