1

我运行 WordPress、WooCommerce、WooCommerce 会员资格和 WooCommerce 订阅。

在这里,我试图查找用户和他们购买的会员资格,每个会员资格基本上都是一个产品,然后我试图循环显示他们订阅的会员信息 - 每个会员的特色图像和名称。

我在下面的代码列出了所有成员资格,但我不知道我做错了什么。

<div id="sidebar">
    <div class="sidebar-title">
        <h2>My Subscriptions</h2>
    </div>
    <div class="sidebar-body">
        <div class="channel-package-list">
            <?php
                $args = array(
                    'post_type' => 'product',
                    'posts_per_page' => 20,
                    );
                $loop = new WP_Query( $args );
                if ( $loop->have_posts() ) {

                // get all active memberships for a user; 
                // returns an array of active user membership objects
                $user_id = get_current_user_id();

                $args = array( 
                  'status' => array( 'active', 'complimentary', 'pending', 'free_trial' ),
                );  

                $active_memberships = wc_memberships_get_user_memberships( $user_id, $args );

                    while ( $loop->have_posts() ) : $loop->the_post(); 
                    if ( ! empty( $active_memberships ) ) {?>
            <div class="package-list-item">
                <div class="active-package"><i class="fa fa-check" aria-hidden="true"></i></div>
                <a href="javascript:;" class="group-link" data-id="27">
                    <figure>
                        <img src="<?php echo get_the_post_thumbnail_url($post , 'thumbnail'); ?>" class="img-responsive" style="max-width: 100px; max-height: 100px" alt="">
                        <figcaption><?php echo get_the_title($post ); ?></figcaption>
                    </figure>
                </a>
            </div>
            <?php } endwhile;
                } else {
                echo __( 'No packages found' );
                }
                wp_reset_postdata(); ?>
        </div>
    </div>
</div>
4

1 回答 1

0

您正在循环所有产品和会员资格,甚至没有检查与会员资格相关的产品 ID。

假设您在购买产品时授予会员资格(检查您的会员计划设置),删除所有循环,搜索用户的会员资格,通过会员计划 ID 获取产品信息:

<div id="sidebar">
<div class="sidebar-title">
    <h2>My Subscriptions</h2>
</div>
<div class="sidebar-body">
    <div class="channel-package-list">
        <?php
          /*  $args = array(
                'post_type' => 'product',
                'posts_per_page' => 20,
                );
            $loop = new WP_Query( $args );
            if ( $loop->have_posts() ) {*/

            // get all active memberships for a user;
            // returns an array of active user membership objects
            $user_id = get_current_user_id();

            $args = array(
              'status' => array( 'active', 'complimentary', 'pending', 'free_trial' ),
            );

            $active_memberships = wc_memberships_get_user_memberships( $user_id, $args );

              //  while ( $loop->have_posts() ) : $loop->the_post();
            if ( ! empty( $active_memberships ) ) {
                                    foreach ( $active_memberships as $plan ) {

                                            $membership_id = $plan->id;
                                            $membership_plan_product_id = get_post_meta($membership_id,'_product_id')[0];

                                        ?>

        <div class="package-list-item">
            <div class="active-package"><i class="fa fa-check" aria-hidden="true"></i></div>
            <a href="javascript:;" class="group-link" data-id="27">
                <figure>
                    <img src="<?php echo get_the_post_thumbnail_url($membership_plan_product_id , 'thumbnail'); ?>" class="img-responsive" style="max-width: 100px; max-height: 100px" alt="">
                    <figcaption><?php echo get_the_title($membership_plan_product_id ); ?></figcaption>
                </figure>
            </a>
        </div>
        <?php

                }}
                //} endwhile;
          //  } else {
          //  echo __( 'No packages found' );
          //  }
        //    wp_reset_postdata(); ?>
    </div>
</div>

于 2019-03-19T23:11:18.033 回答