簡體   English   中英

如果用戶未激活,則在Woocommerce中隱藏價格

[英]Hide price in Woocommerce if user not activated

我正在為新的WordPress商店使用WooCommerce,我想向“待定”用戶隱藏價格,直到我批准它們為止。

我使用“新用戶批准”插件為所有新用戶添加“待處理”狀態,但我不知道如何隱藏價格。 我做了些什么,但是沒有用:

if (is_user_logged_in()) {
$nua = pw_new_user_approve();
$status = $nua->get_user_status(get_current_user_id());
    if ($status !== 'pending') {
            // User is logged in and approved.
            if ($price_html = $product->get_price_html()) : ?>
            <span class="price"><?php echo $price_html; ?></span>
            <?php endif;    
    }
}

我有完全一樣的問題,只是偶然發現的。

您的代碼看起來不錯,我盲目地將其刪除,並通過PHP SYntax Checker(用Google搜索)修復了PHP錯誤。

這是工作代碼,只需要在<? PHP的? >正確標記,並且花括號{}和IF / END IF也稍微標記。

<? php if(is_user_logged_in()){

$nua = pw_new_user_approve();
$status = $nua->get_user_status(get_current_user_id());

if ($status !== 'pending') { ?>
      <?php if ($price_html = $product->get_price_html()) : ?>
        <span class="price"><?php echo $price_html; ?></span>
      <?php endif; ?>

<? php}; }; >

** <和?之間沒有空格。 和php,我必須在這里這樣寫才能看到完整的代碼

您可以使用的另一個最佳選擇是,將以下代碼添加到當前主題的function.php文件中:

add_filter('woocommerce_get_price_html','members_only_price');
function members_only_price($price){
  if(is_user_logged_in() ){
    return $price;
  }
  else {
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
    return 'Only <a href="' .get_permalink(woocommerce_get_page_id('myaccount')). '">Registered Users</a> are able to view pricing.';
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM