【问题标题】:Check if current WordPress user has WooCommerce Subscription using array of product IDs使用产品 ID 数组检查当前 WordPress 用户是否拥有 WooCommerce 订阅
【发布时间】:2021-08-28 23:36:28
【问题描述】:

我找到了使用产品 ID 检查用户是否有活动订阅的代码...

<?php
$has_sub = wcs_user_has_subscription( '', 5861, 'active' );
if ( $has_sub) {?>
<p>You are a member of the Samurai</p>
<?php } ?>

...但是,我想检查他们是否同时是几个订阅之一的成员。我尝试使用数组但无济于事......

<?php
$subscription_id = array(5861, 5862);
$has_sub = wcs_user_has_subscription( '', $subscription_id, 'active');
if ( $has_sub) {?>
<p>You are a member of the Samurai</p>
<?php } ?>

我猜 'wcs_user_has_subscription' 正在寻找一个 ID

关于如何检查该数组中的任何 ID 的任何想法

【问题讨论】:

  • 使用循环?为数组中的每个(提示提示)id 调用 wcs_user_has_subscription()。

标签: php wordpress woocommerce woocommerce-subscriptions


【解决方案1】:

获取用户的所有活动和非活动订阅,由 $user_id 指定

$subscriptions = wcs_get_users_subscriptions( $user_id );

$subscription_ids = array(5861, 5862);

        foreach ( $subscriptions as $subscription_id =>  $subscription ) {
            if(in_array($subscription, $subscription_ids)){
                            echo '<p>You are a member of the Samurai</p>';
                        }
        }

【讨论】:

    猜你喜欢
    • 2017-06-18
    • 2016-12-29
    • 2021-07-01
    • 1970-01-01
    • 2017-02-16
    • 2017-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多