我能够知道用户是否拥有给定产品的有效订阅:
$subscribed = WC_Subscriptions_Manager::user_has_subscription($userId, $productId, $status);
但我还没有弄清楚用户拥有多少订阅此产品... (Woocommerce Subscriptions插件现在支持用户购买多个订阅)。
任何人都知道怎么做?
答案 0 :(得分: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文件中。经过测试和工作。
定义的商品标识9
和
1)用户ID作为动态变量$user_id
:
echo '<p>Subscription count: ' . get_user_active_subscriptions_count( '9', $user_id ) . '</p>';
2)已定义的用户ID (此处用户ID为 15
):
echo '<p>Subscription count: ' . get_user_active_subscriptions_count( '9', '15' ) . '</p>';
3)当前用户ID:
echo '<p>Subscription count: ' . get_user_active_subscriptions_count( '9' ) . '</p>';
产品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的所有项目的数量)...
答案 1 :(得分: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>';