【问题标题】:Get active subscriptions count for a defined product and user in Woocommerce获取 Woocommerce 中定义的产品和用户的活跃订阅数
【发布时间】:2018-03-18 16:36:37
【问题描述】:

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

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

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

有人知道怎么做吗?

【问题讨论】:

    标签: php sql wordpress woocommerce woocommerce-subscriptions


    【解决方案1】:

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

    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 文件中。经过测试并且可以工作。


    用于定义的产品 ID 9 和:

    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 对应的所有项目的数量)...

    【讨论】:

    • 感谢您的回答。但是,如果我没看错,它会返回所有用户的活动订阅数吗?我真的很想知道用户对特定产品有多少订阅(我有 product_id 和 user_id)。
    • 对。但同样,我想获取定义的用户 ID 定义的产品 ID 的活动订阅计数。 (“...对于给定的产品”)
    • @ariel 我已经更新了我的答案……我使用的代码是一个简单的、独特的轻量级快速查询……请尝试一下。谢谢。
    • 嗯。它没有给我与我的建议相同的答案。我已经设置了一个用户,其中包含由多个产品组成的多个订阅。用户总共在 3 个单独的订阅中订购了 10 次相同的订阅产品。我的答案是 10,这是正确的。有了你的,我得到 3。现在,3 确实是该用户拥有的包含给定产品的订阅数量。但在其中一些订阅中,他多次订阅了该产品。
    • @Ariel 抱歉,您的问题真的不清楚……您在哪里询问用户(对于给定产品)有多少活跃订阅但不是该用户具有相同 ID 的订阅产品的数量。所以这个答案对你的问题是正确的。
    【解决方案2】:

    同时,我认为这是可行的。我不知道它的效率如何,但答案似乎是正确的——给定一个特定的用户和特定的产品,这将返回用户对该产品的订阅数量。 (确保在 $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>';
    

    【讨论】:

    • WC_Subscriptions_Manager::get_users_subscriptions 自 2.0 起已弃用,请使用 wcs_get_users_subscriptions
    猜你喜欢
    • 2017-12-03
    • 2016-12-29
    • 2019-07-29
    • 2021-01-25
    • 2019-04-26
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 2017-06-18
    相关资源
    最近更新 更多