autoship_update_schedule_items_action_handler (function)

src/scheduled-orders.php) The main handler for the for updating Scheduled Orders items. 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_schedule_items action
 * Updates the Scheduled Items 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_schedule_items_action_handler( $order_id, $data ){

  // Get Current Scheduled Order items &
  // Verify it needs to be updated if possible
  $original_autoship_order_items = $updated_autoship_order_items = array();
  if ( isset( $data['original_autoship_order'] ) && !empty( $data['original_autoship_order'] ) ) {

    $original_order_items = $data['original_autoship_order']['scheduledOrderItems'];

    // Flip supplied ids for better lookup.
    $item_ids     = array_flip( array_keys( $data['order_items'] ) );

    // Iterate through the scheduled order items and unset any
    // not requested.
    foreach ($original_order_items as $key => $item) {

      if ( empty( $item_ids ) || isset( $item_ids[$item['id']] ) )
      $original_autoship_order_items[$item['id']] = $original_order_items[$key];

    }

  } else {

    $original_autoship_order_items = autoship_get_scheduled_order_items( $order_id, array_keys( $data['order_items'] ) );
    $original_autoship_order_items = autoship_convert_object_to_array( $original_autoship_order_items );

  }

  // Check changes are actual changes and if so init updates.
  foreach ( $data['order_items'] as $item_id => $item) {

    $change = false;
    $temp = $original_autoship_order_items[$item_id];

    // Insert item value update code ( need to make extendable )
    if ( $original_autoship_order_items[$item_id]['quantity'] != $item['qty'] ){

      $change = true;
      $temp['quantity'] = $item['qty'];

    }

    if ( isset( $item['price'] ) && ( $original_autoship_order_items[$item_id]['price'] != $item['price'] ) ){

      $change = true;
      $temp['price'] = $item['price'];

    }

    if ( isset( $item['sale_price'] ) && ( $original_autoship_order_items[$item_id]['salePrice'] != $item['sale_price'] ) ){

      $change = true;
      $temp['salePrice'] = $item['sale_price'];

    }

    // If there was indeed a change then add it to updated items.
    if ( apply_filters( 'autoship_update_schedule_items_changed_made',
    $change,
    $original_autoship_order_items[$item_id],
    $item ) )
    $updated_autoship_order_items[$item_id] = $temp;

  }

  /**
  * Allow devs to modify item changes on the fly.
  */
  $updated_autoship_order_items = apply_filters(
    'autoship_update_schedule_items_changes',
    $updated_autoship_order_items,
    $original_autoship_order_items,
    $order_id, 'autoship_update_schedule_items' , $data
  );

  /**
  * Now update any existing order items if necessary.
  */
  if ( !empty( $updated_autoship_order_items ) ){

    // Run the Set method
    // Method returns WP_Error on failure.
    $result = autoship_set_scheduled_order_items ( $order_id, $updated_autoship_order_items );

  }

  // Check for new items
  $new_autoship_order_items = array();
  if ( ( empty( $updated_autoship_order_items ) || ( !empty( $updated_autoship_order_items ) && !is_wp_error( $result ) ) ) &&
        !empty( $data['new_order_items'] ) ){

    // Get Current Scheduled Order Frequency and FrequencyType needed for new items
    $original_frequency = $original_type = '';
    if ( !isset( $data['original_autoship_order'] ) || empty( $data['original_autoship_order'] ) ){
    $data['original_autoship_order'] = autoship_get_scheduled_orders( $order_id );
    $data['original_autoship_order'] = autoship_convert_object_to_array( $data['original_autoship_order'] ); }

    // No problems getting the original order
    if ( !is_wp_error( $data['original_autoship_order'] ) ){

      foreach ( $data['new_order_items'] as $item_ids => $item_data) {

        // Item ids are QPilot id - External ID
        $ids = explode( '-', $item_ids );

        // Get the Product Data associated with the item.
        $new_autoship_order_items[] = autoship_create_scheduled_order_item_data (
          $order_id,
          $ids[0],
          $ids[1],
          $item_data['qty'],
          $data['original_autoship_order']['frequencyType'],
          $data['original_autoship_order']['frequency'] );

      }

      /**
      * Allow devs to modify item changes on the fly.
      */
      $new_autoship_order_items = apply_filters(
        'autoship_update_schedule_new_items_changes',
        $new_autoship_order_items,
        $order_id, 'autoship_update_schedule_items' , $data
      );

      // Now add the items via the api
      $result = autoship_create_scheduled_order_items( $order_id, $new_autoship_order_items );

    }

  }

  $scheduled_order_label = autoship_translate_text( 'Scheduled Order' );

  // Check if no change is being made
  if ( empty( $updated_autoship_order_items ) && empty( $new_autoship_order_items ) ){

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

  }

  // Dynamically adjust notice based on what action was being performed.
  $new_messages = '';
  if ( !empty( $updated_autoship_order_items ) && !empty( $new_autoship_order_items ) ){

    $new_messages = array(
      'error'   => __( "A problem was encountered updating the {$scheduled_order_label}'s items.", 'autoship'),
      'success' => __( "The items for {$scheduled_order_label} #%s were successfully updated.", 'autoship'),
    );

  } else if (  !empty( $new_autoship_order_items ) ){

    $new_messages = array(
      'error'   => __( "A problem was encountered Adding the new {$scheduled_order_label}'s items.", 'autoship'),
      'success' => __( "The items for {$scheduled_order_label} #%s were successfully added.", 'autoship'),
    );

  }

  if ( !empty( $new_messages ) )
  add_filter( 'default_autoship_update_schedule_items_action_messages', function( $messages ) { return array_merge( $messages, $new_messages ); }, 11, 1 );

  return $result;

}