/** * Checks if the current request is by a user to resubcribe to a subscription, and if it is setup a * subscription resubcribe process via the cart for the product/variation/s that are being renewed. * * @since 2.0 */ public function maybe_setup_cart() { global $wp; if (isset($_GET['resubscribe']) && isset($_GET['_wpnonce'])) { $subscription = wcs_get_subscription($_GET['resubscribe']); $redirect_to = get_permalink(wc_get_page_id('myaccount')); if (wp_verify_nonce($_GET['_wpnonce'], $subscription->id) === false) { wc_add_notice(__('There was an error with your request to resubscribe. Please try again.', 'woocommerce-subscriptions'), 'error'); } elseif (empty($subscription)) { wc_add_notice(__('That subscription does not exist. Has it been deleted?', 'woocommerce-subscriptions'), 'error'); } elseif (!current_user_can('subscribe_again', $subscription->id)) { wc_add_notice(__('That doesn\'t appear to be one of your subscriptions.', 'woocommerce-subscriptions'), 'error'); } elseif (!wcs_can_user_resubscribe_to($subscription)) { wc_add_notice(__('You can not resubscribe to that subscription. Please contact us if you need assistance.', 'woocommerce-subscriptions'), 'error'); } else { $this->setup_cart($subscription, array('subscription_id' => $subscription->id)); if (WC()->cart->get_cart_contents_count() != 0) { wc_add_notice(__('Complete checkout to resubscribe.', 'woocommerce-subscriptions'), 'success'); } $redirect_to = WC()->cart->get_checkout_url(); } wp_safe_redirect($redirect_to); exit; } elseif (isset($_GET['pay_for_order']) && isset($_GET['key']) && isset($wp->query_vars['order-pay'])) { $order_id = isset($wp->query_vars['order-pay']) ? $wp->query_vars['order-pay'] : absint($_GET['order_id']); $order = wc_get_order($wp->query_vars['order-pay']); $order_key = $_GET['key']; if ($order->order_key == $order_key && $order->has_status(array('pending', 'failed')) && wcs_order_contains_resubscribe($order)) { wc_add_notice(__('Complete checkout to resubscribe.', 'woocommerce-subscriptions'), 'success'); $subscriptions = wcs_get_subscriptions_for_resubscribe_order($order); foreach ($subscriptions as $subscription) { $this->setup_cart($subscription, array('subscription_id' => $subscription->id)); } $redirect_to = WC()->cart->get_checkout_url(); wp_safe_redirect($redirect_to); exit; } } }
/** * Check if a given order is a subscription renewal order and optionally, if it is a renewal order of a certain role. * * @param WC_Order|int $order The WC_Order object or ID of a WC_Order order. * @param array $args (optional) An array of name => value flags: * 'order_role' string (optional) A specific role to check the order against. Either 'parent' or 'child'. * 'via_checkout' Indicates whether to check if the renewal order was via the cart/checkout process. * @since 1.2 */ public static function is_renewal($order, $args = array()) { $args = wp_parse_args($args, array('order_role' => '', 'via_checkout' => false)); $is_resubscribe_order = wcs_order_contains_resubscribe($order); $is_renewal_order = wcs_order_contains_renewal($order); if (empty($args['new_order_role'])) { _deprecated_function(__METHOD__, '2.0', 'wcs_order_contains_resubscribe( $order ) and wcs_order_contains_renewal( $order )'); return $is_resubscribe_order || $is_renewal_order; } elseif ('parent' == $args['new_order_role']) { _deprecated_function(__METHOD__, '2.0', 'wcs_order_contains_resubscribe( $order )'); return $is_resubscribe_order; } else { _deprecated_function(__METHOD__, '2.0', 'wcs_order_contains_renewal( $order )'); return $is_renewal_order; } }
/** * Checks an order to see if it contains a subscription. * * @param mixed $order A WC_Order object or the ID of the order which the subscription was purchased in. * @param array|string $order_type Can include 'parent', 'renewal', 'resubscribe' and/or 'switch'. Defaults to 'parent'. * @return bool True if the order contains a subscription that belongs to any of the given order types, otherwise false. * @since 2.0 */ function wcs_order_contains_subscription($order, $order_type = array('parent', 'resubscribe', 'switch')) { // Accept either an array or string (to make it more convenient for singular types, like 'parent' or 'any') if (!is_array($order_type)) { $order_type = array($order_type); } if (!is_object($order)) { $order = new WC_Order($order); } $contains_subscription = false; $get_all = in_array('any', $order_type) ? true : false; if ((in_array('parent', $order_type) || $get_all) && count(wcs_get_subscriptions_for_order($order->id, array('order_type' => 'parent'))) > 0) { $contains_subscription = true; } elseif ((in_array('renewal', $order_type) || $get_all) && wcs_order_contains_renewal($order)) { $contains_subscription = true; } elseif ((in_array('resubscribe', $order_type) || $get_all) && wcs_order_contains_resubscribe($order)) { $contains_subscription = true; } elseif ((in_array('switch', $order_type) || $get_all) && wcs_order_contains_switch($order)) { $contains_subscription = true; } return $contains_subscription; }