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

This code is an example of how merchants can apply an existing Autoship Cloud Coupon to Scheduled Orders at checkout automatically. 

Includes a conditional, role-based example a merchant may use to filter which customer a coupon can be applied. 

Important! Always test code on a staging site before applying custom code to your production / live site.

Filters Used:


Step 1: Add a Function to Auto-Apply Coupons

A developer will first create a new filter: ' xx_autoship_auto_apply_coupon' to hook into with the examples following. 

/**
* 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 separated codes.
  * @return array|string filtered Array of coupon codes or string of comma separated 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 );

Example 1: Add Coupon to All Checkout Orders

In this example, a developer hooks into the ' xx_autoship_auto_apply_coupon' filter they created (see above), and applies the 'AutoshipDiscount' coupon they created in QPilot to all Scheduled Orders created at checkout.

	/**
 * 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 );

Example 2: Add Coupon Based on User Role

In this example, a developer hooks into the ' xx_autoship_auto_apply_coupon' filter they created (see above), and applies the 'MemberDiscount' coupon they created in QPilot to all Scheduled Orders created at checkout by Administrators.

/**
 * 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 3: Add Coupon Based on Order Total

In this example, a developer hooks into the ' xx_autoship_auto_apply_coupon' filter they created (see above), and applies the 'AutoshipThankYou' coupon they created in QPilot to all Scheduled Orders created at checkout when the order total exceeds $300.

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

function apply_autoship_coupon_order_over_300($coupon_code, $scheduledOrderData, $orderId){
	$order = wc_get_order($orderId);
	$orderTotal = $order->get_total();
	// Add an AutoshipCouponCode coupon to all Scheduled Orders.
  	// Coupon codes are a array of codes
  	$new_coupons = empty( $coupon_code ) ? array() : $coupon_code;
  
	if($orderTotal >= 300 ){
	  $new_coupons[] = 'AutoshipThankYou';
	  }
	  
  return $new_coupons;
} 
  add_filter('xx_autoship_auto_apply_coupon', 'apply_autoship_coupon_order_over_300', 10, 3);