猿问

在 WooCommerce 中显示未登录用户的私人产品列表

出于某种原因,我需要在 WooCommerce 的单个页面上为访客用户(非登录用户)显示私人产品列表。这如何通过(或不通过)编程来完成?


泛舟湖上清波郎朗
浏览 149回答 1
1回答

守着星空守着你

您可以在要显示私人产品的特定页面上使用普通的 woocommerce 短代码,例如:[products limit="12" columns="4" paginate="true"]您将根据需要设置短代码参数(如列数、每页项目数、启用分页等) ……然后要查询所有私人产品,请使用以下(用102您使用短代码的页面 ID 在下面替换):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;}代码位于活动子主题(或活动主题)的 function.php 文件中。测试和工作。它将显示未登录用户的所有私人产品。
随时随地看视频慕课网APP
我要回答