/** * Allow items on PayPal Standard Subscriptions to be switch when the PayPal account supports Reference Transactions * * Because PayPal Standard does not support recurring amount or date changes, items can not be switched when the subscription is using a * profile ID for PayPal Standard. However, PayPal Reference Transactions do allow these to be updated and because switching uses the checkout * process, we can migrate a subscription from PayPal Standard to Reference Transactions when the customer switches, so we will allow that. * * @since 2.0 */ public static function can_item_be_switched($item_can_be_switch, $item, $subscription) { if (false === $item_can_be_switch && 'paypal' === $subscription->payment_method && WCS_PayPal::are_reference_transactions_enabled()) { $is_billing_agreement = wcs_is_paypal_profile_a(wcs_get_paypal_id($subscription->id), 'billing_agreement'); if ('line_item' == $item['type'] && wcs_is_product_switchable_type($item['product_id'])) { $is_product_switchable = true; } else { $is_product_switchable = false; } if ($subscription->has_status('active') && 0 !== $subscription->get_date('last_payment')) { $is_subscription_switchable = true; } else { $is_subscription_switchable = false; } // If the only reason the subscription isn't switchable is because the PayPal profile ID is not a billing agreement, allow it to be switched if (false === $is_billing_agreement && $is_product_switchable && $is_subscription_switchable) { $item_can_be_switch = true; } } return $item_can_be_switch; }
/** * 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 switch the subscription, then mark it as purchasable. * * @since 1.4.4 * @return bool */ public static function is_purchasable($is_purchasable, $product) { $product_key = !empty($product->variation_id) ? $product->variation_id : $product->id; if (!isset(self::$is_purchasable_cache[$product_key])) { if (false === $is_purchasable && wcs_is_product_switchable_type($product) && WC_Subscriptions_Product::is_subscription($product->id) && 'no' != $product->limit_subscriptions && is_user_logged_in() && wcs_user_has_subscription(0, $product->id, $product->limit_subscriptions)) { // Adding to cart from the product page if (isset($_GET['switch-subscription'])) { $is_purchasable = true; // Validating when restring cart from session } elseif (self::cart_contains_switches()) { $is_purchasable = true; // Restoring cart from session, so need to check the cart in the session (self::cart_contains_subscription_switch() 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['subscription_switch'])) { $is_purchasable = true; break; } } } } self::$is_purchasable_cache[$product_key] = $is_purchasable; } return self::$is_purchasable_cache[$product_key]; }
/** * Check if a given item on a subscription can be switched. * * For an item to be switchable, switching must be enabled, and the item must be for a variable subscription or * part of a grouped product (at the time the check is made, not at the time the subscription was purchased) * * The subscription must also be active or on-hold and use manual renewals or use a payment method which supports cancellation. * * @param array $item An order item on the subscription * @param WC_Subscription $subscription An instance of WC_Subscription * @since 2.0 */ public static function can_item_be_switched($item, $subscription = null) { if ('line_item' == $item['type'] && wcs_is_product_switchable_type($item['product_id'])) { $is_product_switchable = true; } else { $is_product_switchable = false; } if ($subscription->has_status('active') && 0 !== $subscription->get_date('last_payment')) { $is_subscription_switchable = true; } else { $is_subscription_switchable = false; } if ($subscription->payment_method_supports('subscription_amount_changes') && $subscription->payment_method_supports('subscription_date_changes')) { $can_subscription_be_updated = true; } else { $can_subscription_be_updated = false; } if ($is_product_switchable && $is_subscription_switchable && $can_subscription_be_updated) { $item_can_be_switch = true; } else { $item_can_be_switch = false; } return apply_filters('woocommerce_subscriptions_can_item_be_switched', $item_can_be_switch, $item, $subscription); }