Woocommerce: Enable only one purchase per subscription in all store

Is there a way to block logged in user from buy any product after the first purchase of any product?

I want to limit each subscription to buy only one product and then no more shops on any product, only with new subscription.

Read More

I found a filter that do almost what i need but limit only one per product,

add_filter('woocommerce_add_to_cart_validation','rei_woocommerce_add_to_cart_validation',20, 2);
function rei_woocommerce_add_to_cart_validation($valid, $product_id){
    $current_user = wp_get_current_user();
    if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id)) {
        wc_add_notice( __( 'Purchased', 'woocommerce' ), 'error' );
        $valid = false;
    }
    return $valid;
}

but i need to lock every other product after first purchase of a subscription.

Maybe the best way would be when user click to pay for the product at check out page (at this time he need to be logged in) and check if he already purchased any product before.. if yes, he cant pay, only with new subscription…

im ugly on coding.. could somebody help me with this???

Related posts

3 comments

  1. Here is a solution for one subscription per cart, or membership if you are using subscriptions as product. Wish I knew who to give credit to but I have had this one in my magic kit for a while.

    /**
     * c.)
     * @return (bool)
     */
    add_filter( 'woocommerce_add_to_cart_validation','wpso35381172_validate_block_one_sub', 10, 2 );
    function wpso35381172_validate_block_one_sub( $valid, $product_id ) {
    // Get the current product
    $current_product = wc_get_product( $product_id );
    // See if the current product user's are viewing is a subscription product
    if ( $current_product instanceof WC_Product_Subscription || 
         $current_product instanceof WC_Product_Variable_Subscription ) {
        foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
            // Loop through all products in the cart
            $_product = $values['data'];
            // Check if current item is a subscription type
            if( $_product instanceof WC_Product_Subscription || 
                $_product instanceof WC_Product_Subscription_Variation ) {
                // Display a notice and cancel the addition of item to cart
     wc_add_notice( "Only one Subscription or Membership plan can be purchased." );
                    return false;
                }
            }
        }
        return $valid;
    }
    

    This works on WC 3.0+ and uses instances of the WCS_ class.

  2. For those who are looking for the same answer, here is what i did:

    // Get all customer orders
    $customer_orders = get_posts( array(
        'numberposts' => -1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => wc_get_order_types(),
        'post_status' => array( 'wc-pending', 'wc-processing', 'wc-on-hold', 'wc-completed' ),
    
    ) );
    
    // Order count
    $order_count = 1;
    
    if ( count( $customer_orders ) >= $order_count ) {
        add_filter( 'woocommerce_is_purchasable', false );
    }
    
  3. I’m reading OP’s requirement slightly different. That is, that each user can only have one subscription at any one time. For that, this is my solution:

    add_filter( 'woocommerce_add_to_cart_validation','so_validate_block_one_sub', 10, 2 );
    function so_validate_block_one_sub( $valid, $product_id ) {
        $has_sub = wcs_user_has_subscription( '', '', 'active' );
        if ( $has_sub ) {
            wc_add_notice( __("You can not have more than one subscription.", "so-additions") );
    
            return false;
        }
    

    }

Comments are closed.