• versado

    (@versado)


    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

    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 9 months ago by versado.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter versado

    (@versado)

    “I already have an idea for my project, those modifications aren’t necessary. Thanks for the plugin.”

    Plugin Support lavendervu2510

    (@lavendervu2510)

    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.