Skip to content

Instantly share code, notes, and snippets.

@mattallan
Last active August 29, 2015 14:19
Show Gist options
  • Save mattallan/72432317141c782213f4 to your computer and use it in GitHub Desktop.
Save mattallan/72432317141c782213f4 to your computer and use it in GitHub Desktop.
Limit Subscriptions to one active/suspended per user.
<?php
/**
* Plugin Name: User Limited Subscriptions
* Description: Limit users to only one active or suspended subscription
* Author: Matt Allan
* Author URI: http://www.prospress.com
* Version: 1.0
* License: GPL v2
*/
/**
* Limit customers to one active or suspended subscription. Once they cancell their subscription
* or the subscriptions expires, they will then be able to purchase another.
*
* @param bool $purchasable
* @param WC_Product_Subscription|WC_Product_Variable_Subscription $product
* @author matt@prospress.com
*/
function mc_one_subscription_per_customer( $purchasable, $product ) {
$user = wp_get_current_user();
if( in_array( get_option( WC_Subscriptions_Admin::$option_prefix . '_subscriber_role' ), $user->roles ) ) {
$purchasable = false;
}
return $purchasable;
}
add_filter( 'woocommerce_subscription_is_purchasable', 'mc_one_subscription_per_customer', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment