WooCommerce订阅-检查产品是否已有有效的订阅者 [英] WooCommerce Subscriptions - Check if product already has an active subscriber
问题描述
我正在使用"WooCommerce订阅"插件,我想检查产品是否已经在系统中具有活动订阅者
I am using a plugin "WooCommerce Subscriptions" and I want to check if the product already has the active subscriber in the system
每个产品只希望有1个订阅者.有一个过滤器可以检查它,但我不知道如何使用它:
https://docs.woocommerce.com/document/subscriptions/develop/filter-reference/
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?
谢谢
推荐答案
如果订户已经在积极使用订阅产品,则此定制条件条件函数将返回 true
. strong>.
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;
}
此代码在您的活动子主题(或主题)的function.php或任何插件文件上进行.
我只是在这里使用 wcs_get_users_subscriptions()
本机订阅功能,以在我的代码中获取已定义用户ID的订阅.
I just have used here wcs_get_users_subscriptions()
native subscription function, to get the subscriptions for a defined user ID, in my code.
用法 (用于已定义的$ 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
}
您也可以在此处用产品ID替换$product_id
,例如,如果产品ID为124):
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
您可以使用此条件功能,尤其是在 add-to-cart
(订阅)模板上(必须从订阅中复制)插件模板文件夹保存到活动的主题的woocommerce模板文件夹…)
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…)
所有代码都经过测试且功能齐全
参考:
- Template Structure + Overriding Templates via a Theme
- WooCommerce Subscriptions developper docs
这篇关于WooCommerce订阅-检查产品是否已有有效的订阅者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!