Cart / Checkout: Auto-Apply Coupon to Scheduled Order at Checkout

This customization is an example of how merchants can apply an existing Autoship Cloud Coupon to Scheduled Orders at checkout. This includes a conditional, role-based example a merchant may use to filter who gets the coupon or the discount applied. Always test on a staging site before applying custom code to your production / live site.


Example 1:

/**
* The following is an example of how to apply one or more QPilot Coupons
* to Scheduled Orders at Checkout which can be used as an alternative to
* the recurring price and allow for more customized discounts.
*
* NOTE All Coupon Codes attached to Scheduled Orders at checkout must exists
* in QPilot in order to be valid.
*/

/**
 * Auto-Apply a QPilot Coupon to Scheduled Orders created at checkout.
 * NOTE The Coupon Code used below MUST exist in QPilot and be valid for the order.
 *
 * @param array $scheduled_order_data The current QPilot Scheduled Order data.
 * @param int $order_id The WooCommerce Order ID.
 *
 * @return array The QPilot Scheduled Order data with the new coupon.
 */
function xx_auto_apply_autoship_coupon( $scheduled_order_data, $order_id ){

  if ( !is_checkout() )
  return $scheduled_order_data;

  /**
  * Make the Auto Apply Coupon Code filterable
  * @param array|string an Array of coupon codes or string of comma seperated codes.
  * @return array|string filtered Array of coupon codes or string of comma seperated codes.
  */
  $coupon_code = apply_filters('xx_autoship_auto_apply_coupon', '', $scheduled_order_data, $order_id );


  // Apply a pre-made Coupon to every scheduled order.
  // NOTE Coupons codes are in an array of codes
  if ( !empty( $coupon_code ) ){

    if ( isset( $scheduled_order_data['coupons'] ) && !empty( $scheduled_order_data['coupons'] ) ){

      // Get new coupons ( could be a single item or array )
      $new_coupons = is_array( $coupon_code ) ? $coupon_code : explode( ',', $coupon_code );

      // Add Coupons together & re-attach
      $scheduled_order_data['coupons'] = array_merge( $new_coupons, $scheduled_order_data['coupons'] );

    } else {

      // Add New Coupons
      $scheduled_order_data['coupons'] = is_array( $coupon_code ) ? $coupon_code : explode( ',', $coupon_code );

    }

  }

  return $scheduled_order_data;

}
add_filter( 'autoship_create_scheduled_order_data', 'xx_auto_apply_autoship_coupon', 10, 2 );

/**
 * Adds a Qpilot Autoship coupon to all Scheduled Orders at checkout
 *
 * @param array|string $coupon_code The current coupon(s) applied.
 * @param array $scheduled_order_data The current QPilot Scheduled Order data.
 * @param int $order_id The WooCommerce Order ID.
 *
 * @return array The QPilot Scheduled Order data with the new coupon.
 */
function xx_apply_auto_autoship_coupon( $coupon_code, $scheduled_order_data, $order_id ){

  // Add an AutoshipCouponCode coupon to all Scheduled Orders.
  // Coupon codes are a array of codes
  $new_coupons = empty( $coupon_code ) ? array() : $coupon_code;

  // Check for a comma separated string or array
  $new_coupons  = is_array( $new_coupons ) ? $new_coupons : explode( ',', $new_coupons );

  // Add a new code
  $new_coupons[]= 'AutoshipDiscount';

  return $new_coupons;

}
add_filter( 'xx_autoship_auto_apply_coupon', 'xx_apply_auto_autoship_coupon', 9, 3 );

/**
 * Adds a Qpilot Member coupon to Scheduled Orders at checkout when user has a Member Role.
 *
 * @param array|string $coupon_code The current coupon(s) applied.
 * @param array $scheduled_order_data The current QPilot Scheduled Order data.
 * @param int $order_id The WooCommerce Order ID.
 *
 * @return array The QPilot Scheduled Order data with the new coupon.
 */
function xx_apply_member_autoship_coupon( $coupon_code, $scheduled_order_data, $order_id ){

  // Coupon codes are a array of codes
  $new_coupons = empty( $coupon_code ) ? array() : $coupon_code;

  // Check for a comma separated string or array
  $new_coupons  = is_array( $new_coupons ) ? $new_coupons : explode( ',', $new_coupons );

  // Check if current user is a member and if so give them the additional members coupon.
  $user = wp_get_current_user();
  if ( $user->ID && in_array( 'administrator', (array) $user->roles ) ) {
    $new_coupons[]= 'MemberDiscount';
  }

  return $new_coupons;

}
add_filter( 'xx_autoship_auto_apply_coupon', 'xx_apply_member_autoship_coupon', 10, 3 );

Example 2:

Apply an Autoship Cloud Coupon for Scheduled Orders created at Checkout totaling $300 or more:

/** auto apply thank you coupon for Autoship Checkout orders over $300
 * @param array $scheduledOrderData Autoship Schedueld Order data
 * @param int $orderId WC Order Id
 * @return array Scheduled Order data with applied coupon
 *  */ 

function apply_autoship_coupon_order_over_300($scheduledOrderData, $orderId){
  if(!is_checkout() || empty($orderId)) return $scheduledOrderData;
  $order = new\ WC_Order($orderId);
  $orderTotal = (float)$order->get_total();
  $coupon_code = 'AutoshipThankYou'

  if($orderTotal >= 300 && !empty($coupon_code)){
    if ( isset( $scheduled_order_data['coupons'] ) && !empty( $scheduled_order_data['coupons'] ) ){

      // Get new coupons ( could be a single item or array )
      $new_coupons = is_array( $coupon_code ) ? $coupon_code : explode( ',', $coupon_code );

      // Add Coupons together & re-attach
      $scheduled_order_data['coupons'] = array_merge( $new_coupons, $scheduled_order_data['coupons'] );

    } else {

      // Add New Coupons
      $scheduled_order_data['coupons'] = is_array( $coupon_code ) ? $coupon_code : explode( ',', $coupon_code );

    }
  }
  return $scheduledOrderData;
}
add_filter('autoship_create_scheduled_order_data', 'apply_autoship_coupon_order_over_300', 10, 2);

checkout coupon example1 checkout coupon example2

Still need help? Contact Us Contact Us