I run WordPress, WooCommerce, WooCommerce Memberships & WooCommerce Subscriptions.
Here I'm trying to lookup the user and the memberships they have purchased, each membership is basically a product, and then I'm attempting to loop and show membership information that they have subscribed to - featured image and name of each membership.
The code I have below lists all memberships and I can't figure out what I'm doing wrong.
<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>