Woocommerce hide prices

<?php

// check if wc_hide_prices_categories isset and current product category is in it
function lvds_do_hide_prices_for_product($product = false) {
  global $product;
  $hidden_categories = get_field('wc_hide_prices_categories', 'option');
  if (
    (get_field('wc_hide_prices', 'option') && !$hidden_categories) || 
    ($hidden_categories ? has_term($hidden_categories, 'product_cat', $product->get_ID()) : false)
    ) {
    return false;
  }
  return true;
}

// if wc_hide_prices is enabled, activate this file hooks
add_action( 'init', 'lvds_hide_wc_prices' );
function lvds_hide_wc_prices() {   
  if ( get_field('wc_hide_prices', 'option') && !is_user_logged_in() ) {
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
    remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );   
    add_action( 'woocommerce_after_shop_loop_item', 'lvds_woocommerce_template_loop_add_to_cart', 10 );
    add_action( 'woocommerce_single_product_summary', 'lvds_woocommerce_template_single_add_to_cart', 30 );
    add_action( 'woocommerce_single_product_summary', 'lvds_woocommerce_template_single_price', 10 );
    add_action( 'woocommerce_after_shop_loop_item_title', 'lvds_woocommerce_template_loop_price', 10 );
  }
}

// add conditional hide-prices for all price-related hooks
function lvds_woocommerce_template_loop_price() {
  if ( lvds_do_hide_prices_for_product() ) {
    woocommerce_template_loop_price();
  }
}
function lvds_woocommerce_template_single_price() {
  if ( lvds_do_hide_prices_for_product() ) {
    woocommerce_template_single_price();
  }
}
function lvds_woocommerce_template_loop_add_to_cart() {
  if ( lvds_do_hide_prices_for_product() ) {
    woocommerce_template_loop_add_to_cart();
  } else {
    lvds_print_accountlink();
  }
}
function lvds_woocommerce_template_single_add_to_cart() {
  if ( lvds_do_hide_prices_for_product() ) {
    woocommerce_template_single_add_to_cart();
  } else {
    lvds_print_accountlink();
  }
}

// show a myaccount-link button
function lvds_print_accountlink() {
  global $product;
  echo '<a href="' . get_permalink(wc_get_page_id('myaccount')) . '?redirect_to=' . esc_url(get_permalink($product->get_ID())) . '" class="button">' . __('Bekijk prijzen', 'nbsals') . '</a>';
}
Last Updated:
Contributors: Niek Vlam, Suite Seven