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

求助:修改WooCommerce库存剩余2件及以下时的提示文本

修改WooCommerce低库存提示文本的方法
  • 直接通过WooCommerce的过滤器钩子修改,将以下代码添加到主题的functions.php文件(推荐使用子主题,避免主题更新丢失代码)或自定义插件中:
add_filter( 'woocommerce_get_availability_text', 'custom_low_stock_notice_text', 10, 2 );
function custom_low_stock_notice_text( $text, $product ) {
    // 匹配库存≤2且商品在售的情况
    if ( $product->get_stock_quantity() <= 2 && $product->is_in_stock() ) {
        $text = sprintf( 'Hurry! Only %d left in stock', $product->get_stock_quantity() );
    }
    return $text;
}
  • 代码作用:拦截默认的库存提示文本,当商品库存数量为2件及以下时,自动在原有文本前添加"Hurry!"前缀,同时保留动态的库存数字。
  • 验证方式:添加代码后,刷新商品页面,查看低库存商品的提示文本是否已更新。

内容的提问来源于stack exchange,提问作者Daphne V.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 01:45:26