WooCommerce Subscriptions - Check to see if the product has an active subscriber

I use the WooCommerce Subscriptions plugin and I want to check if the product has an active subscriber in the system

I need only one subscriber per product. There is a filter for checking, but I do not know how to use it:
https://docs.woocommerce.com/document/subscriptions/develop/filter-reference/

How can I use these functions or interceptors to achieve this?

thanks

+5
source share
1 answer

This conditional conditional function returns true if the subscribed product is already actively used by the 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 extends to function.php of your active child theme (or theme), as well as to any plugin file.

I just used wcs_get_users_subscriptions() here as wcs_get_users_subscriptions() own subscription function to get subscriptions for a specific user ID in my code.


USAGE (for a specific variable $ product_id)

 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 with a product identifier, for example, if the product identifier is 124):

 If ( has_an_active_subscriber( 124 ) ) //do something 

You can use this conditional function , in particular, on add-to-cart templates (subscription) (which you will have to copy from the folder of templates of subscription plugins to your active theme template template woocommerce ...)

All code is tested and fully functional.

References:

+4
source

All Articles