Hiding short description text for Logged In users

Viewed 125

I have the below code that shows text above my product short description in woocommerce to a new visitor on my website. After performing the action (Creating an account) required based on the text, I would like to hide the text as it is no longer needed.

add_filter('woocommerce_short_description','ts_add_text_short_descr');
function ts_add_text_short_descr($description){
    echo 'To check if this item qualifies for a discount <a href="https://www.livestainable.co.za/my-account-2/"><u><strong>Log In or Create</strong></u></a> an account.</br></br>';
    return $description.$text;
}
1 Answers

To show some text only for guest, simply use ! is_user_logged_in() in an IF statement, like in this revisited code:

add_filter( 'woocommerce_short_description', 'add_text_before_short_description_for_guests' );
function add_text_before_short_description_for_guests( $description ) {
    // Only for guest users
    if ( ! is_user_logged_in() ) {
        $description = '<p>' . sprintf( __( "To check if this item qualifies for a discount %s an account."), '<a href="https://www.livestainable.co.za/my-account-2/"><u><strong>' . __("Log In or Create") . '</strong></u></a>' ) . '</p>' . $description;
    }
    return $description;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Related