简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM