Title: Active subscription
Last modified: March 4, 2024

---

# Active subscription

 *  [versado](https://wordpress.org/support/users/versado/)
 * (@versado)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/active-subscription/)
 * Good morning, I want to prevent the purchase of the product while it is active
   if the current user has an active subscription for the product they are trying
   to add to the cart. If so, an error message is displayed and the product is prevented
   from being added to the cart. Alternatively, if it is possible to proceed with
   the purchase, I want this purchase to be for renewing the subscription. I’ve 
   been trying to solve this problem with this code, but I don’t know much about
   the topic
 *     ```wp-block-code
       add_filter('woocommerce_is_purchasable', 'restringir_compra_y_carrito_si_suscripcion_activa', 10, 2);
   
       function restringir_compra_y_carrito_si_suscripcion_activa($purchasable, $product) {
           // Obtiene el ID del usuario actual
           $user_id = get_current_user_id();
   
           // Obtiene todas las suscripciones del usuario actual
           $subscriptions = SUBRE_PRODUCT_SUBSCRIPTION_FOR_WOO_Admin_Settings::get_subscriptions_for_user($user_id);
   
           // Recorre todas las suscripciones del usuario
           foreach ($subscriptions as $subscription) {
               // Verifica si el usuario tiene una suscripción activa para el producto
               if ($subscription->has_product($product->get_id()) && $subscription->is_active()) {
                   // Si es así, el producto no puede ser comprado ni añadido al carrito
                   wc_add_notice(__('Ya tienes una suscripción activa para este producto.', 'woocommerce'), 'error');
                   return false;
               }
           }
   
           // Si ninguna de las suscripciones del usuario coincide con el producto,
           // devuelve el valor original de $purchasable
           return $purchasable;
       }
       ```
   
    -  This topic was modified 2 years, 3 months ago by [versado](https://wordpress.org/support/users/versado/).

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [versado](https://wordpress.org/support/users/versado/)
 * (@versado)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/active-subscription/#post-17477347)
 * “I already have an idea for my project, those modifications aren’t necessary.
   Thanks for the plugin.”
 *  [lavendervu2510](https://wordpress.org/support/users/lavendervu2510/)
 * (@lavendervu2510)
 * [2 years, 3 months ago](https://wordpress.org/support/topic/active-subscription/#post-17477950)
 * Hi,
 * Thank you for your confirmation, if you need any further assistance, don’t hesitate
   to contact us.
 * Best regards.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Active subscription’ is closed to new replies.

 * ![](https://ps.w.org/subre-product-subscription-for-woo/assets/icon-256x256.gif?
   rev=3081749)
 * [SUBRE – Product Subscription for WooCommerce - Recurring Payments](https://wordpress.org/plugins/subre-product-subscription-for-woo/)
 * [Support Threads](https://wordpress.org/support/plugin/subre-product-subscription-for-woo/)
 * [Active Topics](https://wordpress.org/support/plugin/subre-product-subscription-for-woo/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/subre-product-subscription-for-woo/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/subre-product-subscription-for-woo/reviews/)

 * 2 replies
 * 2 participants
 * Last reply from: [lavendervu2510](https://wordpress.org/support/users/lavendervu2510/)
 * Last activity: [2 years, 3 months ago](https://wordpress.org/support/topic/active-subscription/#post-17477950)
 * Status: not resolved