【问题标题】:How do I hide WooCommerce products with a given attribute based on user role?如何根据用户角色隐藏具有给定属性的 WooCommerce 产品?
【发布时间】:2021-05-28 07:46:35
【问题描述】:

我有一个“批发商”用户。我正在寻找一种方法来隐藏具有给定属性“nonwh”的产品。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    您没有提供任何代码,但您可能可以使用 wp_get_current_user() 函数来使用某些东西。和wc_get_product() 函数。

    $user = wp_get_current_user();
    if ( in_array( 'Wholesaler', (array) $user->roles ) ) {
    
    // This checks too see if the user has the role of Wholesaler.
    
    }
    

    也许将上面的内容包裹在 wc_get_product() 函数周围。

        $products = wc_get_product( $product_id );
        
        $nonwh = $product->get_attribute('nonwh'); 
        
            if( ! empty( $nonwh ) ){
                
    // Do nothing if product has the nonwh attribute
    
            } else {
            //Show Products
        }
    

    这是在此处的答案中找到的功能: How to check if product has a specific product attribute in Woocommerce

    这应该是您想要完成的工作的良好开端。

    【讨论】:

      猜你喜欢
      • 2016-12-24
      • 2019-03-05
      • 1970-01-01
      • 2016-06-01
      • 2021-03-01
      • 2021-08-05
      • 2021-08-17
      • 1970-01-01
      • 2023-01-27
      相关资源
      最近更新 更多