/** * When a subscription is added to the cart, remove other products/subscriptions to * work with PayPal Standard, which only accept one subscription per checkout. * * If multiple purchase flag is set, allow them to be added at the same time. * * @since 1.0 */ public static function maybe_empty_cart($valid, $product_id, $quantity, $variation_id = '') { $is_subscription = WC_Subscriptions_Product::is_subscription($product_id); $cart_contains_subscription = WC_Subscriptions_Cart::cart_contains_subscription(); $multiple_subscriptions_possible = WC_Subscriptions_Payment_Gateways::one_gateway_supports('multiple_subscriptions'); $manual_renewals_enabled = 'yes' == get_option(WC_Subscriptions_Admin::$option_prefix . '_accept_manual_renewals', 'no') ? true : false; $canonical_product_id = !empty($variation_id) ? $variation_id : $product_id; if ($is_subscription && 'yes' != get_option(WC_Subscriptions_Admin::$option_prefix . '_multiple_purchase', 'no')) { WC()->cart->empty_cart(); } elseif ($is_subscription && wcs_cart_contains_renewal() && !$multiple_subscriptions_possible && !$manual_renewals_enabled) { self::remove_subscriptions_from_cart(); self::add_notice(__('A subscription renewal has been removed from your cart. Multiple subscriptions can not be purchased at the same time.', 'woocommerce-subscriptions'), 'notice'); } elseif ($is_subscription && $cart_contains_subscription && !$multiple_subscriptions_possible && !$manual_renewals_enabled && !WC_Subscriptions_Cart::cart_contains_product($canonical_product_id)) { self::remove_subscriptions_from_cart(); self::add_notice(__('A subscription has been removed from your cart. Due to payment gateway restrictions, different subscription products can not be purchased at the same time.', 'woocommerce-subscriptions'), 'notice'); } elseif ($cart_contains_subscription && 'yes' != get_option(WC_Subscriptions_Admin::$option_prefix . '_multiple_purchase', 'no')) { self::remove_subscriptions_from_cart(); self::add_notice(__('A subscription has been removed from your cart. Products and subscriptions can not be purchased at the same time.', 'woocommerce-subscriptions'), 'notice'); // Redirect to cart page to remove subscription & notify shopper add_filter('add_to_cart_fragments', __CLASS__ . '::redirect_ajax_add_to_cart'); } return $valid; }