Introduction: On WooCommerce driven websites, sometimes we want product prices to be displayed only to logged in users to promote registration and provide a richer user experience. This tutorial will guide you on how to implement this feature in WooCommerce, especially for wordprees websites.
Step 1: Understanding the Basics First of all, you need to understand the WordPress add_filter Function. This function allows us to modify data in specific parts of the website. In this example, we will use it to adjust the way WooCommerce product prices are displayed.
![图片[1]-WooCommerce实用教程:如何设置仅登录用户可见的专属价格-光子波动网 | WordPress教程、Elementor教程与故障修复](https://www.361sale.com/wp-content/uploads/2023/12/2023121807001716.png)
Step 2: Edit the functions.php file Next, you'll need to add the functions.php file to add code. Finding that file is usually done in the wp-content/themes/your-theme-name/ path. Remember to back up the original file before editing.
![图片[2]-WooCommerce实用教程:如何设置仅登录用户可见的专属价格-光子波动网 | WordPress教程、Elementor教程与故障修复](https://www.361sale.com/wp-content/uploads/2023/12/2023121806594470.png)
Step 3: Adding Custom Functions exist functions.php At the end of the file, add the following code:
add_filter('woocommerce_get_price_html', 'members_only_price');
function members_only_price($price) {
if (is_user_logged_in()) {
return $price; } else { if (is_user_logged_in())
} else {
$login_url = wp_login_url(get_permalink());
$register_url = wp_registration_url();
return 'Please <a href="/en/' . esc_url($login_url) . '/">log in</a> or <a href="/en/' . esc_url($register_url) . '/">register</a> to view the price.';
}
}
This code checks if the user is logged in. If not logged in, a message prompting for login or registration is displayed.
Step 4: Testing Functionality Save the changes and test them on the website. Make sure that users who are not logged in see a prompt to log in or register, and that logged in users can see the prices normally.
![图片[3]-WooCommerce实用教程:如何设置仅登录用户可见的专属价格-光子波动网 | WordPress教程、Elementor教程与故障修复](https://www.361sale.com/wp-content/uploads/2023/12/2023121807020552.png)
Conclusion: By following these steps, you can successfully set product prices on your WooCommerce website that are visible only to logged-in users. This not only increases the likelihood of users signing up, but also improves the professionalism and user experience of your website.
Link to this article:https://www.361sale.com/en/5430/The article is copyrighted and must be reproduced with attribution.





















![表情[wozuimei]-光子波动网 | WordPress教程、Elementor教程与故障修复](https://www.361sale.com/wp-content/themes/zibll/img/smilies/wozuimei.gif)
![表情[baoquan]-光子波动网 | WordPress教程、Elementor教程与故障修复](https://www.361sale.com/wp-content/themes/zibll/img/smilies/baoquan.gif)

No comments