By default, Paid Member Subscriptions doesn’t alter WooCommerce queries so restricted products will show up in the Shop page (or product category archive).
It might be desirable to not let the user see these products if he doesn’t have the right membership plan.
Using the custom code from below you can remove the restricted products from these queries so users will only see products that they can purchase:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | //Alters the output of the query that displays the Category Archive (including Shop) pages, filtering the restricted products from the output add_action( 'woocommerce_product_query', 'pmsc_exclude_restricted_products_from_woocoommerce_category_queries' ); function pmsc_exclude_restricted_products_from_woocoommerce_category_queries( $query ) { //if you add $query->is_tax() to the if, it will affect only the category pages; else it affects all queries including the shop page if( !is_admin() && function_exists('pms_is_post_restricted') ) { $args = $query->query_vars; $args['suppress_filters'] = true; $args['posts_per_page'] = -1; $products = wc_get_products($args); $product_ids = array(); foreach ($products as $product) { $product_ids[] = $product->get_id(); } $restricted_ids = array_filter($product_ids, 'pms_is_post_restricted'); $query->set( 'post__not_in', $restricted_ids ); } } |
The code from above does not affect the [products] shortcode, but we can do the same thing for this shortcode also:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | //Alters the output of the [products] shortcode from WooCommerce, filtering restricted products from the output add_action( 'woocommerce_shortcode_products_query', 'pmsc_exclude_restricted_products_from_woocoommerce_products_shortcode_queries' ); function pmsc_exclude_restricted_products_from_woocoommerce_products_shortcode_queries( $query_args ) { if( !is_admin() && function_exists('pms_is_post_restricted') ) { $posts_per_page = $query_args['posts_per_page']; $query_args['suppress_filters'] = true; $query_args['posts_per_page'] = '-1'; $products = wc_get_products($query_args); $query_args['posts_per_page'] = $posts_per_page; $product_ids = array(); foreach ($products as $product) { $product_ids[] = $product->get_id(); } $restricted_ids = array_filter($product_ids, 'pms_is_post_restricted'); $query_args['post__not_in'] = $restricted_ids; } return $query_args; } |
If you don’t know how to use these code snippets, please read the instructions found at the top of this page.
Combine the power of Profile Builder with Paid Member Subscriptions to set up user registration, memberships, and recurring revenue.
Get 25% off with the bundle