如果没有登录,WooCommerce会隐藏某个类别的所有产品

时间:2017-06-08 06:34:54

标签: php wordpress woocommerce

我一直在寻找这个答案,甚至已经下载了多个插件,所有插件都无法使用。

有没有办法用PHP实现这个目标?

我有这段代码:

 <?php if( !is_user_logged_in() && get_field('___unit_price') ): ?>
 <p>Unit Price: $<?php the_field('___unit_price'); ?></p>
 <?php endif; ?>

这似乎适用于特定产品,但不适用于整个类别,任何帮助都会有很多指定

2 个答案:

答案 0 :(得分:1)

请在子主题的 function.php 中使用以下内容:

/* hide YOURCATEGORY category for not loggedin user
===================================================*/

    if ( !is_user_logged_in()) {
        function custom_pre_get_posts_q( $q ) {

            $tax_query = (array) $q->get( 'tax_query' );

            $tax_query[] = array(
                   'taxonomy' => 'product_cat',
                   'field' => 'slug',
                   'terms' => array( 'YOURCATEGORY' ), // Don't display products in the composite category on the shop page.
                   'operator' => 'NOT IN'
            );


            $q->set( 'tax_query', $tax_query );

        }
        add_action( 'woocommerce_product_query', 'custom_pre_get_posts_q' );
    }

不要忘记更换“YOURCATEGORY”&#39;通过您想要隐藏的类别的slu ..

干杯

答案 1 :(得分:0)

var canvas = new fabric.Canvas('c', {width: 1280, height: 720});
canvas.setDimensions({width: '640px', height: '360px'}, {cssOnly: true});

将此添加到您的functions.php文件中以获取更多信息以查看此链接 https://businessbloomer.com/woocommerce-hide-price-add-cart-logged-users/

相关问题