检测当前用户是否具有活动订阅

我正在用WooCommerce在WordPress中开发一个网站。我正在使用额外的WC付费列表WooCommerce订阅插件来处理我的工作。

问题是,当具有活动订阅登录名的“订阅者”角色的用户每次必须选择包时都会尝试发布内容,即使他有一个有效的订阅。

是否有人知道如何检测用户是否具有活动订阅,如果它返回 true,则跳过选择包的步骤?


答案 1

已更新 (2019)

  • 使用WooCommerce订阅的新条件函数wcs_user_has_subscription())。
  • 使用更轻量级代码版本(SQL 查询)的新条件函数。
  • 基于改进WP_Query的原始增强条件函数。

以下自定义条件函数具有可选参数(定义的user_id),当当前用户(或定义的用户)具有活动订阅时,将返回 true$user_id

因此,现在可以使用3种不同的方法(做同样的事情)来完成此操作:

1)使用WooCommerce订阅专用条件函数wcs_user_has_subscription()

function has_active_subscription( $user_id='' ) {
    // When a $user_id is not specified, get the current user Id
    if( '' == $user_id && is_user_logged_in() ) 
        $user_id = get_current_user_id();
    // User not logged in we return false
    if( $user_id == 0 ) 
        return false;

    return wcs_user_has_subscription( $user_id, '', 'active' );
}

2)对于更轻量级的SQL查询(2019年3月添加)也是如此

function has_active_subscription( $user_id=null ) {
    // When a $user_id is not specified, get the current user Id
    if( null == $user_id && is_user_logged_in() ) 
        $user_id = get_current_user_id();
    // User not logged in we return false
    if( $user_id == 0 ) 
        return false;

    global $wpdb;

    // Get all active subscriptions count for a user ID
    $count_subscriptions = $wpdb->get_var( "
        SELECT count(p.ID)
        FROM {$wpdb->prefix}posts as p
        JOIN {$wpdb->prefix}postmeta as pm 
            ON p.ID = pm.post_id
        WHERE p.post_type = 'shop_subscription' 
        AND p.post_status = 'wc-active'
        AND pm.meta_key = '_customer_user' 
        AND pm.meta_value > 0
        AND pm.meta_value = '$user_id'
    " );

    return $count_subscriptions == 0 ? false : true;
}

代码进入函数.php活动子主题(或主题)的文件,也可以在任何插件文件中。


3)原始的增强代码,也将做同样的事情:

function has_active_subscription( $user_id=null ) {
    // When a $user_id is not specified, get the current user Id
    if( null == $user_id && is_user_logged_in() ) 
        $user_id = get_current_user_id();
    // User not logged in we return false
    if( $user_id == 0 ) 
        return false;

    // Get all active subscriptions for a user ID
    $active_subscriptions = get_posts( array(
        'numberposts' => 1, // Only one is enough
        'meta_key'    => '_customer_user',
        'meta_value'  => $user_id,
        'post_type'   => 'shop_subscription', // Subscription post type
        'post_status' => 'wc-active', // Active subscription
        'fields'      => 'ids', // return only IDs (instead of complete post objects)
    ) );

    return sizeof($active_subscriptions) == 0 ? false : true;
}

代码进入函数.php活动子主题(或主题)的文件,也可以在任何插件文件中。


使用情况更新:

1) 当前用户的用法:

if( has_active_subscription() ){ // Current user has an active subscription 
    // do something … here goes your code

    // Example of displaying something
    echo '<p>I have active subscription</p>';
}

2) 已定义用户 ID 的用法:

if( has_active_subscription(26) ){ // Defined User ID has an active subscription 
    // do something … here goes your code

    // Example of displaying something
    echo '<p>User ID "26" have an active subscription</p>';
}

此代码经过测试并正常工作


相关解答:


答案 2

wcs_user_has_subscription()

$has_sub = wcs_user_has_subscription( '', '', 'active' );

if ( $has_sub) {
    // User have active subscription
}

推荐