WooCommerce is a highly customizable e-commerce plugin for WordPress, allowing users to manage products, prices, and a host of other options. However, sometimes you might want to hide either the sale price or the regular price for your products to simplify the display or for marketing purposes. In this article, I’ll guide you through the steps to hide the regular price or sale price in WooCommerce for both simple and variable products, along with examples and code snippets.
Why Would You Hide Prices in WooCommerce?
There are several reasons why you might want to hide either the regular or sale price:
- Marketing Strategy: Sometimes displaying only the sale price can create a cleaner and more attractive product listing.
- Simplicity: If showing both regular and sale prices is confusing or overwhelming for customers, showing only one can make your pricing clearer.
- Product Variation: For variable products, showing a range of sale prices without displaying the regular price might better match your brand’s strategy.
Let’s get into how you can achieve this by modifying your WooCommerce product display.
Prerequisites:
- WooCommerce Plugin: Make sure that WooCommerce is installed and active on your WordPress website.
- Access to Theme Files: You’ll need to access your theme’s
functions.php
file to add the code snippets.
Step 1: Hide Sale Price for Simple and Variable Products
If you want to hide the sale price and only display the regular price for your products, follow these steps:
Code to Hide Sale Price:
- Access the Theme Editor:
- From your WordPress dashboard, go to Appearance > Theme File Editor.
- Select your active theme and find the
functions.php
file.
- Add the Code: Copy and paste the following code into your theme’s
functions.php
file. This code will hide the sale price for both simple and variable products, showing only the regular price.
add_filter('woocommerce_get_price_html', 'hide_sale_price_for_all_products', 100, 2);
function hide_sale_price_for_all_products($price, $product) {
if ($product->is_type('variable')) {
// Get the variations
$prices = $product->get_variation_prices(true);
// Get the minimum and maximum regular price of the variations
$min_regular_price = current($prices['regular_price']);
$max_regular_price = end($prices['regular_price']);
if ($min_regular_price !== $max_regular_price) {
$price = wc_price($min_regular_price) . ' - ' . wc_price($max_regular_price);
} else {
$price = wc_price($min_regular_price);
}
} else {
// For simple products, show only the regular price if on sale
if ($product->is_on_sale()) {
$price = wc_price($product->get_regular_price());
}
}
return $price;
}
How It Works:
- For Simple Products: If the product is on sale, the regular price will be displayed instead of the sale price.
- For Variable Products: The code calculates the minimum and maximum regular prices for the product variations and displays either the price range or a single price, depending on the variations’ regular prices.
- Save Changes: Click “Update File” to save the changes. Now, when you check your product pages, the sale price will be hidden, and only the regular price will show.
Step 2: Hide Regular Price and Display Sale Price
If your goal is to hide the regular price and only show the sale price, especially during promotions or special offers, here’s the process.
Code to Hide Regular Price:
- Access the Theme Editor:
- Like before, go to Appearance > Theme File Editor and select the
functions.php
file.
- Like before, go to Appearance > Theme File Editor and select the
- Add the Code: Insert the following code into the
functions.php
file to hide the regular price and display only the sale price.
add_filter('woocommerce_get_price_html', 'hide_regular_price_for_all_products', 100, 2);
function hide_regular_price_for_all_products($price, $product) {
if ($product->is_type('variable')) {
// Get the variations
$prices = $product->get_variation_prices(true);
// Get the minimum and maximum sale price of the variations
$min_sale_price = current($prices['sale_price']);
$max_sale_price = end($prices['sale_price']);
if ($min_sale_price !== $max_sale_price) {
$price = wc_price($min_sale_price) . ' - ' . wc_price($max_sale_price);
} else {
$price = wc_price($min_sale_price);
}
} else {
// For simple products, show only the sale price if on sale
if ($product->is_on_sale()) {
$price = wc_price($product->get_sale_price());
}
}
return $price;
}
How It Works:
- For Simple Products: If the product is on sale, only the sale price will be displayed, and the regular price will be hidden.
- For Variable Products: The code checks for variations and calculates the minimum and maximum sale prices, showing the price range (or a single sale price) depending on the variation.
- Save Changes: Click “Update File” to save the changes. Now, when viewing your product pages, only the sale price will be visible, and the regular price will be hidden.
Step 3: Testing Your Changes
Once you’ve added the code, it’s essential to test how it affects both simple and variable products on your WooCommerce store.
- For Simple Products: Check if the prices are displayed as expected. Depending on whether the sale price or regular price is hidden, the product page should now show only one price.
- For Variable Products: Navigate to a variable product page. You should see either a single sale/regular price or a range if there are variations.
Customizing the Code Further
You can tweak the code further to meet more specific needs. For example:
- Displaying a Custom Message: You can modify the code to show custom text instead of a price when certain conditions are met (e.g., “Call for Price”).
- Conditional Pricing: You could add conditions based on user roles, such as showing different prices to logged-in users or wholesale customers.
Conclusion
By using simple code snippets in your WordPress theme’s functions.php
file, you can easily hide either the sale price or regular price for products in WooCommerce. This gives you greater flexibility in how you present pricing to your customers, whether you want to simplify the product display, emphasize discounts, or run a clean pricing strategy for variable products.
This guide provides the tools needed to take control of your WooCommerce pricing display, with solutions that are both functional and customizable to suit your specific needs.
Frequently Asked Questions (FAQs)
Q: Will these changes affect all products in my WooCommerce store?
A: Yes, the code applies globally to both simple and variable products. You can customize the code to target specific products or categories if needed.
Q: Can I undo these changes later?
A: Yes, simply remove the code from the functions.php
file to revert to the default WooCommerce price display.
Q: What happens if I change my theme? Should I add both codes?
A: When you change your theme, the changes in your current theme’s functions.php
file will be lost, and the default WooCommerce pricing display will return. If you still want to hide the regular or sale price in the new theme, you’ll need to reapply the correct code for your goal.
You should only apply one of the provided code snippets, based on whether you want to hide the sale price or the regular price. Using both simultaneously can cause conflicts.