autoship_qpilot_orders_update_via_rest_convert_fees_to_coupons (function)

( src/coupons.php) Preprocesses a QPilot Order to remove fee lines that will be added as a virtual coupon so taxes and totals are correctly applied. Takes the $original_request and the (current) $request as its parameters. 

/**
 * Runs QPilot Orders through a pre-processor to remove fee lines
 * which will later be added as a virtual coupon so that taxes and totals
 * are calculated correctly.
 *
 * @param  WP_REST_Request $request The current Request data.
 * @param  WP_REST_Request $original_request The original Request data.
 * @return WP_Error|WP_REST_Response
 */
function autoship_qpilot_orders_update_via_rest_convert_fees_to_coupons( $request, $original_request ) {

  // allow devs to flip the path from the new pre-process route to the legacy direct
  if ( apply_filters('autoship_qpilot_orders_via_rest_enable_fee_lines', autoship_rest_order_fee_lines_enabled() , $request ) )
  return $request;

  // Check for Coupon Metadata
  $coupons = array();
  if ( isset( $request['meta_data'] ) && !empty( $request['meta_data'] ) ){
    foreach ($request['meta_data'] as $key => $meta ) {
      if ( '_qpilot_coupon_data' == $meta['key'] ){
        $coupons = $meta['value'];
        break;
      }
    }
  }

  $new_coupons = $new_codes = array();
  // Loop through coupon objects and generate virtual coupon.
  foreach ( $coupons as $key => $coupon ) {

    $type = autoship_get_coupon_type_by_code( $coupon['discountType'] );

    if ( !empty( $type ) ){
      $new_coupons['Coupon: ' . $coupon['code']] = autoship_generate_virtual_wc_coupon( array(
          'code'          => 'Coupon: ' . $coupon['code'],
          'description'   => sprintf( __( 'QPilot Dynamic Coupon generated from Coupon ID #%d.', 'autoship' ), $coupon['id'] ),
          'discount_type' => $type,
          'amount'        => $coupon['amount']
      ));
    }
  }

  // Allow devs to hook in so that these could be converted to real coupons or modify on the fly
  $new_coupons = apply_filters( 'autoship_rest_generated_virtual_wc_coupons', $new_coupons, $coupons, $request );

  // Grab the request metadata
  $meta_data = $request['meta_data'];

  // Add the virtual coupons to the request for later use.
  if ( !empty( $new_coupons ) ){
    $meta_data[] = array(
      'key'   => '_qpilot_dynamic_coupons',
      'value' => $new_coupons
    );
  }

  // Check for Fee lines and if a coupon has been generated for it remove the line.
  if ( isset( $request['fee_lines'] ) && !empty( $request['fee_lines'] ) ){

    /**
    * Loop through the fee lines and remove any we have coupons for.
    * Any that get removed add to the legacy fee metadata.
    */
    $fee_lines = $request['fee_lines'];
    $legacy_fee_lines = array();

    foreach ($fee_lines as $key => $fee) {
      if ( isset( $new_coupons[$fee['name']] ) ){
        $legacy_fee_lines[] = $fee;
        unset( $fee_lines[$key] );
      }
    }

  }

  // Set the new fee line values.
  if ( empty( $fee_lines ) ){
    $request->offsetUnset( 'fee_lines' );
  } else {
    $request->set_param(
      'fee_lines',
      $fee_lines
    );
  }

  // Attach any legacy fee lines
  if ( !empty( $legacy_fee_lines ) ){
    $meta_data[] = array(
      'key'   => '_qpilot_legacy_fee_lines',
      'value' => $legacy_fee_lines
    );
  }

  // Re-attach the metadata
  $request->set_param(
    'meta_data',
    $meta_data
  );

  return $request;

}
add_filter( 'autoship_qpilot_orders_via_rest', 'autoship_qpilot_orders_update_via_rest_convert_fees_to_coupons', 10, 2 );