/** * Check if a given subscription can be renewed. * * Deprecated because the use of a $subscription_key is deprecated. * * @param string $subscription_key A subscription key of the form created by @see WC_Subscriptions_Manager::get_subscription_key() * @param int $user_id The ID of the user who owns the subscriptions. Although this parameter is optional, if you have the User ID you should pass it to improve performance. * @since 1.2 * @deprecated 2.0 */ public static function can_subscription_be_renewed($subscription_key, $user_id = '') { _deprecated_function(__METHOD__, '2.0', 'wcs_can_user_resubscribe_to( $subscription, $user_id )'); return wcs_can_user_resubscribe_to(wcs_get_subscription_from_key($subscription_key), $user_id); }
/** * If a product is being marked as not purchasable because it is limited and the customer has a subscription, * but the current request is to resubscribe to the subscription, then mark it as purchasable. * * @since 2.0 * @return bool */ public function is_purchasable($is_purchasable, $product) { // If the product is being set as not-purchasable by Subscriptions (due to limiting) if (false === $is_purchasable && false === WC_Subscriptions_Product::is_purchasable($is_purchasable, $product)) { // Validating when restoring cart from session if (false !== wcs_cart_contains_resubscribe()) { $is_purchasable = true; // Restoring cart from session, so need to check the cart in the session (wcs_cart_contains_renewal() only checks the cart) } elseif (isset(WC()->session->cart)) { foreach (WC()->session->cart as $cart_item_key => $cart_item) { if ($product->id == $cart_item['product_id'] && isset($cart_item[$this->cart_item_key])) { $is_purchasable = true; break; } } } elseif (isset($_GET['resubscribe'])) { // Is a request to resubscribe $subscription = wcs_get_subscription(absint($_GET['resubscribe'])); if (false !== $subscription && $subscription->has_product($product->id) && wcs_can_user_resubscribe_to($subscription)) { $is_purchasable = true; } } } return $is_purchasable; }
/** * Retrieve available actions that a user can perform on the subscription * * @since 2.0 */ function wcs_get_all_user_actions_for_subscription($subscription, $user_id) { $actions = array(); if (user_can($user_id, 'edit_shop_subscription_status', $subscription->id)) { $admin_with_suspension_disallowed = current_user_can('manage_woocommerce') && '0' === get_option(WC_Subscriptions_Admin::$option_prefix . '_max_customer_suspensions', '0') ? true : false; $current_status = $subscription->get_status(); if ($subscription->can_be_updated_to('on-hold') && wcs_can_user_put_subscription_on_hold($subscription, $user_id) && !$admin_with_suspension_disallowed) { $actions['suspend'] = array('url' => wcs_get_users_change_status_link($subscription->id, 'on-hold', $current_status), 'name' => __('Suspend', 'woocommerce-subscriptions')); } elseif ($subscription->can_be_updated_to('active') && !$subscription->needs_payment()) { $actions['reactivate'] = array('url' => wcs_get_users_change_status_link($subscription->id, 'active', $current_status), 'name' => __('Reactivate', 'woocommerce-subscriptions')); } if (wcs_can_user_resubscribe_to($subscription, $user_id)) { $actions['resubscribe'] = array('url' => wcs_get_users_resubscribe_link($subscription), 'name' => __('Resubscribe', 'woocommerce-subscriptions')); } // Show button for subscriptions which can be cancelled and which may actually require cancellation (i.e. has a future payment) $next_payment = $subscription->get_time('next_payment'); if ($subscription->can_be_updated_to('cancelled') && !$subscription->is_one_payment() && ($next_payment > 0 || $subscription->has_status('on-hold') && empty($next_payment))) { $actions['cancel'] = array('url' => wcs_get_users_change_status_link($subscription->id, 'cancelled', $current_status), 'name' => _x('Cancel', 'an action on a subscription', 'woocommerce-subscriptions')); } } return apply_filters('wcs_view_subscription_actions', $actions, $subscription); }
/** * Returns a URL including required parameters for an authenticated user to renew a subscription by product ID. * * @param int $product_id The ID of a product post type. * @since 1.2 */ function wcs_get_users_resubscribe_link_for_product($product_id) { $renewal_url = ''; if (is_user_logged_in()) { foreach (wcs_get_users_subscriptions() as $subscription) { foreach ($subscription->get_items() as $line_item) { if (($line_item['product_id'] == $product_id || $line_item['variation_id'] == $product_id) && wcs_can_user_resubscribe_to($subscription)) { $renewal_url = wcs_get_users_resubscribe_link($subscription); break; } } } } return apply_filters('wcs_users_resubscribe_link_for_product', $renewal_url, $product_id); }