skip to Main Content

I want to hide the price for the users that aren’t logged on to my site. I managed to hide the price for single products and for variable products. In variable products that have different prices for single variants, this code does not work. How can I fix it?

add_action( 'init', 'nascondi_prezzo_se_non_loggato' );
function nascondi_prezzo_se_non_loggato() {
    if ( ! is_user_logged_in() ) {
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
        remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
        remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );

        add_filter( 'woocommerce_is_purchasable', '__return_false' );
        add_action( 'woocommerce_single_product_summary', 'mostra_testo_alternativo', 31 );
        add_action( 'woocommerce_after_shop_loop_item', 'mostra_testo_alternativo', 11 );
    }
}

function mostra_testo_alternativo() {
    echo '<a href="http://spaf.whitelabstaging.it/registrazione-login/"><div class="button-prezzo">' . __( 'Accedi per vedere i prezzi', 'JupiterX' ) . '</div></a>';
}

This is the code that I used and that only partially solves the problem.

2

Answers


  1. Chosen as BEST ANSWER

    To help everyone: I use the code above that hides the price and part of my code to hide the add to cart button

    add_filter( 'woocommerce_get_price_html', 'hide_price_not_logged_in', 10, 2 );
    function hide_price_not_logged_in( $price, $product ) {
        if ( ! is_user_logged_in() ) {
            $price = '';
        }
        return $price;
    }
    
    add_action( 'init', 'nascondi_prezzo_se_non_loggato' );
    function nascondi_prezzo_se_non_loggato() {
        if ( ! is_user_logged_in() ) {
            remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
            remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
            add_filter( 'woocommerce_is_purchasable', '__return_false' );
            add_action( 'woocommerce_single_product_summary', 'mostra_testo_alternativo', 31 );
            add_action( 'woocommerce_after_shop_loop_item', 'mostra_testo_alternativo', 11 );
        }
    }
    
    function mostra_testo_alternativo() {
        echo '<a href="#"><div class="button-prezzo">' . __( 'Accedi per vedere i prezzi', 'your-theme-name' ) . '</div></a>';
    }
    

  2. Try the code snippet below. The price is set to an empty string that won’t display the price for users who aren’t logged in. This uses the filter woocommerce_get_price_html which returns the code that outputs the price. In the empty string, add what you want displayed instead of the price.

    add_filter( 'woocommerce_get_price_html', 'hide_price_not_logged_in', 10, 2 );
    function hide_price_not_logged_in( $price, $product ) {
        if ( ! is_user_logged_in() ) {
            $price = '';
        }
        return $price;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search