You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除WooCommerce中WC Product Stock Status插件的库存前数量数字?

Solution to Remove Quantity Number from WC Product Stock Status

Hey there, since the WC Product Stock Status plugin is outdated and you can’t target the quantity with CSS alone, let’s work through two solid fixes for this:

1. Frontend Quick Fix with JavaScript

This method tweaks the page content after it loads to strip out the quantity number. It’s easy to implement if you don’t want to dive into backend code:

document.addEventListener('DOMContentLoaded', function() {
  const stockLabels = document.querySelectorAll('.woocommerce div.product p.stock');
  
  stockLabels.forEach(label => {
    // Remove leading numbers and any following whitespace
    label.textContent = label.textContent.replace(/^\d+\s*/, '');
    // If your status uses a separator like " - " (e.g. "24 - In Stock"), use this regex instead:
    // label.textContent = label.textContent.replace(/^\d+\s*-\s*/, '');
  });
});

To add this code:

  • Head to your WordPress dashboard → Appearance → Customize
  • Look for a "Custom JavaScript" section (most modern themes include this) and paste the code there
  • Or, if your theme doesn’t have that option, add it to your child theme’s footer.php file right before the closing </body> tag, wrapped in <script> tags.

2. Backend Reliable Fix with PHP Hooks

This method filters the stock text before it’s rendered on the page, which is a cleaner, more permanent solution. Add this code to your child theme’s functions.php file (always use a child theme to avoid losing changes when your parent theme updates):

add_filter('woocommerce_get_stock_html', 'strip_quantity_from_stock_status', 10, 2);
function strip_quantity_from_stock_status($html, $product) {
  // Remove HTML tags to get raw stock text
  $raw_stock_text = strip_tags($html);
  
  // Strip leading numbers and extra whitespace
  $cleaned_status = preg_replace('/^\d+\s*/', '', $raw_stock_text);
  
  // Reapply the original color span from your CSS
  if (strpos($cleaned_status, 'in stock') !== false) {
    $html = '<p class="stock"><span class="s_in_stock_color">' . esc_html($cleaned_status) . '</span></p>';
  } else {
    // Adjust this part if you need to handle out-of-stock or other statuses
    $html = '<p class="stock">' . esc_html($cleaned_status) . '</p>';
  }
  
  return $html;
}

Quick Notes:

  • If your stock status has a unique format (like "15 Available"), adjust the regex pattern to match how your quantity is separated from the status text.
  • Test either method on a staging site first to make sure it doesn’t break other parts of your product page.

内容的提问来源于stack exchange,提问作者DrMTR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:14:10