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

WooCommerce单品页订阅价格文本替换代码失效求助

解决WooCommerce订阅价格文本替换问题

你的代码失效主要有两个原因:一是依赖the_content过滤器修改HTML结构,订阅价格可能在该过滤器之后生成,或者正则匹配的table结构不准确;二是直接操作HTML的方式容易受主题/插件更新影响,稳定性差。

推荐使用WooCommerce Subscriptions官方提供的专用过滤器来修改价格字符串,这是更可靠的方案:

function customize_subscription_price_string( $price_string, $product, $args ) {
    // 仅在单品页生效
    if ( is_product() ) {
        // 替换目标文本
        $price_string = str_replace( 'every 3 days', 'Twice a Week', $price_string );
        $price_string = str_replace( '/ week', '/ Week', $price_string );
        $price_string = str_replace( 'one a Week', 'Once a Week', $price_string );
    }
    return $price_string;
}
add_filter( 'woocommerce_subscriptions_product_price_string', 'customize_subscription_price_string', 10, 3 );

注意事项

  • 确保目标产品是订阅型产品,普通产品不会触发该过滤器。
  • 如果仍不生效,可尝试调整过滤器优先级(比如将10改为99),确保在其他价格修改逻辑之后执行。
  • 修改后清除WooCommerce缓存和浏览器缓存,避免缓存导致的内容不更新。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 14:28:13