/**
  * If the subscription has expired since upgrading and the end date is not the original expiration date,
  * we need to unexpire it, which in the case of a previously active subscription means activate it, and
  * in any other case, leave it as on-hold (a cancelled subscription wouldn't have been expired, so the
  * status must be on-hold or active).
  *
  * @param  WC_Subscription $subscription data about the subscription
  * @return bool true if the trial date was repaired, otherwise false
  */
 protected static function maybe_repair_status($subscription, $former_order_item_meta, $dates_to_update)
 {
     if ($subscription->has_status('expired') && 'expired' != $former_order_item_meta['_wcs_migrated_subscription_status'][0] && isset($dates_to_update['end'])) {
         try {
             // we need to bypass the update_status() method here because normally an expired subscription can't have it's status changed, we also don't want normal status change hooks to be fired
             wp_update_post(array('ID' => $subscription->id, 'post_status' => 'wc-on-hold'));
             // if the payment method doesn't support date changes, we still want to reactivate the subscription but we also need to process a special failed payment at the next renewal to fix up the payment method so we'll set a special flag in post meta to handle that
             if (!$subscription->payment_method_supports('subscription_date_changes') && $subscription->get_total() > 0) {
                 update_post_meta($subscription->id, '_wcs_repaired_2_0_2_needs_failed_payment', 'true');
                 WCS_Upgrade_Logger::add(sprintf('For subscription %d: payment method does not support "subscription_date_changes" and total > 0, setting "_wcs_repaired_2_0_2_needs_failed_payment" post meta flag.', $subscription->id));
             }
             if ('active' == $former_order_item_meta['_wcs_migrated_subscription_status'][0] && $subscription->can_be_updated_to('active')) {
                 $subscription->update_status('active');
             }
             WCS_Upgrade_Logger::add(sprintf('For subscription %d: repaired status. Status was "expired", it is now "%s".', $subscription->id, $subscription->get_status()));
             $repair_status = true;
         } catch (Exception $e) {
             WCS_Upgrade_Logger::add(sprintf('!!! For subscription %d: unable to repair status, exception "%s"', $subscription->id, $e->getMessage()));
             $repair_status = false;
         }
     } else {
         WCS_Upgrade_Logger::add(sprintf('For subscription %d: no need to repair status, current status: %s; former status: %s.', $subscription->id, $subscription->get_status(), $former_order_item_meta['_wcs_migrated_subscription_status'][0]));
         $repair_status = false;
     }
     return $repair_status;
 }
Ejemplo n.º 2
0
 /**
  * Function to manage payment method for renewal orders based on availability of store credit (WCS 2.0+)
  *
  * @param WC_Subscription $subscription
  * @return WC_Subscription $subscription
  */
 public function sc_wcs_modify_subscription($subscription = null)
 {
     if (!empty($subscription) && $subscription instanceof WC_Subscription) {
         $pay_from_credit_of_original_order = get_option('pay_from_smart_coupon_of_original_order', 'yes');
         if ($pay_from_credit_of_original_order != 'yes') {
             return $subscription;
         }
         $original_order_id = !empty($subscription->order->id) ? $subscription->order->id : 0;
         if (empty($original_order_id)) {
             return $subscription;
         }
         $renewal_total = $subscription->get_total();
         $original_order = $this->get_order($original_order_id);
         $coupon_used_in_original_order = $original_order->get_used_coupons();
         if (sizeof($coupon_used_in_original_order) > 0) {
             foreach ($coupon_used_in_original_order as $coupon_code) {
                 $coupon = new WC_Coupon($coupon_code);
                 if (!empty($coupon->discount_type) && $coupon->discount_type == 'smart_coupon' && !empty($coupon->amount)) {
                     if ($coupon->amount >= $renewal_total) {
                         $subscription->set_payment_method('');
                     } else {
                         $payment_gateways = $this->global_wc()->payment_gateways->get_available_payment_gateways();
                         if (!empty($payment_gateways[$original_order->payment_method])) {
                             $payment_method = $payment_gateways[$original_order->payment_method];
                             $subscription->set_payment_method($payment_method);
                         }
                     }
                 }
             }
         }
     }
     return $subscription;
 }