autoship_qpilot_orders_update_via_rest_apply_coupons (function)

(src/coupons.php) Hooks into the Order Save and applies QPilot virtual coupons. Takes whether tax is applied and the order as parameters.

 
/**
 * Hook into the Order Save and Applies any QPilot Virtual Coupons
 * @param bool $and_taxes True if Taxes should be calculated.
 * @param WC_Abstract_Order $order The current order.
 */
 function autoship_qpilot_orders_update_via_rest_apply_coupons( $and_taxes, $order ){

  // Only continue if the order is via the rest api and if it's a
  if ( !( $order instanceof WC_Order ) || ( 'rest-api' != $order->get_created_via() ) )
 	return;

  // We Use the Processing ID to ensure even though the order came through rest-api it's
  // a QPilot generated order.
  $id = autoship_get_scheduled_order_processing_id( $order );
  if ( apply_filters( 'autoship_qpilot_orders_update_via_rest_apply_coupons', empty( $id ), $id, $order ) )
 	return;

  // Get the virtual coupons for the order and if they exist apply them.
  $coupons = autoship_get_order_associated_virtual_wc_coupons( $order );
  if ( !empty( $coupons ) ){

    /**
    * Iterate through associated coupons and apply them.
    * NOTE No need to check if they already exist since apply_coupon
    * does that and ignores if they do.
    */
    foreach ($coupons as $code => $coupon){
      $order->apply_coupon($coupon);
    }

  }

 }

Still need help? Contact Us Contact Us