/**
 * A wrapper for @see wcs_get_subscriptions() which accepts simply an order ID
 *
 * @param int|WC_Order $order_id The post_id of a shop_order post or an instance of a WC_Order object
 * @param array $args A set of name value pairs to filter the returned value.
 *		'subscriptions_per_page' The number of subscriptions to return. Default set to -1 to return all.
 *		'offset' An optional number of subscription to displace or pass over. Default 0.
 *		'orderby' The field which the subscriptions should be ordered by. Can be 'start_date', 'trial_end_date', 'end_date', 'status' or 'order_id'. Defaults to 'start_date'.
 *		'order' The order of the values returned. Can be 'ASC' or 'DESC'. Defaults to 'DESC'
 *		'customer_id' The user ID of a customer on the site.
 *		'product_id' The post ID of a WC_Product_Subscription, WC_Product_Variable_Subscription or WC_Product_Subscription_Variation object
 *		'order_id' The post ID of a shop_order post/WC_Order object which was used to create the subscription
 *		'subscription_status' Any valid subscription status. Can be 'any', 'active', 'cancelled', 'suspended', 'expired', 'pending' or 'trash'. Defaults to 'any'.
 *		'order_type' Get subscriptions for the any order type in this array. Can include 'any', 'parent', 'renewal' or 'switch', defaults to parent.
 * @return array Subscription details in post_id => WC_Subscription form.
 * @since  2.0
 */
function wcs_get_subscriptions_for_order($order_id, $args = array())
{
    if (is_object($order_id)) {
        $order_id = $order_id->id;
    }
    $args = wp_parse_args($args, array('order_id' => $order_id, 'subscriptions_per_page' => -1, 'order_type' => array('parent', 'switch')));
    // Accept either an array or string (to make it more convenient for singular types, like 'parent' or 'any')
    if (!is_array($args['order_type'])) {
        $args['order_type'] = array($args['order_type']);
    }
    $subscriptions = array();
    $get_all = in_array('any', $args['order_type']) ? true : false;
    if ($order_id && in_array('parent', $args['order_type']) || $get_all) {
        $subscriptions = wcs_get_subscriptions($args);
    }
    if (wcs_order_contains_resubscribe($order_id) && (in_array('resubscribe', $args['order_type']) || $get_all)) {
        $subscriptions += wcs_get_subscriptions_for_resubscribe_order($order_id);
    }
    if (wcs_order_contains_renewal($order_id) && (in_array('renewal', $args['order_type']) || $get_all)) {
        $subscriptions += wcs_get_subscriptions_for_renewal_order($order_id);
    }
    if (wcs_order_contains_switch($order_id) && (in_array('switch', $args['order_type']) || $get_all)) {
        $subscriptions += wcs_get_subscriptions_for_switch_order($order_id);
    }
    return $subscriptions;
}
 /**
  * trigger function.
  *
  * We need to override WC_Email_New_Order's trigger method because it expects to be run only once
  * per request.
  *
  * @access public
  * @return void
  */
 function trigger($order_id)
 {
     if ($order_id) {
         $this->object = new WC_Order($order_id);
         $order_date_index = array_search('{order_date}', $this->find);
         if (false === $order_date_index) {
             $this->find[] = '{order_date}';
             $this->replace[] = date_i18n(wc_date_format(), strtotime($this->object->order_date));
         } else {
             $this->replace[$order_date_index] = date_i18n(wc_date_format(), strtotime($this->object->order_date));
         }
         $order_number_index = array_search('{order_number}', $this->find);
         if (false === $order_number_index) {
             $this->find[] = '{order_number}';
             $this->replace[] = $this->object->get_order_number();
         } else {
             $this->replace[$order_number_index] = $this->object->get_order_number();
         }
         $this->subscriptions = wcs_get_subscriptions_for_switch_order($this->object);
     }
     if (!$this->is_enabled() || !$this->get_recipient()) {
         return;
     }
     $this->send($this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments());
 }
 /**
  * If switching a subscription using PayPal Standard as the payment method and the customer has entered
  * in a payment method other than PayPal (which would be using Reference Transactions), make sure to update
  * the payment method on the subscription (this is hooked to 'woocommerce_payment_successful_result' to make
  * sure it happens after the payment succeeds).
  *
  * @param array $payment_processing_result The result of the process payment gateway extension request.
  * @param int $order_id The ID of an order potentially recording a switch.
  * @return array
  */
 public static function maybe_set_payment_method($payment_processing_result, $order_id)
 {
     if (wcs_order_contains_switch($order_id)) {
         $order = wc_get_order($order_id);
         foreach (wcs_get_subscriptions_for_switch_order($order_id) as $subscription) {
             if ('paypal' === $subscription->payment_method && $subscription->payment_method !== $order->payment_method && false === wcs_is_paypal_profile_a(wcs_get_paypal_id($subscription->id), 'billing_agreement')) {
                 // Set the new payment method on the subscription
                 $available_gateways = WC()->payment_gateways->get_available_payment_gateways();
                 if (isset($available_gateways[$order->payment_method])) {
                     $subscription->set_payment_method($available_gateways[$order->payment_method]);
                 }
             }
         }
     }
     return $payment_processing_result;
 }
 /**
  * Once payment is processed on a switch from a $0 / period subscription to a non-zero $ / period subscription, if
  * payment was completed with a payment method which supports automatic payments, update the payment on the subscription
  * and the manual renewals flag so that future renewals are processed automatically.
  *
  * @param array $payment_processing_result
  * @param int $order_id
  * @since 2.0.16
  */
 public static function maybe_set_payment_method($payment_processing_result, $order_id)
 {
     // Only update the payment method the order contains a switch, and payment was processed (i.e. a paid date has been set) not just setup for processing, which is the case with PayPal Standard (which is handled by WCS_PayPal_Standard_Switcher)
     if (wcs_order_contains_switch($order_id) && false != get_post_meta($order_id, '_paid_date', true)) {
         $order = wc_get_order($order_id);
         foreach (wcs_get_subscriptions_for_switch_order($order_id) as $subscription) {
             if (false === $subscription->is_manual()) {
                 continue;
             }
             if ($subscription->payment_method !== $order->payment_method) {
                 // Set the new payment method on the subscription
                 $available_gateways = WC()->payment_gateways->get_available_payment_gateways();
                 $payment_method = isset($available_gateways[$order->payment_method]) ? $available_gateways[$order->payment_method] : false;
                 if ($payment_method && $payment_method->supports('subscriptions')) {
                     $subscription->set_payment_method($payment_method);
                     $subscription->update_manual(false);
                 }
             }
         }
     }
     return $payment_processing_result;
 }