简体   繁体   中英

Hide price in Woocommerce if user not activated

I'm using WooCommerce for my new WordPress store and I want to hide prices from "pending" users, until I will approve them.

I use New User Approve plugin to add "pending" status for all new users but I don't know how to hide the price. I did something but it's not working:

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;    
    }
}

I have the exact same issue and just found this by chance.

Your code looks alright, I chucked it in blindly and fixed up the PHP errors via a PHP SYntax Checker (googled it).

Here is the working code, it just needed to be closed off on the < ? php ? > tags properly, and also your curly braces { } and IF/END IF's slightly.

< ? 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 }; }; ? >

** There is no space between the the < and ? and php, i had to write it in like that here for full code visible

An another best option you can go with, add below code to your current theme's function.php file:

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.';
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM