如何仅在WooCommerce单个商品页面隐藏可变价格区间?
Targeted Solution to Hide WooCommerce Variable Price Range Only on Single Product Pages
Got it, I’ve dealt with this exact frustration before—most snippets you find online either nuke prices across all pages or break the variant-specific price display. Let’s fix this with a precise, no-fuss solution:
How It Works
We’ll use a WooCommerce filter to only target single product pages, hide the default variable price range, but leave the variant-specific prices intact when customers select an option. Other pages (shop, categories, etc.) will keep showing the price range as normal.
The Code
Add this to your theme’s functions.php file, or use a custom code plugin like Code Snippets (never edit core WooCommerce files directly!):
add_filter( 'woocommerce_variable_price_html', 'hide_variable_price_range_only_on_product_page', 10, 2 ); function hide_variable_price_range_only_on_product_page( $price, $product ) { // Only apply this logic to single product pages if ( is_product() ) { // Check if the product has available variations if ( $product->has_child() ) { // Return empty string to hide the range; variant prices load via JS later return ''; } } // For all other pages, return the original price range return $price; }
Key Details & Troubleshooting
- Why this works: The
woocommerce_variable_price_htmlfilter controls how the variable price range is rendered. We wrap the logic inis_product()to restrict it only to single product pages. Variant prices are loaded dynamically via JavaScript when a customer selects an option, so they won’t be affected by this snippet. - Theme conflicts?: If your theme uses a custom price template, try increasing the filter priority (change
10to99) to ensure your code overrides the theme’s output. - Test thoroughly:
- Visit a single product page: The price range should be hidden, but selecting a variant will show its specific price.
- Check your shop/category pages: All product price ranges should still display normally.
内容的提问来源于stack exchange,提问作者di7za
相关产品推荐
相关产品推荐

