【问题标题】:Detecting if the current user has an active subscription检测当前用户是否有活动订阅
【发布时间】:2017-07-13 05:31:35
【问题描述】:

我正在使用 WooCommerce 在 WordPress 中开发一个网站。我还使用了WC Paid ListingsWooCommerce Subscriptions 插件来处理我的工作。

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

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

谢谢。

【问题讨论】:

    标签: php wordpress woocommerce orders woocommerce-subscriptions


    【解决方案1】:

    更新(2019 年)

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

    以下自定义条件函数有一个可选参数$user_id(定义的用户id),当当前用户(或定义的用户)返回true有有效的订阅。

    所以现在可以使用 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;
    }
    

    代码进入您的活动子主题(或主题)的 function.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;
    }
    

    代码进入您的活动子主题(或主题)的 function.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>';
    }
    

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


    相关答案:

    【讨论】:

    • 感谢 Loic,代码似乎可以工作,但问题是当我重定向页面时出现以下错误警告:无法修改标头信息 - 标头已发送(输出开始于 /home2/xxx/xxxx /wp-content/themes/jobsdojo/header.php:13) 在 /home2/xxx/public_html/quickextra/wp-includes/pluggable.php 的第 1174 行。请再提供一点帮助
    • 感谢 @ Loic TheAztec 我试图覆盖 package-selection.php
    • 此代码很有帮助,但如果您有大型帖子元表,请注意实施。由于缺少索引,它将进行全表扫描。我刚刚修复了一个复制/粘贴此内容的站点,如果他们有活动订阅,则必须对其进行修改以设置和检查会话变量,并在调用 get_posts 之前检查用户 ID 不是 0。无论用户是否登录,他们都会在页面上调用它来显示优质内容。如果未登录,get_current_user_id 将始终返回 0。在我的更改优化为 200/秒后,站点经历了 2k 次选择/秒。我还为元键/值添加了索引。
    • @mtg169 谢谢(这是一个旧答案)...我已经更新了功能代码并进行了一些增强,并添加了其他 2 种不同的方式:更轻量级的代码版本 (使用 SQL 查询) 另一个基于 wcs_user_has_subscription() WooCommerce 订阅条件函数。
    【解决方案2】:

    使用wcs_user_has_subscription()

    $has_sub = wcs_user_has_subscription( '', '', 'active' );
    
    if ( $has_sub) {
        // User have active subscription
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-26
      • 2016-12-16
      • 2018-04-12
      • 2016-09-30
      • 2014-08-15
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      相关资源
      最近更新 更多