Logged in users see hidden information? Easy to fix with code (for WooCommerce product pages)

exist WooCommerce In an e-commerce site, we sometimes want to provide theAdditional information for logged-in users, such as members-only offers, hidden content descriptions, and wholesale prices. And visitors who are not logged in can only see basic information to guide them to register or log in.

this kind of functionalityNo need to install plug-ins, which takes only a few lines of code to implement:

  • Specified content visible to logged-in users
  • Non-logged-in users see prompts to log in/register

This tutorial will show you how to easily implement the "Login Visible" feature in WooCommerce product detail pages with code that is secure, lightweight, and customizable.

Image [1] - Logged in users see hidden information? Easy to fix with code (for WooCommerce product pages)

I. Examples of applicable scenarios

  • Show members-only offers or price descriptions
  • Display "Purchase Instructions" for logged-in users only.
  • Display prices or contact information visible to wholesale customers
  • Encourage user registration: prompt "login to view more content" when not logged in

Second, the code implementation: login user visible content

Add the following code to the current theme's functions.php Documentation:

Open the path as:Backend - Appearance - Theme File Editorfunctions.php

Image [2] - Logged in users see hidden information? Easy to fix with code (for WooCommerce product pages)
add_action( 'woocommerce_single_product_summary', 'show_content_for_logged_in_users', 35 );

function show_content_for_logged_in_users() {
    if ( is_user_logged_in() ) {
        echo '<div class="custom-member-content">'; echo '
        echo '<strong>🎉 Members Only:</strong> Free 20 off coupon for orders over 100!' ;
        echo '</div>'; } else {
    } else {
        echo '<div class="custom-guest-message">'; echo 'Please start first.
        echo 'Please first <a href="/en/' . wp_login_url( get_permalink() ) . '/">log in</a> maybe <a href="/en/' . wp_registration_url() . '/">enrollment</a> Check out more deals later.' ;
        echo '</div>';
    }
}

III. Description and extension

  • The code uses WooCommerce hooks woocommerce_single_product_summaryInserted after the "Add to Cart" button.
  • is_user_logged_in() be WordPress Built-in function to determine if a user is logged in
  • wp_login_url() respond in singing wp_registration_url() Generate links to login/registration pages

Fourth, the page effect preview

Not seen by logged in users:
Please login or register to view more offers.

Logged in users see:
🎉 Exclusive for members: 20 off coupon for 100 off when you place an order!

Image [3] - Logged in users see hidden information? Easy to fix with code (for WooCommerce product pages)

You can replace the text content, button styles, add images, etc. in them as needed to achieve personalized effects.

V. Advancement recommendations (optional)

If further control of the display is desired:

  • Display different information according to user roles (e.g. only for "wholesale customers")
  • Hide the content with the get_field() combining ACF Plugin Dynamic Fetch
  • utilization current_user_can() Determine if a user with specific privileges

Sample code (shown by role):

if ( current_user_can( 'wholesale_customer' ) ) {
    echo 'This is what wholesale customers see.' ;
}

VI. Summary

With simple code judgments, it can be easily implemented in the WooCommerce Showing different content for different users in the product page can enhance the member experience and effectively guide visitors to register.


Contact Us
Can't read the tutorial? Contact us for a free answer! Free help for personal, small business sites!
Customer Service
Customer Service
Tel: 020-2206-9892
QQ咨询:1025174874
(iii) E-mail: info@361sale.com
Working hours: Monday to Friday, 9:30-18:30, holidays off
© Reprint statement
Author: linxiulian
THE END
If you like it, support it.
kudos511 share (joys, benefits, privileges etc) with others
commentaries sofa-buying

Please log in to post a comment

    No comments