WooCommerce Subscriptions Limit Total Quantity of Subscriptions or Payment Plans in Cart

We use WooCommerce Subscriptions to setup payment plans for our products.  Deposits was our first choice but it only sent “Invoices” for future payments and did not auto charge the credit card used at initial purchase.  Not ok as our products are very expensive and people tended to ignore those invoices.

We also need to limit these purchase to one per customer.  Easier said than done.  I found a WooCommerce extension that would do the trick but we already pay so much a year for extensions we need I try to avoid that for simple functionality.

Limit Subscription

This won’t work for my needs.  The option on a subscription product only limits users from having multiple subscriptions to that particular product.  We only want one subscript for ANY product.

Solution Limits

My solution works for one shopping cart session.  I’m working on preventing others which shouldn’t be too hard by just checking the database at checkout time for active subscriptions to that user.

The Hook & Function

I went with woocommerce_add_to_cart_validation after going through quite a few other hooks and filters.  The hardest thing about finding a solution is finding what all hooks and filters are available.  Takes some serious google-fu to find the right one quickly.

This filter allows you to run a function before an item is validated and sent to the cart.  We tell our add_filter that we need two of the parameters so we can get the product id as well as the validity of the admission.  This is done with the 4th parameter we set to “2.”

Detect Variations Too

If you are using variable Subscriptions you will need to plan ahead that some items in the cart may be variations.  These have a separate class.  We must first detect if the product being added is an instance of WC_Product_Subscription which is a “Simple Subscription” in WooCommerce.  Or is an instance of WC_Product_Variable_Subscription which is like it says a version of a subscription product with variations.

The odd part that caught me off guard is when we are checking items already in the cart.  We check for WC_Product_Subscription like normal but once in the cart the product is of the WC_Product_Subscription_Variation class meaning it’s just a variation of a product and not the full product variable class.


10 thoughts on “WooCommerce Subscriptions Limit Total Quantity of Subscriptions or Payment Plans in Cart”

  1. thanks for posting this. i’m not totally understanding how to write this type of code (hook into other plugin hooks to perform a different task / amend the default task.

    i’m working on a project using wooComm Product Vendors, wooComm Membership (possibly, or Ultimate Members – very polished plugin), wooComm Subscriptions with the Listify theme.

    I’ve definitely got some roadblocks to overcome. I’ve been testing different multistore plugins but decided to got all wooComm thinking i’d have less of a capatibility issue. There plugins though lack some basic details which i frustrating.

    thanks again for posting code/ solution

  2. Hi Chris,

    Nice snippet. Trying to do something similar, but interestingly what you’d noted as “I’m working on preventing others which shouldn’t be too hard by just checking the database at checkout time for active subscriptions to that user.”

    Wondered if you got anywhere in developing this addition? And if you did, would you be comfortable with sharing the solution?

    Thanks in advance,

    1. Hi matt, unfortunately I never got around to that and went off to a new job.

      Off the top of my head I’d say hook in to checkout field validation for email, find a user with that email and look through orders if they have any. Check for any subscriptions and then check for a post of type, I believe ‘subscription’ and I also believe woo sets the post_status to ‘active’ for active subscriptions.

  3. Hi Chris! Thank you for your awesome snippet. It just would lack a way for replace the product subscription when the customers select other product.

    1. Yea, if that’s what you want to do then you can replace it. My intention was to send a warning saying they already have one subscription and they can remove that from cart if they want a different one. If you want to replace I have another snippet I wrote that talks about upgrading items and you could pull some of the replacing code from that here: https://whoischris.com/snippet-add-upgrade-button-item-checkout-woocommerce-select-upgrade-product-per-product/

  4. Hi Chris, Do you have any idea on how to limit daily download limit of digital downloable products for woocommerce. If a subscriber/member of site downloaded 10 files a day, a message should tell him on page that “you have reached your daily download limit” and show all list of files downloaded on that particular day.

    1. Totally possible but would require some dev work. I don’t do a ton of freelance but I could possibly help here as it seems like a small task. Feel free to shoot me an email or Twitter message: me@whoischris.com or @ChrisFlanny

  5. Can there be a conversion for subscription totals? I’m trying to piece together customer accounts with credit limits. Subscriptions work great for installments for customers. But I want to set up a syste where say, I give them a $500 credit limit. I make them make an initial purchase of $400. I require a first payment of say $100. So out of the $500 limit they spent $400 leaving available $100. Then the minimum payment they made to start subtracts another $100. Leaving now $200 available to use with $300 in installments. So i want to limit where they cannot but another product that would exceed the $200 installment plan or $500 credit limit. Similar to setting up store credit accounts. Is that possible?

Leave a Reply

Your email address will not be published.