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