Php 获取电子商务订阅中订阅计划的活动成员列表
我正在尝试使用Woocommerce订阅来检索和显示特定订阅计划的活动成员 我想知道他们的名字、姓氏和计费电话,这样我的员工就可以轻松地知道他们是如何仍然活跃的 我试图找到一条进去的路,但没有成功Php 获取电子商务订阅中订阅计划的活动成员列表,php,sql,wordpress,woocommerce,woocommerce-subscriptions,Php,Sql,Wordpress,Woocommerce,Woocommerce Subscriptions,我正在尝试使用Woocommerce订阅来检索和显示特定订阅计划的活动成员 我想知道他们的名字、姓氏和计费电话,这样我的员工就可以轻松地知道他们是如何仍然活跃的 我试图找到一条进去的路,但没有成功 非常感谢您的帮助。这可以使用自定义SQL查询完成 下面的第一个函数将根据作为目标订阅计划的产品ID(或变体ID)进行查询 第二个功能将在html表格中显示结果,其中包含用户Id、账单名、账单姓、账单电话和账单电子邮件 守则: // The SQL query function get_active_s
非常感谢您的帮助。这可以使用自定义SQL查询完成 下面的第一个函数将根据作为目标订阅计划的产品ID(或变体ID)进行查询 第二个功能将在html表格中显示结果,其中包含用户Id、账单名、账单姓、账单电话和账单电子邮件 守则:
// The SQL query
function get_active_subscribers_for_plan( $product_id = 0, $variation_id = 0 ) {
global $wpdb;
if( $variation_id > 0 ){
$query_subscription_plan = "AND woim.meta_key = '_variation_id' AND woim.meta_value = '$variation_id'";
} else {
$query_subscription_plan = "AND woim.meta_key = '_product_id' AND woim.meta_value = '$product_id'";
}
$results = $wpdb->get_results( "
SELECT DISTINCT pm1.meta_value as user_id, pm2.meta_value as first_name,
pm3.meta_value as last_name, pm4.meta_value as phone, pm5.meta_value as email
FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm1 ON p.ID = pm1.post_id
JOIN {$wpdb->prefix}postmeta as pm2 ON p.ID = pm2.post_id
JOIN {$wpdb->prefix}postmeta as pm3 ON p.ID = pm3.post_id
JOIN {$wpdb->prefix}postmeta as pm4 ON p.ID = pm4.post_id
JOIN {$wpdb->prefix}postmeta as pm5 ON p.ID = pm5.post_id
JOIN {$wpdb->prefix}posts as p2 ON p.ID = p2.post_parent
JOIN {$wpdb->prefix}woocommerce_order_items as woi ON p2.ID = woi.order_id
JOIN {$wpdb->prefix}woocommerce_order_itemmeta as woim ON woi.order_item_id = woim.order_item_id
WHERE p.post_type = 'shop_order'
AND p2.post_type = 'shop_subscription'
AND p2.post_status = 'wc-active'
AND pm1.meta_key = '_customer_user'
AND pm2.meta_key = '_billing_first_name'
AND pm3.meta_key = '_billing_last_name'
AND pm4.meta_key = '_billing_phone'
AND pm5.meta_key = '_billing_email'
$query_subscription_plan
");
return $results;
}
// The Html Output
function display_active_subscribers_for_plan( $product_id = 0, $variation_id = 0 ){
// The query
$results = get_active_subscribers_for_plan( $product_id, $variation_id );
// The display
echo '<table>
<thead><tr>
<th>' . __("User ID","woocommerce") . '</th>
<th>' . __("First name","woocommerce") . '</th>
<th>' . __("Last name","woocommerce") . '</th>
<th>' . __("Phone","woocommerce") . '</th>
<th>' . __("Email","woocommerce") . '</th>
</tr></thead>
<tbody>';
// Loop through each subscriber
foreach( $results as $result ){
// Edit User Url
$edit_user_url = home_url('/wp-admin/user-edit.php?user_id=' . $result->user_id . '/');
echo '<tr>
<td><a href="' . $edit_user_url . '">' . $result->user_id . '</a></td>
<td>' . $result->first_name . '</td>
<td>' . $result->last_name . '</td>
<td>' . $result->phone . '</td>
<td><a href="malto:' . $result->email . '">' . $result->email . '</a></td>
</tr>';
}
echo '</tbody></table>';
}
2)对于变更订阅类型和定义的变更ID(其中149
是产品ID):
您将得到如下结果:
这可以使用自定义SQL查询来完成 下面的第一个函数将根据作为目标订阅计划的产品ID(或变体ID)进行查询 第二个功能将在html表格中显示结果,其中包含用户Id、账单名、账单姓、账单电话和账单电子邮件 守则:
// The SQL query
function get_active_subscribers_for_plan( $product_id = 0, $variation_id = 0 ) {
global $wpdb;
if( $variation_id > 0 ){
$query_subscription_plan = "AND woim.meta_key = '_variation_id' AND woim.meta_value = '$variation_id'";
} else {
$query_subscription_plan = "AND woim.meta_key = '_product_id' AND woim.meta_value = '$product_id'";
}
$results = $wpdb->get_results( "
SELECT DISTINCT pm1.meta_value as user_id, pm2.meta_value as first_name,
pm3.meta_value as last_name, pm4.meta_value as phone, pm5.meta_value as email
FROM {$wpdb->prefix}posts as p
JOIN {$wpdb->prefix}postmeta as pm1 ON p.ID = pm1.post_id
JOIN {$wpdb->prefix}postmeta as pm2 ON p.ID = pm2.post_id
JOIN {$wpdb->prefix}postmeta as pm3 ON p.ID = pm3.post_id
JOIN {$wpdb->prefix}postmeta as pm4 ON p.ID = pm4.post_id
JOIN {$wpdb->prefix}postmeta as pm5 ON p.ID = pm5.post_id
JOIN {$wpdb->prefix}posts as p2 ON p.ID = p2.post_parent
JOIN {$wpdb->prefix}woocommerce_order_items as woi ON p2.ID = woi.order_id
JOIN {$wpdb->prefix}woocommerce_order_itemmeta as woim ON woi.order_item_id = woim.order_item_id
WHERE p.post_type = 'shop_order'
AND p2.post_type = 'shop_subscription'
AND p2.post_status = 'wc-active'
AND pm1.meta_key = '_customer_user'
AND pm2.meta_key = '_billing_first_name'
AND pm3.meta_key = '_billing_last_name'
AND pm4.meta_key = '_billing_phone'
AND pm5.meta_key = '_billing_email'
$query_subscription_plan
");
return $results;
}
// The Html Output
function display_active_subscribers_for_plan( $product_id = 0, $variation_id = 0 ){
// The query
$results = get_active_subscribers_for_plan( $product_id, $variation_id );
// The display
echo '<table>
<thead><tr>
<th>' . __("User ID","woocommerce") . '</th>
<th>' . __("First name","woocommerce") . '</th>
<th>' . __("Last name","woocommerce") . '</th>
<th>' . __("Phone","woocommerce") . '</th>
<th>' . __("Email","woocommerce") . '</th>
</tr></thead>
<tbody>';
// Loop through each subscriber
foreach( $results as $result ){
// Edit User Url
$edit_user_url = home_url('/wp-admin/user-edit.php?user_id=' . $result->user_id . '/');
echo '<tr>
<td><a href="' . $edit_user_url . '">' . $result->user_id . '</a></td>
<td>' . $result->first_name . '</td>
<td>' . $result->last_name . '</td>
<td>' . $result->phone . '</td>
<td><a href="malto:' . $result->email . '">' . $result->email . '</a></td>
</tr>';
}
echo '</tbody></table>';
}
2)对于变更订阅类型和定义的变更ID(其中149
是产品ID):
您将得到如下结果:
谢谢,但是如果我想添加一个自定义的元字段呢?比如地址、开始日期和结束日期OK,我将测试这个函数并返回给您,您能确认它只处于“活动”状态吗members?@Gamalelwazery我已经添加了这一行
和p2.post_status='wc active'
,这将只查询活动订阅。谢谢,但是如果我想添加一个自定义元字段呢?比如地址和开始日期和结束日期确定我将测试此函数并返回给你,那么你能确认它只“活动”吗members?@Gamalelwazery我添加了这一行和p2.post_status='wc active'
,这将只查询活动订阅。
display_active_subscribers_for_plan( 149, 'variation' );