यह कस्टम मेड सशर्त समारोह 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()
देशी सदस्यता समारोह का इस्तेमाल किया है, एक परिभाषित यूजर आईडी के लिए सदस्यता प्राप्त करने के लिए,।
उपयोग(एक परिभाषित $ 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
}
तुम भी $product_id
एक उत्पाद आईडी के आधार पर यहां उदाहरण के लिए जगह ले सकता है, तो उत्पाद की आईडी 124 है) :
If (has_an_active_subscriber(124)) //do something
आप इस सशर्त समारोह विशेष रूप से उपयोग कर सकते हैं पर add-to-cart
(सदस्यता) टेम्पलेट्स (है कि आप अपने सक्रिय theme's woocommerce template folder के लिए सदस्यता प्लगइन टेम्पलेट्स फ़ोल्डर से कॉपी करना होगा ...)
सभी कोड परीक्षण किया जाता है, और पूरी तरह कार्यात्मक
संदर्भ:
किसी भी मदद करें। – user3814097