检测当前用户是否具有有效订阅 [英] Detecting if the current user has an active subscription

查看:108
本文介绍了检测当前用户是否具有有效订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WooCommerce开发WordPress网站.我还使用了 WC付费列表

I'm developing a website in WordPress with WooCommerce. I'm using additionally WC Paid Listings and WooCommerce Subscriptions plugins to handle my work.

问题是当具有订阅者"角色的用户具有有效的订阅登录时,即使他/她具有有效的订阅,每次他/她必须选择软件包时都尝试发布内容.

The problem is when a user with "subscriber" role with an active subscription login and try to post a content every time he / she has to choose a package even he has an active subscription.

有没有人知道如何检测用户是否有有效的订阅,如果返回true,则跳过选择软件包的步骤?

Is there anyone with an idea of how to detect if user has an active subscription, if it returns true then the step choosing package skipped?

谢谢.

推荐答案

已更新(2019)

  • New conditional function using WooCommerce Subscriptions wcs_user_has_subscription().
  • New conditional function using a much lighter code version (SQL query).
  • Original enhanced conditional function based on an improved WP_Query.

以下自定义条件函数具有可选参数$user_id (已定义的用户ID),当当前用户(或已定义的用户)返回 true 拥有有效的订阅.

The following custom conditional functions have an optional argument $user_id (a defined user_id) and will return true when the current user (or a defined user) has active subscriptions.

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

1)使用WooCommerce订阅专用的条件功能 wcs_user_has_subscription() :

1) Using WooCommerce Subscriptions dedicated conditional function 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;
}

代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

3)原始的增强代码,也将执行相同的操作:

3) The original enhanced code, that will also do the same:

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;
}

代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

使用情况更新:

1)当前用户的用法:

1) USAGE for the current user:

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的用法:

2) USAGE for a defined user 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>';
}

此代码已经过测试,可以正常工作

This code is tested and it works

相关答案:

  • WooCommerce Subscriptions - Check if product already has an active subscriber
  • WooCommerce - Get active subscriptions in a list between start / end date

这篇关于检测当前用户是否具有有效订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆