/** * A wrapper for @see wcs_get_subscriptions() which accepts simply an order ID * * @param int|WC_Order $order_id The post_id of a shop_order post or an instance of a WC_Order object * @param array $args A set of name value pairs to filter the returned value. * 'subscriptions_per_page' The number of subscriptions to return. Default set to -1 to return all. * 'offset' An optional number of subscription to displace or pass over. Default 0. * 'orderby' The field which the subscriptions should be ordered by. Can be 'start_date', 'trial_end_date', 'end_date', 'status' or 'order_id'. Defaults to 'start_date'. * 'order' The order of the values returned. Can be 'ASC' or 'DESC'. Defaults to 'DESC' * 'customer_id' The user ID of a customer on the site. * 'product_id' The post ID of a WC_Product_Subscription, WC_Product_Variable_Subscription or WC_Product_Subscription_Variation object * 'order_id' The post ID of a shop_order post/WC_Order object which was used to create the subscription * 'subscription_status' Any valid subscription status. Can be 'any', 'active', 'cancelled', 'suspended', 'expired', 'pending' or 'trash'. Defaults to 'any'. * 'order_type' Get subscriptions for the any order type in this array. Can include 'any', 'parent', 'renewal' or 'switch', defaults to parent. * @return array Subscription details in post_id => WC_Subscription form. * @since 2.0 */ function wcs_get_subscriptions_for_order($order_id, $args = array()) { if (is_object($order_id)) { $order_id = $order_id->id; } $args = wp_parse_args($args, array('order_id' => $order_id, 'subscriptions_per_page' => -1, 'order_type' => array('parent', 'switch'))); // Accept either an array or string (to make it more convenient for singular types, like 'parent' or 'any') if (!is_array($args['order_type'])) { $args['order_type'] = array($args['order_type']); } $subscriptions = array(); $get_all = in_array('any', $args['order_type']) ? true : false; if ($order_id && in_array('parent', $args['order_type']) || $get_all) { $subscriptions = wcs_get_subscriptions($args); } if (wcs_order_contains_resubscribe($order_id) && (in_array('resubscribe', $args['order_type']) || $get_all)) { $subscriptions += wcs_get_subscriptions_for_resubscribe_order($order_id); } if (wcs_order_contains_renewal($order_id) && (in_array('renewal', $args['order_type']) || $get_all)) { $subscriptions += wcs_get_subscriptions_for_renewal_order($order_id); } if (wcs_order_contains_switch($order_id) && (in_array('switch', $args['order_type']) || $get_all)) { $subscriptions += wcs_get_subscriptions_for_switch_order($order_id); } return $subscriptions; }
/** * Displays the renewal orders in the Related Orders meta box. * * @param object $post A WordPress post * @since 2.0 */ public static function output_rows($post) { $subscriptions = array(); $orders = array(); // On the subscription page, just show related orders if (wcs_is_subscription($post->ID)) { $subscriptions[] = wcs_get_subscription($post->ID); } elseif (wcs_order_contains_subscription($post->ID, array('parent', 'renewal'))) { $subscriptions = wcs_get_subscriptions_for_order($post->ID, array('order_type' => array('parent', 'renewal'))); } // First, display all the subscriptions foreach ($subscriptions as $subscription) { $subscription->relationship = __('Subscription', 'woocommerce-subscriptions'); $orders[] = $subscription; } //Resubscribed $initial_subscriptions = array(); if (wcs_is_subscription($post->ID)) { $initial_subscriptions = wcs_get_subscriptions_for_resubscribe_order($post->ID); $resubscribed_subscriptions = get_posts(array('meta_key' => '_subscription_resubscribe', 'meta_value' => $post->ID, 'post_type' => 'shop_subscription', 'post_status' => 'any', 'posts_per_page' => -1)); foreach ($resubscribed_subscriptions as $subscription) { $subscription = wcs_get_subscription($subscription); $subscription->relationship = _x('Resubscribed Subscription', 'relation to order', 'woocommerce-subscriptions'); $orders[] = $subscription; } } else { if (wcs_order_contains_subscription($post->ID, array('resubscribe'))) { $initial_subscriptions = wcs_get_subscriptions_for_order($post->ID, array('order_type' => array('resubscribe'))); } } foreach ($initial_subscriptions as $subscription) { $subscription->relationship = _x('Initial Subscription', 'relation to order', 'woocommerce-subscriptions'); $orders[] = $subscription; } // Now, if we're on a single subscription or renewal order's page, display the parent orders if (1 == count($subscriptions)) { foreach ($subscriptions as $subscription) { if (false !== $subscription->order) { $subscription->order->relationship = _x('Parent Order', 'relation to order', 'woocommerce-subscriptions'); $orders[] = $subscription->order; } } } // Finally, display the renewal orders foreach ($subscriptions as $subscription) { foreach ($subscription->get_related_orders('all', 'renewal') as $order) { $order->relationship = _x('Renewal Order', 'relation to order', 'woocommerce-subscriptions'); $orders[] = $order; } } $orders = apply_filters('woocommerce_subscriptions_admin_related_orders_to_display', $orders, $subscriptions, $post); foreach ($orders as $order) { if ($order->id == $post->ID) { continue; } include 'views/html-related-orders-row.php'; } }
/** * 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; } } }