programing

현재 사용자에게 활성 구독이 있는지 감지

easyjava 2023. 3. 20. 23:41
반응형

현재 사용자에게 활성 구독이 있는지 감지

WooCommerce와 함께 WordPress에서 웹사이트를 개발하고 있습니다.WC 유료 목록과 WooCommerce Subscriptions 플러그인을 추가로 사용하여 작업을 처리하고 있습니다.

문제는 액티브한 서브스크립션 로그인을 가진 '서브스크라이버' 역할을 가진 사용자가 액티브한 서브스크립션이 있어도 패키지를 선택해야 할 때마다 콘텐츠를 게시하려고 하는 경우입니다.

사용자가 활성 구독을 가지고 있는지 여부를 감지하고 true를 반환하면 단계 선택 패키지를 건너뛸 수 있는 방법을 알고 있는 사람이 있습니까?

갱신(2019년)

  • WooCommerce Subscriptions를 사용한 새로운 조건부 함수.
  • 훨씬 더 가벼운 코드 버전을 사용하는 새로운 조건부 함수(SQL 쿼리)
  • 개선된 WP_Query를 기반으로 한 원래 향상된 조건부 함수입니다.

다음 사용자 지정 조건부 함수에는 선택적 인수가 있습니다.$user_id (정의된 user_id)로 현재 사용자(또는 정의된 사용자)가 활성 서브스크립션을 가지고 있을 때 반환됩니다.

작업은 3가지 다른 방법으로 실행할 수 있습니다(동일한 방법으로 실행).

1) WooCommerce Subscriptions 전용 조건부 함수 사용:

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

이 코드는 테스트되어 동작합니다.


관련 답변:

사용하다wcs_user_has_subscription()

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

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

언급URL : https://stackoverflow.com/questions/42403821/detecting-if-the-current-user-has-an-active-subscription

반응형