7

プラグイン「WooCommerce サブスクリプション」を使用しており、製品がシステム内にアクティブなサブスクライバーを既に持っているかどうかを確認したい

製品ごとに 1 人のサブスクライバーのみが必要です。それをチェックするために利用できるフィルターがありますが、それを使用する方法がわかりません:
https://docs.woocommerce.com/document/subscriptions/develop/filter-reference/

これを達成するために、その関数またはフックをどのように使用できますか?

ありがとう

4

1 に答える 1

7

このカスタムメイドの条件付き関数は、サブスクリプション製品がサブスクライバーによって既にアクティブに使用されている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 (定義済みの $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たとえば、製品の ID が 124 の場合は、ここで製品 ID に置き換えることもできます)。

If ( has_an_active_subscriber( 124 ) )  //do something

この条件付き関数は、特に(サブスクリプション)テンプレート (サブスクリプション プラグイン テンプレート フォルダーからアクティブなテーマの woocommerce テンプレート フォルダーにコピーする必要があります) で使用できます。add-to-cart

すべてのコードはテスト済みで、完全に機能します

参考文献:

于 2016-08-24T10:11:13.410 に答える