warnerque
warnerque

Reputation: 83

Display a list of private products for unlogged users in WooCommerce

For some reason, I need to show the list of private products on a single page on WooCommerce for guest users (non-logged in users). How can this be done with (or without) programming?

Upvotes: 0

Views: 705

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254182

You can use a normal woocommerce shortcode on a specific page where you want to display the private products, like for example:

[products limit="12" columns="4" paginate="true"]

You will set shortcode arguments as you wish (like number of columns, number of items per page, enable pagination and so on)

Then to query all private products, use following (replacing below 102 by the page ID where you are using the shortcode):

add_filter( 'woocommerce_shortcode_products_query', 'display_private_product_list', 10, 3 );
function display_private_product_list( $query_args, $atts, $loop_name ){
    if( get_the_id() == 102 ){
        if( ! is_user_logged_in() ){
            $query_args['post_status'] = 'private';
        } else {
            $query_args['post_type'] = 'nothing'; // Display nothing for logged users
        }
    }
    return $query_args;
}

Code goes in function.php file of the active child theme (or active theme). Tested and works.

It will display all private products for non logged users.

Upvotes: 1

Related Questions