3

我能够知道用户是否有给定产品的有效订阅:

$subscribed = WC_Subscriptions_Manager::user_has_subscription($userId, $productId, $status);

但我无法弄清楚用户有多少订阅了这个产品…… (Woocommerce Subscriptions 插件现在支持用户购买多个订阅)

有人知道怎么做吗?

4

2 回答 2

2

对于定义的已用 ID 和特定订阅产品 ID,此自定义函数将以非常简单的 SQL 查询为您提供活动订阅计数:

function get_user_active_subscriptions_count( $product_id, $user_id = null ) {
    global $wpdb;

    // if the user_id is not set in function argument we get the current user ID
    if( null == $user_id )
        $user_id = get_current_user_id();

    // return the active subscriptions for a define user and a defined product ID
    return $wpdb->get_var("
        SELECT COUNT(p.ID)
        FROM {$wpdb->prefix}posts as p
        LEFT JOIN {$wpdb->prefix}posts AS p2 ON p.post_parent = p2.ID
        LEFT JOIN {$wpdb->prefix}postmeta AS pm ON p2.ID = pm.post_id
        LEFT JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON pm.post_id = woi.order_id
        LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
        WHERE p.post_type LIKE 'shop_subscription' AND p.post_status LIKE 'wc-active'
        AND p2.post_type LIKE 'shop_order' AND woi.order_item_type LIKE 'line_item'
        AND pm.meta_key LIKE '_customer_user' AND pm.meta_value = '$user_id'
        AND woim.meta_key = '_product_id'
        AND woim.meta_value = '$product_id'
    ");
}

代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作。


用于定义的产品 ID9和:

  1. 用户 ID 作为动态变量$user_id

    回声'

    订阅计数:'。get_user_active_subscriptions_count('9', $user_id) 。'

    ';

  2. 一个已定义的用户 ID (这里的用户 ID 是15

    回声'

    订阅计数:'。get_user_active_subscriptions_count('9', '15') 。'

    ';

  3. 当前用户 ID:

    回声'

    订阅计数:'。get_user_active_subscriptions_count('9') 。'

    ';

产品 ID 也可以是动态的,使用变量而不是整数(对于产品 ID)


更新:要获取定义的已用 ID 的活动订阅中产品订阅的总数(在非常简单的 SQL 查询中):

function get_user_active_product_subscriptions_count( $product_id, $user_id = null ) {
    global $wpdb;

    // if the user_id is not set in function argument we get the current user ID
    if( null == $user_id )
        $user_id = get_current_user_id();

    // return the active subscriptions for a define user and a defined product ID
    return $wpdb->get_var("
        SELECT sum(woim2.meta_value)
        FROM {$wpdb->prefix}posts as p
        LEFT JOIN {$wpdb->prefix}posts AS p2 ON p.post_parent = p2.ID
        LEFT JOIN {$wpdb->prefix}postmeta AS pm ON p2.ID = pm.post_id
        LEFT JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON pm.post_id = woi.order_id
        LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
        LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim2 ON woim.order_item_id = woim2.order_item_id
        WHERE p.post_type LIKE 'shop_subscription' AND p.post_status LIKE 'wc-active'
        AND p2.post_type LIKE 'shop_order' AND woi.order_item_type LIKE 'line_item'
        AND pm.meta_key LIKE '_customer_user' AND pm.meta_value = '$user_id'
        AND woim.meta_key = '_product_id' AND woim.meta_value = '$product_id'
        AND woim2.meta_key = '_qty'
    ");
}

代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作。

用法与上述相同,但结果会有所不同,因为您将获得活动订阅中定义的产品订阅的总和(对于定义的用户ID(因此它将与定义的产品ID对应的所有项目的数量相加)在用户 ID 进行的订阅中)...</p>

于 2018-03-18T17:17:12.667 回答
0

同时,我认为这行得通。我不知道它的效率如何,但答案似乎是正确的——给定一个特定的用户和特定的产品,这将返回用户对该产品的订阅数量。 (确保在 $user_id 变量中提供 Wordpress user_id,在 $product_id 变量中提供 Woocommerce 产品 ID。)

$allSubscriptions = WC_Subscriptions_Manager::get_users_subscriptions($user_id);
$item_quantity = 0;
foreach ($allSubscriptions as $subscription){
    if (($subscription['status'] == 'active' || $subscription['status'] == 'pending-cancel') == false) continue;
    $order = wc_get_order($subscription['order_id']);
    // Iterating through each line-item in the order
    foreach ($order->get_items() as $item_id => $item_data) {
        if ($item_data->get_product()->get_id() != $product_id) continue;
        $item_quantity += $item_data->get_quantity();
    }
}
echo 'Quantity: ' . $item_quantity . '<br>';
于 2018-03-18T18:15:53.667 回答