/** * Handles the subscription upgrade/downgrade process. * * @since 1.4 */ public static function subscription_switch_handler() { global $woocommerce, $post; // If the current user doesn't own the subscription, remove the query arg from the URL if (isset($_GET['switch-subscription'])) { // Visiting a switch link for someone elses subscription if (!WC_Subscriptions_Manager::user_owns_subscription($_GET['switch-subscription'])) { wp_redirect(remove_query_arg('switch-subscription')); exit; } else { if (isset($_GET['auto-switch'])) { $switch_message = __('You have an active subscription to this product. Choosing a new subscription will replace your existing subscription.', 'woocommerce-subscriptions'); } else { $switch_message = __('Choose a new subscription.', 'woocommerce-subscriptions'); } WC_Subscriptions::add_notice($switch_message, 'notice'); } } elseif ((is_cart() || is_checkout()) && false !== self::cart_contains_subscription_switch()) { WC_Subscriptions::add_notice(__('Once sign up is complete, this will replace your existing subscription.', 'woocommerce-subscriptions'), 'notice'); } elseif (is_product() && ($product = get_product($post))) { // Automatically initiate the switch process for limited variable subscriptions if (($product->is_type(array('variable-subscription', 'subscription_variation')) || 0 !== $product->post->post_parent) && WC_Subscriptions_Product::is_subscription($product->id) && 'yes' === $product->limit_subscriptions) { // Check if the user has an active subscription for this product, and if so, initiate the switch process $subscriptions = WC_Subscriptions_Manager::get_users_subscriptions(); foreach ($subscriptions as $subscription_key => $subscription) { if ($subscription['product_id'] == $product->id && 'active' == $subscription['status']) { wp_redirect(add_query_arg('auto-switch', 'true', self::get_switch_link($subscription_key))); exit; } } } } }
/** * Handles the subscription upgrade/downgrade process. * * @since 1.4 */ public static function subscription_switch_handler() { global $woocommerce, $post; // If the current user doesn't own the subscription, remove the query arg from the URL if (isset($_GET['switch-subscription'])) { if (!WC_Subscriptions_Manager::user_owns_subscription($_GET['switch-subscription'])) { wp_redirect(remove_query_arg('switch-subscription')); exit; } else { $woocommerce->add_message(__('Choose a new subscription.', WC_Subscriptions::$text_domain)); } } elseif ((is_cart() || is_checkout()) && false !== self::cart_contains_subscription_switch()) { $woocommerce->add_message(__('Once sign up is complete, this will replace your existing subscription.', WC_Subscriptions::$text_domain)); } }
/** * If requesting a payment method change, replace the woocommerce_pay_shortcode() with a change payment form. * * @since 1.4 */ public static function maybe_replace_pay_shortcode() { global $woocommerce; if (!self::$is_request_to_change_payment) { return; } ob_clean(); do_action('before_woocommerce_pay'); echo '<div class="woocommerce">'; if (!empty(self::$woocommerce_errors)) { foreach (self::$woocommerce_errors as $error) { WC_Subscriptions::add_notice($error, 'error'); } } if (!empty(self::$woocommerce_messages)) { foreach (self::$woocommerce_messages as $message) { WC_Subscriptions::add_notice($message, 'success'); } } $subscription_key = $_GET['change_payment_method']; $subscription = WC_Subscriptions_Manager::get_subscription($subscription_key); if (wp_verify_nonce($_GET['_wpnonce'], __FILE__) === false) { WC_Subscriptions::add_notice(__('There was an error with your request. Please try again.', 'woocommerce-subscriptions'), 'error'); WC_Subscriptions::print_notices(); } elseif (!WC_Subscriptions_Manager::user_owns_subscription($subscription_key)) { WC_Subscriptions::add_notice(__('That doesn\'t appear to be one of your subscriptions.', 'woocommerce-subscriptions'), 'error'); WC_Subscriptions::print_notices(); } elseif (empty($subscription)) { WC_Subscriptions::add_notice(__('Invalid subscription.', 'woocommerce-subscriptions'), 'error'); WC_Subscriptions::print_notices(); } elseif (!WC_Subscriptions_Manager::can_subscription_be_changed_to('new-payment-method', $subscription_key, get_current_user_id())) { WC_Subscriptions::add_notice(__('The payment method can not be changed for that subscription.', 'woocommerce-subscriptions'), 'error'); WC_Subscriptions::print_notices(); } else { $order = new WC_Order($subscription['order_id']); $order_id = absint($_GET['order_id']); $order_key = isset($_GET['key']) ? $_GET['key'] : $_GET['order']; $product_id = $subscription['product_id']; $next_payment_timestamp = WC_Subscriptions_Order::get_next_payment_timestamp($order, $product_id); if (!empty($next_payment_timestamp)) { $next_payment_string = sprintf(__(' Next payment is due %s.', 'woocommerce-subscriptions'), date_i18n(woocommerce_date_format(), $next_payment_timestamp)); } else { $next_payment_string = ''; } WC_Subscriptions::add_notice(sprintf(__('Choose a new payment method.%s', 'woocommerce-subscriptions'), $next_payment_string), 'notice'); WC_Subscriptions::print_notices(); if ($order->order_key == $order_key) { // Set customer location to order location if ($order->billing_country) { $woocommerce->customer->set_country($order->billing_country); } if ($order->billing_state) { $woocommerce->customer->set_state($order->billing_state); } if ($order->billing_postcode) { $woocommerce->customer->set_postcode($order->billing_postcode); } // Show form WC_Subscriptions_Order::$recurring_only_price_strings = true; woocommerce_get_template('checkout/form-change-payment-method.php', array('order' => $order, 'subscription_key' => $subscription_key), '', plugin_dir_path(WC_Subscriptions::$plugin_file) . 'templates/'); WC_Subscriptions_Order::$recurring_only_price_strings = false; } else { WC_Subscriptions::add_notice(__('Invalid order.', 'woocommerce-subscriptions'), 'error'); WC_Subscriptions::print_notices(); } } }
/** * When a subscription switch is added to the cart, store a record of pertinent meta about the switch. * * @since 1.4 */ public static function set_switch_details_in_cart($cart_item_data, $product_id, $variation_id) { global $woocommerce; if (!isset($_GET['switch-subscription'])) { return $cart_item_data; } $subscription = WC_Subscriptions_Manager::get_subscription($_GET['switch-subscription']); // Requesting a switch for someone elses subscription if (!WC_Subscriptions_Manager::user_owns_subscription($_GET['switch-subscription'])) { WC_Subscriptions::add_notice(__('You can not switch this subscription. It appears you do not own the subscription.', 'woocommerce-subscriptions'), 'error'); $woocommerce->cart->empty_cart(true); wp_redirect(get_permalink($subscription['product_id'])); exit; } // Else it's a valid switch $product = get_product($subscription['product_id']); $child_products = 0 !== $product->post->post_parent ? get_product($product->post->post_parent)->get_children() : array(); if ($product_id != $subscription['product_id'] && !in_array($subscription['product_id'], $child_products)) { return $cart_item_data; } $next_payment_timestamp = WC_Subscriptions_Manager::get_next_payment_date($_GET['switch-subscription'], get_current_user_id(), 'timestamp'); // If there are no more payments due on the subscription, because we're in the last billing period, we need to use the subscription's expiration date, not next payment date if (false == $next_payment_timestamp && WC_Subscriptions_Manager::get_subscriptions_completed_payment_count($_GET['switch-subscription']) >= WC_Subscriptions_Order::get_subscription_length($subscription['order_id'], $subscription['product_id'])) { $next_payment_timestamp = WC_Subscriptions_Manager::get_subscription_expiration_date($_GET['switch-subscription'], get_current_user_id(), 'timestamp'); } $cart_item_data['subscription_switch'] = array('subscription_key' => $_GET['switch-subscription'], 'next_payment_timestamp' => $next_payment_timestamp, 'upgraded_or_downgraded' => ''); return $cart_item_data; }