/** * If the payment for a renewal order has previously failed and is then paid, we need to make sure the * subscription payment function is called. * * @param $user_id int The id of the user who purchased the subscription * @param $subscription_key string A subscription key of the form created by @see WC_Subscriptions_Manager::get_subscription_key() * @since 1.2 */ public static function process_subscription_payment_on_child_order($order_id, $payment_status = 'completed') { if (self::is_renewal($order_id, 'child')) { $child_order = new WC_Order($order_id); $parent_order = self::get_parent_order($child_order); $subscriptions_in_order = $child_order->get_items(); // Should only be one subscription in the renewal order, but just in case foreach ($subscriptions_in_order as $item) { if (WC_Subscriptions_Order::is_item_a_subscription($parent_order, $item['id'])) { if ('failed' == $payment_status) { // Don't duplicate renewal order remove_action('processed_subscription_payment_failure', __CLASS__ . '::generate_failed_payment_renewal_order', 10, 2); WC_Subscriptions_Manager::process_subscription_payment_failure_on_order($parent_order->id, $item['id']); // But make sure orders are still generated for other payments in the same request add_action('processed_subscription_payment_failure', __CLASS__ . '::generate_failed_payment_renewal_order', 10, 2); } else { // Don't duplicate renewal order remove_action('processed_subscription_payment', __CLASS__ . '::generate_paid_renewal_order', 10, 2); WC_Subscriptions_Manager::process_subscription_payments_on_order($parent_order->id, $item['id']); // But make sure orders are still generated for other payments in the same request add_action('processed_subscription_payment', __CLASS__ . '::generate_paid_renewal_order', 10, 2); // Reactivate the subscription - activate_subscription doesn't operate on child orders $subscription_key = WC_Subscriptions_Manager::get_subscription_key($parent_order->id, $item['id']); WC_Subscriptions_Manager::reactivate_subscription($parent_order->customer_user, $subscription_key); } } } } }