autoship_create_scheduled_order_link_handler (function)

(src/scheduled-orders.php) Used to process the Create Scheduled Order link data. It takes the $link (array) as a parameter and returns a boolean or WP_Error.

Function

/**
 * Processes the Create Scheduled Order Link Data
 * @param array $link {
 *          The link param in an array
 *          @type int|array $products       Array of woocommerce simple or variation ids or single id
 *          @type int       $qty            The qty to add
 *          @type int       $min            The min cycles
 *          @type int       $max            The max cycles
 *          @type int       $frequency      The frequency
 *          @type string    $frequency_type The frequency type
 *          @type int       $order          The Autoship Scheduled Order ID
 *          @type string    $coupon         The coupon code
 *          @type string    $customer_id    The WC Customer ID hashed
 *          @type float     $discount       The discount amount ( SUPPLIED AS NULL )
 * }
 *
 * @return bool|WP_Error  True on Success, False if no scheduled order exists,
 *                        WP_Error on failure
 */
function autoship_create_scheduled_order_link_handler( $link ){

  // The General notice is used for any link failures not related to coupons
  $general_notice = apply_filters( 'autoship_create_scheduled_order_link_invalid',
  __( 'The supplied link is no longer valid or has expired.', "autoship" ),
  $link );

  do_action( 'autoship_initiate_create_scheduled_order_link_handler', $link );

  if ( !isset( $link['customer_id'] ) )
  return new WP_Error( 'invalid_or_expired_link', $general_notice );

  $customer_id = $link['customer_id'];
  $args = array();

  // Check for frequency
  if ( isset( $link['frequency'] ) )
  $args['frequency'] = $link['frequency'];

  // Check for frequency type
  if ( isset( $link['frequency_type'] ) )
  $args['frequencyType'] = $link['frequency_type'];

  // Check for frequency type
  if ( isset( $link['next_occurrence'] ) )
  $args['nextOccurrenceUtc'] = $link['next_occurrence'];

  // Check for Products / Line Items
  if ( isset( $link['products'] ) && !empty( $link['products'] ) ){

    // Setup the Query Params for the QPilot call
    // By default we search for only those ids that are enabled to be added to an order.
    $valid_products = apply_filters( 'autoship_create_scheduled_order_endpoint_valid_products_params',
    array( 'productIds' => array_keys( $link['products'] ) ), $link );

    // Make the Search call
    $available_products = autoship_search_available_products( $valid_products );

    if ( !is_wp_error( $available_products ) && !empty( $available_products ) ){

      // Loop through the QPilot products and attach any needed info.
      $args['scheduledOrderItems'] = array();
      foreach ($available_products as $key => $value) {

        if ( !isset( $link['products'][$value->id] ) )
        continue;

        // Grab the prices - runs through the custom filter
        $prices = autoship_get_product_prices( $value->id );

        // Create the min-needed for a line item
        // Devs can adjust on the fly using URL params
        $line = apply_filters('autoship_create_scheduled_order_endpoint_item', array(
          'productId' => $value->id,
          'price'     => $prices['regular_price'],
          'salePrice' => $prices['autoship_recurring_price'],
          'quantity'  => $link['products'][$value->id]
        ), $link );

        // Check for Min Cycle Data
        if ( isset( $link['min'] ) && is_array( $link['min'] ) ){
          $line['minCycles'] = isset( $link['min'][$value->id] ) ? $link['min'][$value->id] : NULL;
        } else if ( isset( $link['min'] ) ){
          $line['minCycles'] = $link['min'];
        }

        // Check for Max Cycle Data
        if (  isset( $link['max'] ) && is_array( $link['max'] ) ){
          $line['maxCycles'] = isset( $link['max'][$value->id] ) ? $link['max'][$value->id] : NULL;
        } else if ( isset( $link['max'] ) ){
          $line['maxCycles'] = $link['max'];
        }

        $args['scheduledOrderItems'][] = $line;

      }

    }

  }

  // Allow a final filter for last adjustments & customizations
  $args = apply_filters( 'autoship_create_scheduled_order_endpoint_order_args', $args, $link );

  // Make the call to create the order.
  $result = autoship_create_scheduled_order( $customer_id, $args );

  return $result;
}

Still need help? Contact Us Contact Us