获取所有活跃用户以获取WooCommerce中的会员计划

时间:2018-03-12 19:13:17

标签: php wordpress woocommerce hook-woocommerce woocommerce-memberships

我正在使用Wordpress版本4.9.4和WooCommerce WooCommerce Memberships plugin

如何让所有用户获得特定会员计划($ membership)?

是否有钩子或我应该使用原始查询?

在文档中,它说计划/成员资格是帖子类型,所以我试图查询这个表:

SELECT jsu.id, jsu.user_email, jsu.display_name, jsp.post_title, jsp.post_type FROM `wp_users` jsu
LEFT JOIN `wp_posts` jsp ON (jsu.id = jsp.post_author)
LEFT JOIN `wp_usermeta` jsum ON (jsu.id = jsum.user_id)
WHERE jsp.post_title IN ($memberships) AND post_type = 'wc_membership_plan'

但我没有得到理想的结果。

1 个答案:

答案 0 :(得分:2)

我在SQL中做了一些更改,并在实用程序函数中设置代码:

function get_active_members_for_membership($memberships){
    global $wpdb;

    // Getting all User IDs and data for a membership plan
    return $wpdb->get_results( "
        SELECT DISTINCT um.user_id, u.user_email, u.display_name, p2.post_title, p2.post_type
        FROM {$wpdb->prefix}posts AS p
        LEFT JOIN {$wpdb->prefix}posts AS p2 ON p2.ID = p.post_parent
        LEFT JOIN {$wpdb->prefix}users AS u ON u.id = p.post_author
        LEFT JOIN {$wpdb->prefix}usermeta AS um ON u.id = um.user_id
        WHERE p.post_type = 'wc_user_membership'
        AND p.post_status IN ('wcm-active')
        AND p2.post_type = 'wc_membership_plan'
        AND p2.post_title LIKE '$memberships'
    ");
}

此代码位于您的活动子主题(或主题)的function.php文件中。经过测试并正常工作。

用法:

// Replace 'My Membership' by your targeted membership name.
$users_data = get_active_members_for_membership('My Membership');

// Test raw output
echo '<pre>'; print_r( $users_data ); echo '</pre>';