求助:移除WooCommerce全站商品价格范围(仅单产品页面生效,商品目录及首页产品滑块仍存在)
Hey Chris, nice work getting the single product page sorted out! Let’s tackle those stubborn price ranges still showing up on your shop archives and homepage product slider to get this working site-wide. Here are two tailored approaches depending on your end goal:
Option 1: Quick CSS Fix (Hide Price Ranges Entirely)
If you just want to wipe out price ranges (and all price displays) from archives and the homepage slider immediately, add this CSS to your theme’s customizer or style.css file:
/* Hide price ranges on shop archives */ .archive .products li.product .price, /* Hide price ranges on homepage product slider */ .home .product-slider .product .price { display: none !important; }
This is a fast solution if you don’t need to show any price in those areas. If you’d prefer to show a single fixed price (like the lowest available price for variable products) instead of hiding prices entirely, check out Option 2.
Option 2: Replace Price Ranges with a Single Price (PHP + JS)
This approach replaces the messy price range with a clean single price on archives and sliders, and can handle variant selections if those areas have variant pickers (uncommon but possible):
Step 1: Add PHP Code to Your Theme’s functions.php
This code hooks into WooCommerce’s price output filter to override range displays with the lowest available price for variable products:
add_filter('woocommerce_get_price_html', 'replace_archive_price_range', 10, 2); function replace_archive_price_range($price, $product) { // Target shop archives, product categories/tags, and homepage if (is_shop() || is_product_category() || is_product_tag() || is_front_page()) { if ($product->is_type('variable')) { // Fetch and display the lowest variation price $min_price = $product->get_variation_price('min', true); $price = wc_price($min_price); } // Simple products keep their regular price (adjust this if needed) } return $price; }
Step 2: Add JS for Variant Selection (If Applicable)
If your homepage slider or archive pages include variant pickers, add this script to update the price when a user selects a variant—mirroring your single product page functionality:
jQuery(document).ready(function($) { // Target variant forms in archives and homepage slider $('.archive .variations_form, .home .product-slider .variations_form').each(function() { var $form = $(this); var $priceElement = $form.closest('.product').find('.price'); var originalPrice = $priceElement.html(); $form.on('show_variation', function(event, variation) { $priceElement.html(wc_price(variation.display_price)); }); $form.on('hide_variation', function() { $priceElement.html(originalPrice); }); }); });
Why Your Previous Hook Didn’t Work
The woocommerce_after_shop_loop_item_title hook is meant for adding content after the product title, but WooCommerce already outputs the price using woocommerce_template_loop_price (which hooks into that same action with priority 10). To modify the price itself, you need to use the woocommerce_get_price_html filter—it lets you directly override the price output instead of trying to work around existing content.
内容的提问来源于stack exchange,提问作者Chris Xanthou

