WooCommerce 订阅 - 检查产品是否已有活跃订阅者
WooCommerce Subscriptions - Check if product already has an active subscriber
我正在使用插件"WooCommerce Subscriptions",我想检查该产品是否已经在系统中拥有活跃订阅者
我每个产品只需要 1 个订阅者。有一个过滤器可以用来检查它,但我不知道如何使用它:
https://docs.woocommerce.com/document/subscriptions/develop/filter-reference/
如何使用该函数或挂钩来实现此目的?
谢谢
这个定制的条件函数将 return true
如果 订阅产品已经被订阅者主动使用 .
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;
}
此代码在您的活动子主题(或主题)的 function.php 或任何插件文件上。
我刚刚在这里使用了 wcs_get_users_subscriptions()
本机订阅功能,在我的代码中获取定义的用户 ID 的订阅。
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…)
所有代码都经过测试且功能齐全
参考文献:
我正在使用插件"WooCommerce Subscriptions",我想检查该产品是否已经在系统中拥有活跃订阅者
我每个产品只需要 1 个订阅者。有一个过滤器可以用来检查它,但我不知道如何使用它:
https://docs.woocommerce.com/document/subscriptions/develop/filter-reference/
如何使用该函数或挂钩来实现此目的?
谢谢
这个定制的条件函数将 return true
如果 订阅产品已经被订阅者主动使用 .
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;
}
此代码在您的活动子主题(或主题)的 function.php 或任何插件文件上。
我刚刚在这里使用了 wcs_get_users_subscriptions()
本机订阅功能,在我的代码中获取定义的用户 ID 的订阅。
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…)
所有代码都经过测试且功能齐全
参考文献: