Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce Subscriptions - Check if product already has an active subscriber

I am using a plugin "WooCommerce Subscriptions" and I want to check if the product already has the active subscriber in the system

I only want 1 subscriber per product. There is a filter available to check it but I don't know how to use it:
https://docs.woocommerce.com/document/subscriptions/develop/filter-reference/

How can I use that functions or hooks, to achieve this?

Thanks

like image 716
user3814097 Avatar asked Aug 23 '16 07:08

user3814097


People also ask

How do I check my subscription status in WooCommerce?

You can try using the wcs_get_subscriptions function. First you need to track somehow which subscription the user created account with. If they are able to later signup for something else or they cancel the first and add another subscription, you will need to be able to get the first one.

What is the difference between WooCommerce memberships and Subscriptions?

The main difference between WooCommerce subscriptions and memberships is that WooCommerce subscriptions are sold as a product, while memberships are a service. This means that WooCommerce subscriptions can be sold as an add-on to other products or services, and memberships can be sold as a standalone product.

Can WooCommerce handle Subscriptions?

WooCommerce Subscriptions by WooCommerceWith WooCommerce Subscriptions, you can create and manage products with recurring payments. This implies that these payments will give the residual revenue that you can track later. Moreover, you can also introduce variety of subscriptions for physical and virtual products.

How do I export active Subscriptions in WooCommerce?

Once the extension is activated, you need to go to WooCommerce > Settings > Subscriptions and click on the Export tab. On this page, you will see an Export button. By clicking on it, you will be able to export your WooCommerce subscriptions as a CSV file.


1 Answers

This custom made conditional function will return true if a subscription product is already actively used by a subscriber.

function has_an_active_subscriber( $product_id = null ){

    // Empty array to store ALL existing Subscription PRODUCTS
    $products_arr = array();


    $products_subscr = get_posts( array(
        'numberposts' => -1,
        'post_status' => 'publish',
        'post_type'   => array( 'product', 'product_variation' ),
        'meta_key' => '_subscription_price',
    ) );
    foreach( $products_subscr as $prod_subs ) {
        $products_arr[] = $prod_subs->ID;
    }

    // Testing if current product is a subscription product
    if (in_array( $product_id, $products_arr) ){

        // Declaring empties arrays
        $subscribers_arr = array(); // ALL subscribers IDS
        $active_subscriptions_arr = array(); // ALL actives subscriptions
        $active_subscription_products_arr = array(); // ALL actif subscription products IDS IDS
        $subscriber_subscriptions = array();

        // Getting arrays of "active" IDS for subscribers, subscriptions orders and subscription products
        $subscribers = get_users( array( 'role' => 'subscriber') );
        foreach( $subscribers as $subscriber ) {
            $subscriber_arr[] = $subscriber->ID;
            $subscriptions = wcs_get_users_subscriptions($subscriber->ID);
            foreach ($subscriptions as  $key => $subscription ){
                $subscription_status = $subscription->post->post_status;
                if ( $subscription_status == 'wc-active' ) { // active subscriptions only
                    $subscription_id = $subscription->post->ID;
                    $order_id = $subscription->order->post->ID; // order ID (corresponding to the subscription ID)
                    $active_subscriptions_arr[] = $subscription->post->ID;
                    $order_items = $subscription->order->get_items();
                    // Getting all the products in the Order
                    foreach ( $order_items as $item ) {
                        // $item_id = $item[product_id];

                        // Avoiding to add existing products in the array 
                        if( !in_array( $product_id, $active_subscription_products_arr ))
                            $active_subscription_products_arr[] = $item[product_id];
                    }
                }
            }
        }
    }
    if (in_array( $product_id, $active_subscription_products_arr ) ) return true;
    else return false;
}

This code goes on function.php of your active child theme (or theme) or also on any plugin file.

I just have used here wcs_get_users_subscriptions() native subscription function, to get the subscriptions for a defined user ID, in my code.


USAGE (for a defined $product_id variable)

If ( has_an_active_subscriber( $product->id ) ) { // or $product_id
    // This product is already used by an active subscriber
    // DO SOMETHING HERE
} else {
    // This product is NOT used
    // DO SOMETHING HERE
}

You can also replace $product_id by a product ID here for example if the ID of the product is 124):

If ( has_an_active_subscriber( 124 ) )  //do something

You can use this conditional function particularly, on add-to-cart (subscription) templates (that you will have to copy from the subscription plugin templates folder to your active theme's woocommerce template folder…)

All code is tested and fully functional

References:

  • Template Structure + Overriding Templates via a Theme
  • WooCommerce Subscriptions developper docs
like image 134
LoicTheAztec Avatar answered Nov 08 '22 13:11

LoicTheAztec