Skip to main content

How to detect the current user has an active subscription in woocommerce?

Detect the current user has an active subscription in Woocommerce.


function has_active_subscription( $user_id=null ) {
    // When a $user_id is not specified, get the current user Id
    if( null == $user_id && is_user_logged_in() )
        $user_id = get_current_user_id();
    // User not logged in we return false
    if( $user_id == 0 )
        return false;

    // Get all active subscriptions for a user ID
    $active_subscriptions = get_posts( array(
        'numberposts' => 1, // Only one is enough
        'meta_key'    => '_customer_user',
        'meta_value'  => $user_id,
        'post_type'   => 'shop_subscription', // Subscription post type
        'post_status' => 'wc-active', // Active subscription
        'fields'      => 'ids', // return only IDs (instead of complete post objects)
    ) );

    return sizeof($active_subscriptions) == 0 ? false : true;
}




Or

if( has_active_subscription() ){ // Current user has an active subscription
    // do something … here goes your code

    // Example of displaying something
    echo '<p>I have active subscription</p>';
}



                                                                             Or


if( has_active_subscription(26) ){ // Defined User ID has an active subscription
    // do something … here goes your code

    // Example of displaying something
    echo '<p>User ID "26" have an active subscription</p>';
}

Comments

Popular posts from this blog

How to use inner html value or data in php from javascript(innerHTML)?

 use inner html value or data in php from javascript(innerHTML)? <html> <body> <p id="demo">use inner html value in php(innerhtml)</p> <script>   var jassi = document.getElementById("demo").innerHTML;   //document.write(jassi); </script> <?php $jassi = '<script>document.write(jassi);</script>'; echo $jassi; ?> </body> </html>

How to build a tree from a flat array in PHP>

Build a tree from a flat array in PHP   $hello = array   (   array("ID" => "1","student" => "Jassi","marks" => "80","parent_id" => ""),   array("ID" => "2","student" => "Golu","marks" => "80","parent_id" => "1"),   array("ID" => "3","student" => "Jassi","marks" => "80","parent_id" => "1"),   array("ID" => "4","student" => "Jai","marks" => "90","parent_id" => "1"),   array("ID" => "5","student" => "Jassi","marks" => "85","parent_id" => "1"),   array("ID" => "6","student" => "Ravinder","

How to create remove and add more inputs functionality with input type file and text in Laravel?

Create remove and add more inputs functionality with input type file and text in Laravel Blade file code       <form action="{{ route('storeDocuments') }}" method="post" enctype="multipart/form-data">         @csrf         <div id="inputFields">             @if(!empty($documents[0]))             @php $counter = 1000; @endphp                 @foreach($documents as $document)                     <input type="hidden" id="document_id" name="documents[{{$counter}}][document_id]" value="{{$document->id}}">                     <div class="field">                         <label for="title">Title:</label>                         <input type="text" id="title" class="title_input" name="documents[{{$counter}}][title]" value="{{$document->title}}" required>                         <label for=&