【问题标题】:Show/Hide product based on user role and custom taxonomy根据用户角色和自定义分类显示/隐藏产品
【发布时间】:2020-08-21 09:50:24
【问题描述】:

我正在尝试根据用户角色显示/隐藏产品。我有一个功能可以完美地用于索引/搜索/类别页面,但是如果您在单个产品页面上,我用来尝试隐藏产品的代码。它基本上卡在网站任何页面上的重定向循环中。

基本上,如果您以“super_wholesale_user”身份登录并且产品不在分类术语 ID“380”中,它应该重定向您并向您显示该通知。

我使用的代码是这样的:

// On single product pages, redirect to shop and display a custom notice for specific products
add_action( 'template_redirect', 'wcpp_redirect_hiding_product_detail' );
function wcpp_redirect_hiding_product_detail() {

if ( ( is_user_logged_in() || ! current_user_can('super_wholesale_customer') )
&& has_term( '380', 'Role' ) && is_product() ) {
}

else {
    // Add a notice (optional)
    wc_add_notice(__("Sorry, but you are not allowed yet to see this product."), 'notice' );

    // Shop redirection url
    $redirect_url = get_permalink( get_option('woocommerce_shop_page_id') );
    wp_redirect($redirect_url); // Redirect to shop

    exit(); // Always exit
}

}

【问题讨论】:

    标签: php wordpress function woocommerce


    【解决方案1】:

    我想通了。以下代码供参考:

    // On single product pages, redirect to shop and display a custom notice for specific products
    add_action( 'template_redirect', 'wcpp_redirect_hiding_product_detail' );
    function wcpp_redirect_hiding_product_detail() {
    
    if ( is_product() ) {
    
        if ( ( is_user_logged_in() || ! current_user_can('super_wholesale_customer') )
        && has_term( '380', 'Role' ) && is_product() ) {
        }
        
        else {
            // Add a notice (optional)
            wc_add_notice(__("Sorry, but you are not allowed yet to see this product."), 'notice' );
    
            // Shop redirection url
            $redirect_url = get_permalink( get_option('woocommerce_shop_page_id') );
            wp_redirect($redirect_url); // Redirect to shop
    
            exit(); // Always exit
        };
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-24
      • 2012-12-14
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      • 2021-06-11
      • 2017-12-13
      • 2021-08-05
      • 1970-01-01
      相关资源
      最近更新 更多