2

I found the code to check whether a user has an active subscription using the product ID...

<?php
$has_sub = wcs_user_has_subscription( '', 5861, 'active' );
if ( $has_sub) {?>
<p>You are a member of the Samurai</p>
<?php } ?>

...however, I'd like to check if they are a member of one of a few subscriptions at a time. I tried to use an array but to no avail...

<?php
$subscription_id = array(5861, 5862);
$has_sub = wcs_user_has_subscription( '', $subscription_id, 'active');
if ( $has_sub) {?>
<p>You are a member of the Samurai</p>
<?php } ?>

I guess the 'wcs_user_has_subscription' is looking for a single ID

Any ideas how I'd check for any of the IDs in that array

mujuonly
  • 11,370
  • 5
  • 45
  • 75
bboybeatle
  • 549
  • 1
  • 8
  • 28

2 Answers2

0

Gets all the active and inactive subscriptions for a user, as specified by $user_id

$subscriptions = wcs_get_users_subscriptions( $user_id );

$subscription_ids = array(5861, 5862);

        foreach ( $subscriptions as $subscription_id =>  $subscription ) {
            if(in_array($subscription, $subscription_ids)){
                            echo '<p>You are a member of the Samurai</p>';
                        }
        }
mujuonly
  • 11,370
  • 5
  • 45
  • 75
0
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_user  = is_user_logged_in() ? wp_get_current_user() : null;
$current_product = wc_get_product( $product_id );
$subscriptions = wcs_get_users_subscriptions( $current_user->ID );
// See if the current product user's are viewing is a subscription product
if($subscriptions)
{
    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;
                }
        }
            
    }
    else if($subscriptions)
    {
        
        //if(WC()->cart->get_cart_contents_count() > 1)
        //{
            foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                
                if( isset( $values["subscribe_product"] ) ) { 
                    //echo "<pre>";
                    //print_r($values);
                    wc_add_notice( "Only one product allowed in cart." );
                    return false;
                }
                
            }
        //}
    }
}
else
{
    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;
}
Maulik patel
  • 2,546
  • 2
  • 20
  • 26