代码之家  ›  专栏  ›  技术社区  ›  Farrukh Nasir

在WooCommerce上为特定类别或类别的用户隐藏产品价格

  •  0
  • Farrukh Nasir  · 技术社区  · 2 年前

    我有一个代码,用于隐藏未登录用户的访客用户的产品价格,但它仅限于非特定类别的所有产品。有些代码可以帮助我将代码转换为目标特定类别

    // Hide prices
    add_action('after_setup_theme','magik_activate_filter') ;
    
    function magik_activate_filter()
    {
       add_filter('woocommerce_get_price_html', 'magik_show_price_logged');
    }
    
    function magik_show_price_logged($price)
    {
       if(is_user_logged_in() )
       {
          return $price;
       }
       else
      {
         remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
         remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
         return '<a href="' . get_permalink(woocommerce_get_page_id('myaccount')) . '">Login to order</a>';
      }
    }
    
    //Option Two (If you decided to use Option One then don't add the following code)
    add_filter('woocommerce_is_purchasable', 'my_woocommerce_is_purchasable', 10, 2);
    function my_woocommerce_is_purchasable($is_purchasable, $product) {
    
        $isLoggedIn = is_user_logged_in();
        if(true == $isLoggedIn){
            //Make product purchasable to logged in user
            return true;
        }
    
        //Make product not purchasable to unlogged in user
        return false;
    }
    
    1 回复  |  直到 2 年前
        1
  •  0
  •   Marmik    2 年前

    您可以使用hasterm函数来检查产品是否属于某个类别。 以下是代码

    // Hide prices
    add_action('after_setup_theme', 'magik_activate_filter');
    
    function magik_activate_filter() {
        add_filter('woocommerce_get_price_html', 'magik_show_price_logged');
    }
    
    function magik_show_price_logged($price) {
        global $post;
    
        if (is_user_logged_in()) {
            return $price;
        } else {
            $product_id = $post->ID;
            
            // Check if the product belongs to a specific category or categories
            $target_categories = array('category1', 'category2'); // Replace with your target category or categories
            $has_target_category = false;
    
            foreach ($target_categories as $target_category) {
                if (has_term($target_category, 'product_cat', $product_id)) {
                    $has_target_category = true;
                    break;
                }
            }
    
            if ($has_target_category) {
                remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
                remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10);
                return '<a href="' . get_permalink(woocommerce_get_page_id('myaccount')) . '">Login to order</a>';
            } else {
                return $price;
            }
        }
    }
    
    // Option Two (If you decided to use Option One then don't add the following code)
    add_filter('woocommerce_is_purchasable', 'my_woocommerce_is_purchasable', 10, 2);
    
    function my_woocommerce_is_purchasable($is_purchasable, $product) {
        $isLoggedIn = is_user_logged_in();
    
        if (true == $isLoggedIn) {
            // Make product purchasable to logged-in user
            return true;
        }
    
        // Make product not purchasable to unlogged-in user
        return false;
    }